Skip to content
Snippets Groups Projects
mjpeg.c 61.9 KiB
Newer Older
  • Learn to ignore specific revisions
  •         nb_components > MAX_COMPONENTS)
            return -1;
    
        s->nb_components = nb_components;
    
        s->h_max = 1;
        s->v_max = 1;
        for(i=0;i<nb_components;i++) {
            /* component id */
    
            s->component_id[i] = get_bits(&s->gb, 8) - 1;
    
            s->h_count[i] = get_bits(&s->gb, 4);
            s->v_count[i] = get_bits(&s->gb, 4);
            /* compute hmax and vmax (only used in interleaved case) */
            if (s->h_count[i] > s->h_max)
                s->h_max = s->h_count[i];
            if (s->v_count[i] > s->v_max)
                s->v_max = s->v_count[i];
            s->quant_index[i] = get_bits(&s->gb, 8);
            if (s->quant_index[i] >= 4)
                return -1;
    
            dprintf("component %d %d:%d id: %d quant:%d\n", i, s->h_count[i],
    	    s->v_count[i], s->component_id[i], s->quant_index[i]);
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
        
        if(s->v_max==1 && s->h_max==1 && s->lossless==1) s->rgb=1;
    
    
        /* if different size, realloc/alloc picture */
        /* XXX: also check h_count and v_count */
        if (width != s->width || height != s->height) {
    
            for(i=0;i<MAX_COMPONENTS;i++)
                av_freep(&s->current_picture[i]);
    
            s->width = width;
            s->height = height;
    
            /* test interlaced mode */
            if (s->first_picture &&
                s->org_height != 0 &&
                s->height < ((s->org_height * 3) / 4)) {
                s->interlaced = 1;
    
    //	    s->bottom_field = (s->interlace_polarity) ? 1 : 0;
    
            if(s->rgb){
                int w, h;
                w = s->width;
                h = s->height;
                if (s->interlaced)
                    w *= 2;
                s->linesize[0] = 4*w;
                s->current_picture[0] = av_mallocz(4*w * h);
                s->current_picture[1] = s->current_picture[2] = NULL;
            }else{
              for(i=0;i<nb_components;i++) {
    
                int w, h;
                w = (s->width  + 8 * s->h_max - 1) / (8 * s->h_max);
                h = (s->height + 8 * s->v_max - 1) / (8 * s->v_max);
                w = w * 8 * s->h_count[i];
                h = h * 8 * s->v_count[i];
    
                if (s->interlaced)
                    w *= 2;
    
                s->linesize[i] = w;
                s->current_picture[i] = av_mallocz(w * h);
    
    Alex Beregszaszi's avatar
    Alex Beregszaszi committed
    	    if (!s->current_picture[i])
    	    {
    		dprintf("error: no picture buffers allocated\n");
    		return -1;
    	    }
    
            s->first_picture = 0;
    
        if (len != (8+(3*nb_components)))
        {
    
    	dprintf("decode_sof0: error, len(%d) mismatch\n", len);
    
    static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
        int code;
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
        code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
    
    	dprintf("mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index,
    
                    &s->vlcs[0][dc_index]);
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    
        if(code)
            return get_xbits(&s->gb, code);
        else
            return 0;
    
    }
    
    /* decode block and dequantize */
    static int decode_block(MJpegDecodeContext *s, DCTELEM *block, 
                            int component, int dc_index, int ac_index, int quant_index)
    {
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
        int code, i, j, level, val;
    
        int16_t *quant_matrix;
    
        val = mjpeg_decode_dc(s, dc_index);
    
        if (val == 0xffff) {
            dprintf("error dc\n");
            return -1;
        }
    
        quant_matrix = s->quant_matrixes[quant_index];
    
        val = val * quant_matrix[0] + s->last_dc[component];
        s->last_dc[component] = val;
        block[0] = val;
        /* AC coefs */
        ac_vlc = &s->vlcs[1][ac_index];
        i = 1;
        for(;;) {
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    	code = get_vlc2(&s->gb, s->vlcs[1][ac_index].table, 9, 2);
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    
    
            if (code < 0) {
                dprintf("error ac\n");
                return -1;
            }
            /* EOB */
            if (code == 0)
                break;
            if (code == 0xf0) {
                i += 16;
            } else {
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                level = get_xbits(&s->gb, code & 0xf);
                i += code >> 4;
    
                if (i >= 64) {
                    dprintf("error count: %d\n", i);
                    return -1;
                }
    
                block[j] = level * quant_matrix[j];
                i++;
    
    static int mjpeg_decode_sos(MJpegDecodeContext *s)
    
        int len, nb_components, i, j, n, h, v, ret, point_transform, predictor;
    
        int mb_width, mb_height, mb_x, mb_y, vmax, hmax, index, id;
    
        int comp_index[4];
        int dc_index[4];
        int ac_index[4];
        int nb_blocks[4];
        int h_count[4];
        int v_count[4];
    
        const int block_size= s->lossless ? 1 : 8;
    
    
        /* XXX: verify len field validity */
        len = get_bits(&s->gb, 16);
        nb_components = get_bits(&s->gb, 8);
    
        if (len != 6+2*nb_components)
        {
    	dprintf("decode_sos: invalid len (%d)\n", len);
    	return -1;
        }
    
        /* XXX: only interleaved scan accepted */
        if (nb_components != 3)
    
        {
    	dprintf("decode_sos: components(%d) mismatch\n", nb_components);
    
        vmax = 0;
        hmax = 0;
        for(i=0;i<nb_components;i++) {
    
            id = get_bits(&s->gb, 8) - 1;
    
            /* find component index */
            for(index=0;index<s->nb_components;index++)
                if (id == s->component_id[index])
                    break;
            if (index == s->nb_components)
    
    	    dprintf("decode_sos: index(%d) out of components\n", index);
    
            comp_index[i] = index;
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    
    
            nb_blocks[i] = s->h_count[index] * s->v_count[index];
            h_count[i] = s->h_count[index];
            v_count[i] = s->v_count[index];
    
            dc_index[i] = get_bits(&s->gb, 4);
            ac_index[i] = get_bits(&s->gb, 4);
    
    
    	if (dc_index[i] < 0 || ac_index[i] < 0 ||
    	    dc_index[i] >= 4 || ac_index[i] >= 4)
    	    goto out_of_range;
    	switch(s->start_code)
    	{
    	    case SOF0:
    		if (dc_index[i] > 1 || ac_index[i] > 1)
    		    goto out_of_range;
    		break;
    	    case SOF1:
    	    case SOF2:
    		if (dc_index[i] > 3 || ac_index[i] > 3)
    		    goto out_of_range;
    		break;
    	    case SOF3:
    		if (dc_index[i] > 3 || ac_index[i] != 0)
    		    goto out_of_range;
    		break;	
    	}
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    
    
        predictor= get_bits(&s->gb, 8); /* lossless predictor or start of spectral (Ss) */
    
        skip_bits(&s->gb, 4); /* Ah */
        point_transform= get_bits(&s->gb, 4); /* Al */
    
    
        for(i=0;i<nb_components;i++) 
            s->last_dc[i] = 1024;
    
        if (nb_components > 1) {
            /* interleaved stream */
    
            mb_width  = (s->width  + s->h_max * block_size - 1) / (s->h_max * block_size);
            mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
    
        } else {
            h = s->h_max / s->h_count[comp_index[0]];
            v = s->v_max / s->v_count[comp_index[0]];
    
            mb_width  = (s->width  + h * block_size - 1) / (h * block_size);
            mb_height = (s->height + v * block_size - 1) / (v * block_size);
    
            nb_blocks[0] = 1;
            h_count[0] = 1;
            v_count[0] = 1;
        }
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
        if(s->avctx->debug & FF_DEBUG_PICT_INFO)
            printf("%s %s p:%d >>:%d\n", s->lossless ? "lossless" : "sequencial DCT", s->rgb ? "RGB" : "", predictor, point_transform);
        
    
        if(s->lossless){
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
            if(s->rgb){
                uint16_t buffer[2048][4];
                int left[3], top[3], topleft[3];
    
                const int linesize= s->linesize[0];
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                const int mask= (1<<s->bits)-1;
                
                for(i=0; i<3; i++){
                    buffer[0][i]= 1 << (s->bits + point_transform - 1);
                }
    
                for(mb_y = 0; mb_y < mb_height; mb_y++) {
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                    const int modified_predictor= mb_y ? 1 : predictor;
                    uint8_t *ptr = s->current_picture[0] + (linesize * mb_y);
    
                    if (s->interlaced && s->bottom_field)
                        ptr += linesize >> 1;
    
                    for(i=0; i<3; i++){
                        top[i]= left[i]= topleft[i]= buffer[0][i];
                    }
    
                    for(mb_x = 0; mb_x < mb_width; mb_x++) {
                        if (s->restart_interval && !s->restart_count)
                            s->restart_count = s->restart_interval;
    
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                        for(i=0;i<3;i++) {
                            int pred;
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                            topleft[i]= top[i];
                            top[i]= buffer[mb_x][i];
    
    
                            PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                            
                            left[i]= 
                            buffer[mb_x][i]= mask & (pred + (mjpeg_decode_dc(s, dc_index[i]) << point_transform));
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    
                        if (s->restart_interval && !--s->restart_count) {
    
                            align_get_bits(&s->gb);
                            skip_bits(&s->gb, 16); /* skip RSTn */
                        }
                    }
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    
                    if(s->rct){
    
                        for(mb_x = 0; mb_x < mb_width; mb_x++) {
                            ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200)>>2);
                            ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1];
                            ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1];
                        }
                    }else if(s->pegasus_rct){
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                        for(mb_x = 0; mb_x < mb_width; mb_x++) {
                            ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2])>>2);
                            ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1];
                            ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1];
                        }
                    }else{
                        for(mb_x = 0; mb_x < mb_width; mb_x++) {
                            ptr[4*mb_x+0] = buffer[mb_x][0];
                            ptr[4*mb_x+1] = buffer[mb_x][1];
                            ptr[4*mb_x+2] = buffer[mb_x][2];
                        }
                    }
    
                }
            }else{
                for(mb_y = 0; mb_y < mb_height; mb_y++) {
                    for(mb_x = 0; mb_x < mb_width; mb_x++) {
                        if (s->restart_interval && !s->restart_count)
                            s->restart_count = s->restart_interval;
    
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                        if(mb_x==0 || mb_y==0 || s->interlaced){
    
                            for(i=0;i<nb_components;i++) {
                                uint8_t *ptr;
                                int x, y, c, linesize;
                                n = nb_blocks[i];
                                c = comp_index[i];
                                h = h_count[i];
                                v = v_count[i];
                                x = 0;
                                y = 0;
                                linesize= s->linesize[c];
                                
                                for(j=0; j<n; j++) {
                                    int pred;
    
                                    ptr = s->current_picture[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
                                    if(y==0 && mb_y==0){
                                        if(x==0 && mb_x==0){
                                            pred= 128 << point_transform;
                                        }else{
                                            pred= ptr[-1];
                                        }
                                    }else{
                                        if(x==0 && mb_x==0){
                                            pred= ptr[-linesize];
                                        }else{
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                                            PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
    
                                        }
                                    }
                                    
                                    if (s->interlaced && s->bottom_field)
                                        ptr += linesize >> 1;
                                    *ptr= pred + (mjpeg_decode_dc(s, dc_index[i]) << point_transform);
    
                                    if (++x == h) {
                                        x = 0;
                                        y++;
                                    }
                                }
                            }
                        }else{
                            for(i=0;i<nb_components;i++) {
                                uint8_t *ptr;
                                int x, y, c, linesize;
                                n = nb_blocks[i];
                                c = comp_index[i];
                                h = h_count[i];
                                v = v_count[i];
                                x = 0;
                                y = 0;
                                linesize= s->linesize[c];
                                
                                for(j=0; j<n; j++) {
                                    int pred;
    
                                    ptr = s->current_picture[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                                    PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
    
                                    *ptr= pred + (mjpeg_decode_dc(s, dc_index[i]) << point_transform);
                                    if (++x == h) {
                                        x = 0;
                                        y++;
                                    }
                                }
                            }
                        }
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                        if (s->restart_interval && !--s->restart_count) {
    
                            align_get_bits(&s->gb);
                            skip_bits(&s->gb, 16); /* skip RSTn */
                        }
                    }
                }
            }
        }else{
          for(mb_y = 0; mb_y < mb_height; mb_y++) {
    
            for(mb_x = 0; mb_x < mb_width; mb_x++) {
    
                if (s->restart_interval && !s->restart_count)
                    s->restart_count = s->restart_interval;
    
    
                for(i=0;i<nb_components;i++) {
    
                    uint8_t *ptr;
    
                    int x, y, c;
                    n = nb_blocks[i];
                    c = comp_index[i];
                    h = h_count[i];
                    v = v_count[i];
                    x = 0;
                    y = 0;
                    for(j=0;j<n;j++) {
                        memset(s->block, 0, sizeof(s->block));
                        if (decode_block(s, s->block, i, 
                                         dc_index[i], ac_index[i], 
                                         s->quant_index[c]) < 0) {
    
                            dprintf("error y=%d x=%d\n", mb_y, mb_x);
    
    //		    dprintf("mb: %d %d processed\n", mb_y, mb_x);
    
                        ptr = s->current_picture[c] + 
                            (s->linesize[c] * (v * mb_y + y) * 8) + 
                            (h * mb_x + x) * 8;
    
                        if (s->interlaced && s->bottom_field)
                            ptr += s->linesize[c] >> 1;
    
                        s->idct_put(ptr, s->linesize[c], s->block);
    
    	    /* (< 1350) buggy workaround for Spectralfan.mov, should be fixed */
    
                if (s->restart_interval && (s->restart_interval < 1350) &&
    		!--s->restart_count) {
    
                    align_get_bits(&s->gb);
                    skip_bits(&s->gb, 16); /* skip RSTn */
                    for (j=0; j<nb_components; j++) /* reset dc */
                        s->last_dc[j] = 1024;
                }
    
     out_of_range:
        dprintf("decode_sos: ac/dc index out of range\n");
        return -1;
    
    static int mjpeg_decode_dri(MJpegDecodeContext *s)
    
    {
        if (get_bits(&s->gb, 16) != 4)
    	return -1;
        s->restart_interval = get_bits(&s->gb, 16);
    
        dprintf("restart interval: %d\n", s->restart_interval);
    
    static int mjpeg_decode_app(MJpegDecodeContext *s)
    
    {
        int len, id;
    
        /* XXX: verify len field validity */
    
        if (len < 5)
    	return -1;
    
    Alex Beregszaszi's avatar
    Alex Beregszaszi committed
        id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
        id = be2me_32(id);
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
        if(s->avctx->debug & FF_DEBUG_STARTCODE){
            printf("APPx %8X\n", id); 
        }
        
    
        /* buggy AVID, it puts EOI only at every 10th frame */
    
        /* also this fourcc is used by non-avid files too, it holds some
           informations, but it's always present in AVID creates files */
    
    	/* structure:
    	    4bytes	AVI1
    	    1bytes	polarity
    	    1bytes	always zero
    	    4bytes	field_size
    	    4bytes	field_size_less_padding
    	*/
        	s->buggy_avid = 1;
    
    //	if (s->first_picture)
    //	    printf("mjpeg: workarounding buggy AVID\n");
    	s->interlace_polarity = get_bits(&s->gb, 8);
    
    #if 0
    	skip_bits(&s->gb, 8);
    	skip_bits(&s->gb, 32);
    	skip_bits(&s->gb, 32);
    	len -= 10;
    #endif
    
    //	if (s->interlace_polarity)
    //	    printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity);
    
    	skip_bits(&s->gb, 8); /* the trailing zero-byte */
    	printf("mjpeg: JFIF header found (version: %x.%x)\n",
    	    get_bits(&s->gb, 8), get_bits(&s->gb, 8));
    
    	if (get_bits(&s->gb, 8) == 0)
    	{
    
    	    int x_density, y_density; 
    	    x_density = get_bits(&s->gb, 16);
    	    y_density = get_bits(&s->gb, 16);
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    
    
    	    dprintf("x/y density: %d (%f), %d (%f)\n", x_density,
    		(float)x_density, y_density, (float)y_density);
    #if 0
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                //MN: needs to be checked
    
    //                s->avctx->aspect_ratio= s->width*y_density/((float)s->height*x_density);
    		s->avctx->aspect_ratio = (float)x_density/y_density;
    		/* it's better, but every JFIF I have seen stores 1:1 */
    
                else
                    s->avctx->aspect_ratio= 0.0;
    
    	}
    	else
    	{
    	    skip_bits(&s->gb, 16);
    	    skip_bits(&s->gb, 16);
    	}
    
    	t_w = get_bits(&s->gb, 8);
    	t_h = get_bits(&s->gb, 8);
    	if (t_w && t_h)
    	{
    	    /* skip thumbnail */
    	    if (len-10-(t_w*t_h*3) > 0)
    		len -= t_w*t_h*3;
    	}
    	len -= 10;
    	goto out;
        }
        
        if (id == ff_get_fourcc("Adob") && (get_bits(&s->gb, 8) == 'e'))
        {
    	printf("mjpeg: Adobe header found\n");
    	skip_bits(&s->gb, 16); /* version */
    	skip_bits(&s->gb, 16); /* flags0 */
    	skip_bits(&s->gb, 16); /* flags1 */
    	skip_bits(&s->gb, 8); /* transform */
    	len -= 7;
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    
        if (id == ff_get_fourcc("LJIF")){
            printf("Pegasus lossless jpeg header found\n");
    	skip_bits(&s->gb, 16); /* version ? */
    	skip_bits(&s->gb, 16); /* unknwon always 0? */
    	skip_bits(&s->gb, 16); /* unknwon always 0? */
    	skip_bits(&s->gb, 16); /* unknwon always 0? */
            switch( get_bits(&s->gb, 8)){
            case 1:
                s->rgb= 1;
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                break;
            case 2:
                s->rgb= 1;
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                break;
            default:
                printf("unknown colorspace\n");
            }
            len -= 9;
            goto out;
        }
    
        if ((s->start_code == APP1) && (len > (0x28 - 8)))
    
    Alex Beregszaszi's avatar
    Alex Beregszaszi committed
    	id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
    	id = be2me_32(id);
    
    	if (id == ff_get_fourcc("mjpg")) /* Apple MJPEG-A */
    
    	{
    #if 0
    	    skip_bits(&s->gb, 32); /* field size */
    	    skip_bits(&s->gb, 32); /* pad field size */
    	    skip_bits(&s->gb, 32); /* next off */
    	    skip_bits(&s->gb, 32); /* quant off */
    	    skip_bits(&s->gb, 32); /* huff off */
    	    skip_bits(&s->gb, 32); /* image off */
    	    skip_bits(&s->gb, 32); /* scan off */
    	    skip_bits(&s->gb, 32); /* data off */
    #endif
    	    if (s->first_picture)
    		printf("mjpeg: Apple MJPEG-A header found\n");
    	}
        }
    
        /* slow but needed for extreme adobe jpegs */
        if (len < 0)
    	printf("mjpeg: error, decode_app parser read over the end\n");
        while(--len > 0)
    	skip_bits(&s->gb, 8);
    
    
    static int mjpeg_decode_com(MJpegDecodeContext *s)
    
    {
        /* XXX: verify len field validity */
    
    Alex Beregszaszi's avatar
    Alex Beregszaszi committed
        int len = get_bits(&s->gb, 16);
    
        if (len >= 2 && len < 32768) {
    	/* XXX: any better upper bound */
    
    	uint8_t *cbuf = av_malloc(len - 1);
    
    	if (cbuf) {
    	    int i;
    	    for (i = 0; i < len - 2; i++)
    		cbuf[i] = get_bits(&s->gb, 8);
    	    if (i > 0 && cbuf[i-1] == '\n')
    		cbuf[i-1] = 0;
    	    else
    		cbuf[i] = 0;
    
    	    printf("mjpeg comment: '%s'\n", cbuf);
    
    	    /* buggy avid, it puts EOI only at every 10th frame */
    	    if (!strcmp(cbuf, "AVID"))
    	    {
    		s->buggy_avid = 1;
    		//	if (s->first_picture)
    		//	    printf("mjpeg: workarounding buggy AVID\n");
    	    }
    
    #if 0
    static int valid_marker_list[] =
    {
            /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */
    /* 0 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    /* 1 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    /* 2 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    /* 3 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    /* 4 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    /* 5 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    /* 6 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    /* 7 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    /* 8 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    /* 9 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    /* a */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    /* b */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    /* c */    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    /* d */    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    /* e */    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    /* f */    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
    }
    #endif
    
    
    /* return the 8 bit start code value and update the search
       state. Return -1 if no start code found */
    
    static int find_marker(uint8_t **pbuf_ptr, uint8_t *buf_end)
    
        uint8_t *buf_ptr;
    
        while (buf_ptr < buf_end) {
            v = *buf_ptr++;
    	v2 = *buf_ptr;
            if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe)) {
    	    val = *buf_ptr++;
    	    goto found;
    
        val = -1;
    found:
    #ifdef DEBUG
        dprintf("find_marker skipped %d bytes\n", skipped);
    #endif
    
        *pbuf_ptr = buf_ptr;
        return val;
    }
    
    static int mjpeg_decode_frame(AVCodecContext *avctx, 
                                  void *data, int *data_size,
    
                                  uint8_t *buf, int buf_size)
    
    {
        MJpegDecodeContext *s = avctx->priv_data;
    
        uint8_t *buf_end, *buf_ptr;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        *data_size = 0;
    
    
        /* no supplementary picture */
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (buf_size == 0)
    
            return 0;
    
        buf_ptr = buf;
        buf_end = buf + buf_size;
        while (buf_ptr < buf_end) {
            /* find start next marker */
    
            start_code = find_marker(&buf_ptr, buf_end);
    	{
    	    /* EOF */
                if (start_code < 0) {
    		goto the_end;
    
                    dprintf("marker=%x avail_size_in_buf=%d\n", start_code, buf_end - buf_ptr);
    		
    		if ((buf_end - buf_ptr) > s->buffer_size)
    		{
    		    av_free(s->buffer);
    		    s->buffer_size = buf_end-buf_ptr;
    		    s->buffer = av_malloc(s->buffer_size);
    
    		    dprintf("buffer too small, expanding to %d bytes\n",
    			s->buffer_size);
    
    		    uint8_t *src = buf_ptr;
    		    uint8_t *dst = s->buffer;
    
    			uint8_t x = *(src++);
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    			    while(*src == 0xff) src++;
    
    
    			    x = *(src++);
    			    if (x >= 0xd0 && x <= 0xd7)
    				*(dst++) = x;
    			    else if (x)
    				break;
    			}
    		    }
    
    		    init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8);
    
    		    
    		    dprintf("escaping removed %d bytes\n",
    			(buf_end - buf_ptr) - (dst - s->buffer));
    
    		    init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8);
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                    if(s->avctx->debug & FF_DEBUG_STARTCODE){
                        printf("startcode: %X\n", start_code);
                    }
    
    
    		/* process markers */
    		if (start_code >= 0xd0 && start_code <= 0xd7) {
    		    dprintf("restart marker: %d\n", start_code&0x0f);
    		} else if (s->first_picture) {
    		    /* APP fields */
    		    if (start_code >= 0xe0 && start_code <= 0xef)
    
    		    /* Comment */
    		    else if (start_code == COM)
    			mjpeg_decode_com(s);
    		}
    
    
                    switch(start_code) {
                    case SOI:
    
                        /* nothing to do on SOI */
                        break;
                    case DQT:
    
                        if(mjpeg_decode_dht(s) < 0){
                            fprintf(stderr, "huffman table decode error\n");
                            return -1;
                        }
    
                        s->lossless=0;
                        if (mjpeg_decode_sof(s) < 0) 
    			return -1;
                        break;
                    case SOF3:
                        s->lossless=1;
                        if (mjpeg_decode_sof(s) < 0) 
    
    Alex Beregszaszi's avatar
    Alex Beregszaszi committed
    			return -1;
    
                            if (s->interlaced) {
                                s->bottom_field ^= 1;
                                /* if not bottom field, do not output image yet */
                                if (s->bottom_field)
    
                            for(i=0;i<3;i++) {
                                picture->data[i] = s->current_picture[i];
    
    			    picture->linesize[i] = (s->interlaced) ?
    				s->linesize[i] >> 1 : s->linesize[i];
    
                            }
                            *data_size = sizeof(AVPicture);
                            avctx->height = s->height;
    
                            if (s->interlaced)
                                avctx->height *= 2;
    
                            avctx->width = s->width;
    
                            /* XXX: not complete test ! */
                            switch((s->h_count[0] << 4) | s->v_count[0]) {
                            case 0x11:
    
                                if(s->rgb){
                                    avctx->pix_fmt = PIX_FMT_RGBA32;
                                }else
                                    avctx->pix_fmt = PIX_FMT_YUV444P;
    
                                break;
                            case 0x21:
                                avctx->pix_fmt = PIX_FMT_YUV422P;
                                break;
                            default:
                            case 0x22:
                                avctx->pix_fmt = PIX_FMT_YUV420P;
                                break;
                            }
    
                            /* dummy quality */
                            /* XXX: infer it with matrix */
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    //                    	avctx->quality = 3; 
    
    		    break;
                    case SOS:
                        mjpeg_decode_sos(s);
    		    /* buggy avid puts EOI every 10-20th frame */
    		    /* if restart period is over process EOI */
    		    if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
    			goto eoi_parser;
    
    		    break;
    		case SOF1:
    		case SOF2:
    		case SOF5:
    		case SOF6:
    		case SOF7:
    		case SOF9:
    		case SOF10:
    		case SOF11:
    		case SOF13:
    		case SOF14:
    		case SOF15:
    		case JPG:
    		    printf("mjpeg: unsupported coding type (%x)\n", start_code);
    
    		    break;
    //		default:
    //		    printf("mjpeg: unsupported marker (%x)\n", start_code);
    //		    break;
    
    
    not_the_end:
    		/* eof process start code */
    		buf_ptr += (get_bits_count(&s->gb)+7)/8;
    		dprintf("marker parser used %d bytes (%d bits)\n",
    		    (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb));
    
    the_end:
        dprintf("mjpeg decode frame unused %d bytes\n", buf_end - buf_ptr);
    //    return buf_end - buf_ptr;
    
    static int mjpegb_decode_frame(AVCodecContext *avctx, 
                                  void *data, int *data_size,
    
                                  uint8_t *buf, int buf_size)
    
    {
        MJpegDecodeContext *s = avctx->priv_data;
    
        uint8_t *buf_end, *buf_ptr;
    
        int i;
        AVPicture *picture = data;
        GetBitContext hgb; /* for the header */
        uint32_t dqt_offs, dht_offs, sof_offs, sos_offs, second_field_offs;
        uint32_t field_size;
    
        *data_size = 0;
    
        /* no supplementary picture */
        if (buf_size == 0)
            return 0;
    
        buf_ptr = buf;
        buf_end = buf + buf_size;
        
    read_header:
        /* reset on every SOI */
        s->restart_interval = 0;
    
    
        init_get_bits(&hgb, buf_ptr, /*buf_size*/(buf_end - buf_ptr)*8);
    
    
        skip_bits(&hgb, 32); /* reserved zeros */
        
        if (get_bits(&hgb, 32) != be2me_32(ff_get_fourcc("mjpg")))
        {
    	dprintf("not mjpeg-b (bad fourcc)\n");
    	return 0;
        }
    
        field_size = get_bits(&hgb, 32); /* field size */
        dprintf("field size: 0x%x\n", field_size);
        skip_bits(&hgb, 32); /* padded field size */
        second_field_offs = get_bits(&hgb, 32);
        dprintf("second field offs: 0x%x\n", second_field_offs);
        if (second_field_offs)
    	s->interlaced = 1;
    
        dqt_offs = get_bits(&hgb, 32);
        dprintf("dqt offs: 0x%x\n", dqt_offs);
        if (dqt_offs)
        {
    
    	init_get_bits(&s->gb, buf+dqt_offs, (buf_end - (buf+dqt_offs))*8);
    
    	s->start_code = DQT;
    	mjpeg_decode_dqt(s);
        }
        
        dht_offs = get_bits(&hgb, 32);
        dprintf("dht offs: 0x%x\n", dht_offs);
        if (dht_offs)
        {
    
    	init_get_bits(&s->gb, buf+dht_offs, (buf_end - (buf+dht_offs))*8);
    
    	s->start_code = DHT;
    	mjpeg_decode_dht(s);
        }
    
        sof_offs = get_bits(&hgb, 32);
        dprintf("sof offs: 0x%x\n", sof_offs);
        if (sof_offs)
        {
    
    	init_get_bits(&s->gb, buf+sof_offs, (buf_end - (buf+sof_offs))*8);
    
    	s->start_code = SOF0;
    
    	if (mjpeg_decode_sof(s) < 0)
    
    Alex Beregszaszi's avatar
    Alex Beregszaszi committed
    	    return -1;
    
        }
    
        sos_offs = get_bits(&hgb, 32);
        dprintf("sos offs: 0x%x\n", sos_offs);
        if (sos_offs)
        {
    
    //	init_get_bits(&s->gb, buf+sos_offs, (buf_end - (buf+sos_offs))*8);
    	init_get_bits(&s->gb, buf+sos_offs, field_size*8);
    
    	s->start_code = SOS;
    	mjpeg_decode_sos(s);
        }
    
        skip_bits(&hgb, 32); /* start of data offset */
    
        if (s->interlaced) {
            s->bottom_field ^= 1;
            /* if not bottom field, do not output image yet */
            if (s->bottom_field && second_field_offs)
    	{
    	    buf_ptr = buf + second_field_offs;
    	    second_field_offs = 0;
    	    goto read_header;
        	}
        }
    
        for(i=0;i<3;i++) {
            picture->data[i] = s->current_picture[i];
            picture->linesize[i] = (s->interlaced) ?
        	    s->linesize[i] >> 1 : s->linesize[i];
        }
        *data_size = sizeof(AVPicture);
        avctx->height = s->height;
        if (s->interlaced)
            avctx->height *= 2;
        avctx->width = s->width;
        /* XXX: not complete test ! */
        switch((s->h_count[0] << 4) | s->v_count[0]) {
            case 0x11:
        	    avctx->pix_fmt = PIX_FMT_YUV444P;
                break;
            case 0x21:
                avctx->pix_fmt = PIX_FMT_YUV422P;
                break;
            default:
    	case 0x22:
                avctx->pix_fmt = PIX_FMT_YUV420P;
                break;
        }
        /* dummy quality */
        /* XXX: infer it with matrix */