• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/mjpegdec.c

Go to the documentation of this file.
00001 /*
00002  * MJPEG decoder
00003  * Copyright (c) 2000, 2001 Fabrice Bellard
00004  * Copyright (c) 2003 Alex Beregszaszi
00005  * Copyright (c) 2003-2004 Michael Niedermayer
00006  *
00007  * Support for external huffman table, various fixes (AVID workaround),
00008  * aspecting, new decode_frame mechanism and apple mjpeg-b support
00009  *                                  by Alex Beregszaszi
00010  *
00011  * This file is part of FFmpeg.
00012  *
00013  * FFmpeg is free software; you can redistribute it and/or
00014  * modify it under the terms of the GNU Lesser General Public
00015  * License as published by the Free Software Foundation; either
00016  * version 2.1 of the License, or (at your option) any later version.
00017  *
00018  * FFmpeg is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021  * Lesser General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public
00024  * License along with FFmpeg; if not, write to the Free Software
00025  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00026  */
00027 
00033 //#define DEBUG
00034 #include <assert.h>
00035 
00036 #include "avcodec.h"
00037 #include "dsputil.h"
00038 #include "mjpeg.h"
00039 #include "mjpegdec.h"
00040 #include "jpeglsdec.h"
00041 
00042 
00043 static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table,
00044                       int nb_codes, int use_static, int is_ac)
00045 {
00046     uint8_t huff_size[256+16];
00047     uint16_t huff_code[256+16];
00048 
00049     assert(nb_codes <= 256);
00050 
00051     memset(huff_size, 0, sizeof(huff_size));
00052     ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
00053 
00054     if(is_ac){
00055         memmove(huff_size+16, huff_size, sizeof(uint8_t)*nb_codes);
00056         memmove(huff_code+16, huff_code, sizeof(uint16_t)*nb_codes);
00057         memset(huff_size, 0, sizeof(uint8_t)*16);
00058         memset(huff_code, 0, sizeof(uint16_t)*16);
00059         nb_codes += 16;
00060     }
00061 
00062     return init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2, use_static);
00063 }
00064 
00065 static void build_basic_mjpeg_vlc(MJpegDecodeContext * s) {
00066     build_vlc(&s->vlcs[0][0], ff_mjpeg_bits_dc_luminance,
00067               ff_mjpeg_val_dc, 12, 0, 0);
00068     build_vlc(&s->vlcs[0][1], ff_mjpeg_bits_dc_chrominance,
00069               ff_mjpeg_val_dc, 12, 0, 0);
00070     build_vlc(&s->vlcs[1][0], ff_mjpeg_bits_ac_luminance,
00071               ff_mjpeg_val_ac_luminance, 251, 0, 1);
00072     build_vlc(&s->vlcs[1][1], ff_mjpeg_bits_ac_chrominance,
00073               ff_mjpeg_val_ac_chrominance, 251, 0, 1);
00074 }
00075 
00076 av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
00077 {
00078     MJpegDecodeContext *s = avctx->priv_data;
00079 
00080     s->avctx = avctx;
00081     dsputil_init(&s->dsp, avctx);
00082     ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
00083     s->buffer_size = 0;
00084     s->buffer = NULL;
00085     s->start_code = -1;
00086     s->first_picture = 1;
00087     s->org_height = avctx->coded_height;
00088 
00089     build_basic_mjpeg_vlc(s);
00090 
00091     if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
00092     {
00093         av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n");
00094         init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8);
00095         if (ff_mjpeg_decode_dht(s)) {
00096             av_log(avctx, AV_LOG_ERROR, "mjpeg: error using external huffman table, switching back to internal\n");
00097             build_basic_mjpeg_vlc(s);
00098         }
00099     }
00100     if (avctx->extradata_size > 9 &&
00101         AV_RL32(avctx->extradata + 4) == MKTAG('f','i','e','l')) {
00102         if (avctx->extradata[9] == 6) { /* quicktime icefloe 019 */
00103             s->interlace_polarity = 1; /* bottom field first */
00104             av_log(avctx, AV_LOG_DEBUG, "mjpeg bottom field first\n");
00105         }
00106     }
00107 
00108     return 0;
00109 }
00110 
00111 
00112 /* quantize tables */
00113 int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
00114 {
00115     int len, index, i, j;
00116 
00117     len = get_bits(&s->gb, 16) - 2;
00118 
00119     while (len >= 65) {
00120         /* only 8 bit precision handled */
00121         if (get_bits(&s->gb, 4) != 0)
00122         {
00123             av_log(s->avctx, AV_LOG_ERROR, "dqt: 16bit precision\n");
00124             return -1;
00125         }
00126         index = get_bits(&s->gb, 4);
00127         if (index >= 4)
00128             return -1;
00129         av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index);
00130         /* read quant table */
00131         for(i=0;i<64;i++) {
00132             j = s->scantable.permutated[i];
00133             s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
00134         }
00135 
00136         //XXX FIXME finetune, and perhaps add dc too
00137         s->qscale[index]= FFMAX(
00138             s->quant_matrixes[index][s->scantable.permutated[1]],
00139             s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1;
00140         av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n", index, s->qscale[index]);
00141         len -= 65;
00142     }
00143 
00144     return 0;
00145 }
00146 
00147 /* decode huffman tables and build VLC decoders */
00148 int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
00149 {
00150     int len, index, i, class, n, v, code_max;
00151     uint8_t bits_table[17];
00152     uint8_t val_table[256];
00153 
00154     len = get_bits(&s->gb, 16) - 2;
00155 
00156     while (len > 0) {
00157         if (len < 17)
00158             return -1;
00159         class = get_bits(&s->gb, 4);
00160         if (class >= 2)
00161             return -1;
00162         index = get_bits(&s->gb, 4);
00163         if (index >= 4)
00164             return -1;
00165         n = 0;
00166         for(i=1;i<=16;i++) {
00167             bits_table[i] = get_bits(&s->gb, 8);
00168             n += bits_table[i];
00169         }
00170         len -= 17;
00171         if (len < n || n > 256)
00172             return -1;
00173 
00174         code_max = 0;
00175         for(i=0;i<n;i++) {
00176             v = get_bits(&s->gb, 8);
00177             if (v > code_max)
00178                 code_max = v;
00179             val_table[i] = v;
00180         }
00181         len -= n;
00182 
00183         /* build VLC and flush previous vlc if present */
00184         free_vlc(&s->vlcs[class][index]);
00185         av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n",
00186                class, index, code_max + 1);
00187         if(build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1, 0, class > 0) < 0){
00188             return -1;
00189         }
00190     }
00191     return 0;
00192 }
00193 
00194 int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
00195 {
00196     int len, nb_components, i, width, height, pix_fmt_id;
00197 
00198     /* XXX: verify len field validity */
00199     len = get_bits(&s->gb, 16);
00200     s->bits= get_bits(&s->gb, 8);
00201 
00202     if(s->pegasus_rct) s->bits=9;
00203     if(s->bits==9 && !s->pegasus_rct) s->rct=1;    //FIXME ugly
00204 
00205     if (s->bits != 8 && !s->lossless){
00206         av_log(s->avctx, AV_LOG_ERROR, "only 8 bits/component accepted\n");
00207         return -1;
00208     }
00209 
00210     height = get_bits(&s->gb, 16);
00211     width = get_bits(&s->gb, 16);
00212 
00213     //HACK for odd_height.mov
00214     if(s->interlaced && s->width == width && s->height == height + 1)
00215         height= s->height;
00216 
00217     av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height);
00218     if(avcodec_check_dimensions(s->avctx, width, height))
00219         return -1;
00220 
00221     nb_components = get_bits(&s->gb, 8);
00222     if (nb_components <= 0 ||
00223         nb_components > MAX_COMPONENTS)
00224         return -1;
00225     if (s->ls && !(s->bits <= 8 || nb_components == 1)){
00226         av_log(s->avctx, AV_LOG_ERROR, "only <= 8 bits/component or 16-bit gray accepted for JPEG-LS\n");
00227         return -1;
00228     }
00229     s->nb_components = nb_components;
00230     s->h_max = 1;
00231     s->v_max = 1;
00232     for(i=0;i<nb_components;i++) {
00233         /* component id */
00234         s->component_id[i] = get_bits(&s->gb, 8) - 1;
00235         s->h_count[i] = get_bits(&s->gb, 4);
00236         s->v_count[i] = get_bits(&s->gb, 4);
00237         /* compute hmax and vmax (only used in interleaved case) */
00238         if (s->h_count[i] > s->h_max)
00239             s->h_max = s->h_count[i];
00240         if (s->v_count[i] > s->v_max)
00241             s->v_max = s->v_count[i];
00242         s->quant_index[i] = get_bits(&s->gb, 8);
00243         if (s->quant_index[i] >= 4)
00244             return -1;
00245         av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n", i, s->h_count[i],
00246                s->v_count[i], s->component_id[i], s->quant_index[i]);
00247     }
00248 
00249     if(s->ls && (s->h_max > 1 || s->v_max > 1)) {
00250         av_log(s->avctx, AV_LOG_ERROR, "Subsampling in JPEG-LS is not supported.\n");
00251         return -1;
00252     }
00253 
00254     if(s->v_max==1 && s->h_max==1 && s->lossless==1) s->rgb=1;
00255 
00256     /* if different size, realloc/alloc picture */
00257     /* XXX: also check h_count and v_count */
00258     if (width != s->width || height != s->height) {
00259         av_freep(&s->qscale_table);
00260 
00261         s->width = width;
00262         s->height = height;
00263         s->interlaced = 0;
00264 
00265         /* test interlaced mode */
00266         if (s->first_picture &&
00267             s->org_height != 0 &&
00268             s->height < ((s->org_height * 3) / 4)) {
00269             s->interlaced = 1;
00270             s->bottom_field = s->interlace_polarity;
00271             s->picture.interlaced_frame = 1;
00272             s->picture.top_field_first = !s->interlace_polarity;
00273             height *= 2;
00274         }
00275 
00276         avcodec_set_dimensions(s->avctx, width, height);
00277 
00278         s->qscale_table= av_mallocz((s->width+15)/16);
00279 
00280         s->first_picture = 0;
00281     }
00282 
00283     if(s->interlaced && (s->bottom_field == !s->interlace_polarity))
00284         return 0;
00285 
00286     /* XXX: not complete test ! */
00287     pix_fmt_id = (s->h_count[0] << 28) | (s->v_count[0] << 24) |
00288                  (s->h_count[1] << 20) | (s->v_count[1] << 16) |
00289                  (s->h_count[2] << 12) | (s->v_count[2] <<  8) |
00290                  (s->h_count[3] <<  4) |  s->v_count[3];
00291     av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id);
00292     if(!(pix_fmt_id & 0x10101010))
00293         pix_fmt_id-= (pix_fmt_id & 0xF0F0F0F0)>>1;
00294     if(!(pix_fmt_id & 0x01010101))
00295         pix_fmt_id-= (pix_fmt_id & 0x0F0F0F0F)>>1;
00296 
00297     switch(pix_fmt_id){
00298     case 0x11111100:
00299         if(s->rgb){
00300             s->avctx->pix_fmt = PIX_FMT_RGB32;
00301         }else
00302             s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV444P : PIX_FMT_YUVJ444P;
00303         assert(s->nb_components==3);
00304         break;
00305     case 0x11000000:
00306         s->avctx->pix_fmt = PIX_FMT_GRAY8;
00307         break;
00308     case 0x12111100:
00309         s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV440P : PIX_FMT_YUVJ440P;
00310         break;
00311     case 0x21111100:
00312         s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV422P : PIX_FMT_YUVJ422P;
00313         break;
00314     case 0x22111100:
00315         s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420P;
00316         break;
00317     default:
00318         av_log(s->avctx, AV_LOG_ERROR, "Unhandled pixel format 0x%x\n", pix_fmt_id);
00319         return -1;
00320     }
00321     if(s->ls){
00322         if(s->nb_components > 1)
00323             s->avctx->pix_fmt = PIX_FMT_RGB24;
00324         else if(s->bits <= 8)
00325             s->avctx->pix_fmt = PIX_FMT_GRAY8;
00326         else
00327             s->avctx->pix_fmt = PIX_FMT_GRAY16;
00328     }
00329 
00330     if(s->picture.data[0])
00331         s->avctx->release_buffer(s->avctx, &s->picture);
00332 
00333     s->picture.reference= 0;
00334     if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
00335         av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00336         return -1;
00337     }
00338     s->picture.pict_type= FF_I_TYPE;
00339     s->picture.key_frame= 1;
00340 
00341     for(i=0; i<3; i++){
00342         s->linesize[i]= s->picture.linesize[i] << s->interlaced;
00343     }
00344 
00345 //    printf("%d %d %d %d %d %d\n", s->width, s->height, s->linesize[0], s->linesize[1], s->interlaced, s->avctx->height);
00346 
00347     if (len != (8+(3*nb_components)))
00348     {
00349         av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len);
00350     }
00351 
00352     /* totally blank picture as progressive JPEG will only add details to it */
00353     if(s->progressive){
00354         int bw = (width  + s->h_max*8-1) / (s->h_max*8);
00355         int bh = (height + s->v_max*8-1) / (s->v_max*8);
00356         for(i=0; i<s->nb_components; i++) {
00357             int size = bw * bh * s->h_count[i] * s->v_count[i];
00358             av_freep(&s->blocks[i]);
00359             av_freep(&s->last_nnz[i]);
00360             s->blocks[i] = av_malloc(size * sizeof(**s->blocks));
00361             s->last_nnz[i] = av_mallocz(size * sizeof(**s->last_nnz));
00362             s->block_stride[i] = bw * s->h_count[i];
00363         }
00364         memset(s->coefs_finished, 0, sizeof(s->coefs_finished));
00365     }
00366     return 0;
00367 }
00368 
00369 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
00370 {
00371     int code;
00372     code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
00373     if (code < 0)
00374     {
00375         av_log(s->avctx, AV_LOG_WARNING, "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index,
00376                &s->vlcs[0][dc_index]);
00377         return 0xffff;
00378     }
00379 
00380     if(code)
00381         return get_xbits(&s->gb, code);
00382     else
00383         return 0;
00384 }
00385 
00386 /* decode block and dequantize */
00387 static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
00388                         int component, int dc_index, int ac_index, int16_t *quant_matrix)
00389 {
00390     int code, i, j, level, val;
00391 
00392     /* DC coef */
00393     val = mjpeg_decode_dc(s, dc_index);
00394     if (val == 0xffff) {
00395         av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
00396         return -1;
00397     }
00398     val = val * quant_matrix[0] + s->last_dc[component];
00399     s->last_dc[component] = val;
00400     block[0] = val;
00401     /* AC coefs */
00402     i = 0;
00403     {OPEN_READER(re, &s->gb)
00404     for(;;) {
00405         UPDATE_CACHE(re, &s->gb);
00406         GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2)
00407 
00408         /* EOB */
00409         if (code == 0x10)
00410             break;
00411         i += ((unsigned)code) >> 4;
00412         if(code != 0x100){
00413             code &= 0xf;
00414             if(code > MIN_CACHE_BITS - 16){
00415                 UPDATE_CACHE(re, &s->gb)
00416             }
00417             {
00418                 int cache=GET_CACHE(re,&s->gb);
00419                 int sign=(~cache)>>31;
00420                 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
00421             }
00422 
00423             LAST_SKIP_BITS(re, &s->gb, code)
00424 
00425             if (i >= 63) {
00426                 if(i == 63){
00427                     j = s->scantable.permutated[63];
00428                     block[j] = level * quant_matrix[j];
00429                     break;
00430                 }
00431                 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
00432                 return -1;
00433             }
00434             j = s->scantable.permutated[i];
00435             block[j] = level * quant_matrix[j];
00436         }
00437     }
00438     CLOSE_READER(re, &s->gb)}
00439 
00440     return 0;
00441 }
00442 
00443 static int decode_dc_progressive(MJpegDecodeContext *s, DCTELEM *block, int component,
00444                                  int dc_index, int16_t *quant_matrix, int Al)
00445 {
00446     int val;
00447     s->dsp.clear_block(block);
00448     val = mjpeg_decode_dc(s, dc_index);
00449     if (val == 0xffff) {
00450         av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
00451         return -1;
00452     }
00453     val = (val * quant_matrix[0] << Al) + s->last_dc[component];
00454     s->last_dc[component] = val;
00455     block[0] = val;
00456     return 0;
00457 }
00458 
00459 /* decode block and dequantize - progressive JPEG version */
00460 static int decode_block_progressive(MJpegDecodeContext *s, DCTELEM *block, uint8_t *last_nnz,
00461                                     int ac_index, int16_t *quant_matrix,
00462                                     int ss, int se, int Al, int *EOBRUN)
00463 {
00464     int code, i, j, level, val, run;
00465 
00466     if(*EOBRUN){
00467         (*EOBRUN)--;
00468         return 0;
00469     }
00470     {OPEN_READER(re, &s->gb)
00471     for(i=ss;;i++) {
00472         UPDATE_CACHE(re, &s->gb);
00473         GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2)
00474         /* Progressive JPEG use AC coeffs from zero and this decoder sets offset 16 by default */
00475         code -= 16;
00476         if(code & 0xF) {
00477             i += ((unsigned) code) >> 4;
00478             code &= 0xf;
00479             if(code > MIN_CACHE_BITS - 16){
00480                 UPDATE_CACHE(re, &s->gb)
00481             }
00482             {
00483                 int cache=GET_CACHE(re,&s->gb);
00484                 int sign=(~cache)>>31;
00485                 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
00486             }
00487 
00488             LAST_SKIP_BITS(re, &s->gb, code)
00489 
00490             if (i >= se) {
00491                 if(i == se){
00492                     j = s->scantable.permutated[se];
00493                     block[j] = level * quant_matrix[j] << Al;
00494                     break;
00495                 }
00496                 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
00497                 return -1;
00498             }
00499             j = s->scantable.permutated[i];
00500             block[j] = level * quant_matrix[j] << Al;
00501         }else{
00502             run = ((unsigned) code) >> 4;
00503             if(run == 0xF){// ZRL - skip 15 coefficients
00504                 i += 15;
00505             }else{
00506                 val = run;
00507                 run = (1 << run);
00508                 UPDATE_CACHE(re, &s->gb);
00509                 run += (GET_CACHE(re, &s->gb) >> (32 - val)) & (run - 1);
00510                 if(val)
00511                     LAST_SKIP_BITS(re, &s->gb, val);
00512                 *EOBRUN = run - 1;
00513                 break;
00514             }
00515         }
00516     }
00517     CLOSE_READER(re, &s->gb)}
00518     if(i > *last_nnz)
00519         *last_nnz = i;
00520     return 0;
00521 }
00522 
00523 #define REFINE_BIT(j) {\
00524     UPDATE_CACHE(re, &s->gb);\
00525     sign = block[j]>>15;\
00526     block[j] += SHOW_UBITS(re, &s->gb, 1) * ((quant_matrix[j]^sign)-sign) << Al;\
00527     LAST_SKIP_BITS(re, &s->gb, 1);\
00528 }
00529 
00530 #define ZERO_RUN \
00531 for(;;i++) {\
00532     if(i > last) {\
00533         i += run;\
00534         if(i > se) {\
00535             av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);\
00536             return -1;\
00537         }\
00538         break;\
00539     }\
00540     j = s->scantable.permutated[i];\
00541     if(block[j])\
00542         REFINE_BIT(j)\
00543     else if(run-- == 0)\
00544         break;\
00545 }
00546 
00547 /* decode block and dequantize - progressive JPEG refinement pass */
00548 static int decode_block_refinement(MJpegDecodeContext *s, DCTELEM *block, uint8_t *last_nnz,
00549                         int ac_index, int16_t *quant_matrix,
00550                         int ss, int se, int Al, int *EOBRUN)
00551 {
00552     int code, i=ss, j, sign, val, run;
00553     int last = FFMIN(se, *last_nnz);
00554 
00555     OPEN_READER(re, &s->gb);
00556     if(*EOBRUN)
00557         (*EOBRUN)--;
00558     else {
00559         for(;;i++) {
00560             UPDATE_CACHE(re, &s->gb);
00561             GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2)
00562             /* Progressive JPEG use AC coeffs from zero and this decoder sets offset 16 by default */
00563             code -= 16;
00564             if(code & 0xF) {
00565                 run = ((unsigned) code) >> 4;
00566                 UPDATE_CACHE(re, &s->gb);
00567                 val = SHOW_UBITS(re, &s->gb, 1);
00568                 LAST_SKIP_BITS(re, &s->gb, 1);
00569                 ZERO_RUN;
00570                 j = s->scantable.permutated[i];
00571                 val--;
00572                 block[j] = ((quant_matrix[j]^val)-val) << Al;
00573                 if(i == se) {
00574                     if(i > *last_nnz)
00575                         *last_nnz = i;
00576                     CLOSE_READER(re, &s->gb)
00577                     return 0;
00578                 }
00579             }else{
00580                 run = ((unsigned) code) >> 4;
00581                 if(run == 0xF){
00582                     ZERO_RUN;
00583                 }else{
00584                     val = run;
00585                     run = (1 << run);
00586                     if(val) {
00587                         UPDATE_CACHE(re, &s->gb);
00588                         run += SHOW_UBITS(re, &s->gb, val);
00589                         LAST_SKIP_BITS(re, &s->gb, val);
00590                     }
00591                     *EOBRUN = run - 1;
00592                     break;
00593                 }
00594             }
00595         }
00596 
00597         if(i > *last_nnz)
00598             *last_nnz = i;
00599     }
00600 
00601     for(;i<=last;i++) {
00602         j = s->scantable.permutated[i];
00603         if(block[j])
00604             REFINE_BIT(j)
00605     }
00606     CLOSE_READER(re, &s->gb);
00607 
00608     return 0;
00609 }
00610 #undef REFINE_BIT
00611 #undef ZERO_RUN
00612 
00613 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor, int point_transform){
00614     int i, mb_x, mb_y;
00615     uint16_t buffer[32768][4];
00616     int left[3], top[3], topleft[3];
00617     const int linesize= s->linesize[0];
00618     const int mask= (1<<s->bits)-1;
00619 
00620     if((unsigned)s->mb_width > 32768) //dynamic alloc
00621         return -1;
00622 
00623     for(i=0; i<3; i++){
00624         buffer[0][i]= 1 << (s->bits + point_transform - 1);
00625     }
00626     for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
00627         const int modified_predictor= mb_y ? predictor : 1;
00628         uint8_t *ptr = s->picture.data[0] + (linesize * mb_y);
00629 
00630         if (s->interlaced && s->bottom_field)
00631             ptr += linesize >> 1;
00632 
00633         for(i=0; i<3; i++){
00634             top[i]= left[i]= topleft[i]= buffer[0][i];
00635         }
00636         for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
00637             if (s->restart_interval && !s->restart_count)
00638                 s->restart_count = s->restart_interval;
00639 
00640             for(i=0;i<3;i++) {
00641                 int pred;
00642 
00643                 topleft[i]= top[i];
00644                 top[i]= buffer[mb_x][i];
00645 
00646                 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
00647 
00648                 left[i]=
00649                 buffer[mb_x][i]= mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform));
00650             }
00651 
00652             if (s->restart_interval && !--s->restart_count) {
00653                 align_get_bits(&s->gb);
00654                 skip_bits(&s->gb, 16); /* skip RSTn */
00655             }
00656         }
00657 
00658         if(s->rct){
00659             for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
00660                 ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200)>>2);
00661                 ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1];
00662                 ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1];
00663             }
00664         }else if(s->pegasus_rct){
00665             for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
00666                 ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2])>>2);
00667                 ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1];
00668                 ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1];
00669             }
00670         }else{
00671             for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
00672                 ptr[4*mb_x+0] = buffer[mb_x][0];
00673                 ptr[4*mb_x+1] = buffer[mb_x][1];
00674                 ptr[4*mb_x+2] = buffer[mb_x][2];
00675             }
00676         }
00677     }
00678     return 0;
00679 }
00680 
00681 static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, int point_transform){
00682     int i, mb_x, mb_y;
00683     const int nb_components=3;
00684 
00685     for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
00686         for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
00687             if (s->restart_interval && !s->restart_count)
00688                 s->restart_count = s->restart_interval;
00689 
00690             if(mb_x==0 || mb_y==0 || s->interlaced){
00691                 for(i=0;i<nb_components;i++) {
00692                     uint8_t *ptr;
00693                     int n, h, v, x, y, c, j, linesize;
00694                     n = s->nb_blocks[i];
00695                     c = s->comp_index[i];
00696                     h = s->h_scount[i];
00697                     v = s->v_scount[i];
00698                     x = 0;
00699                     y = 0;
00700                     linesize= s->linesize[c];
00701 
00702                     for(j=0; j<n; j++) {
00703                         int pred;
00704 
00705                         ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
00706                         if(y==0 && mb_y==0){
00707                             if(x==0 && mb_x==0){
00708                                 pred= 128 << point_transform;
00709                             }else{
00710                                 pred= ptr[-1];
00711                             }
00712                         }else{
00713                             if(x==0 && mb_x==0){
00714                                 pred= ptr[-linesize];
00715                             }else{
00716                                 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
00717                             }
00718                         }
00719 
00720                         if (s->interlaced && s->bottom_field)
00721                             ptr += linesize >> 1;
00722                         *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
00723 
00724                         if (++x == h) {
00725                             x = 0;
00726                             y++;
00727                         }
00728                     }
00729                 }
00730             }else{
00731                 for(i=0;i<nb_components;i++) {
00732                     uint8_t *ptr;
00733                     int n, h, v, x, y, c, j, linesize;
00734                     n = s->nb_blocks[i];
00735                     c = s->comp_index[i];
00736                     h = s->h_scount[i];
00737                     v = s->v_scount[i];
00738                     x = 0;
00739                     y = 0;
00740                     linesize= s->linesize[c];
00741 
00742                     for(j=0; j<n; j++) {
00743                         int pred;
00744 
00745                         ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
00746                         PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
00747                         *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
00748                         if (++x == h) {
00749                             x = 0;
00750                             y++;
00751                         }
00752                     }
00753                 }
00754             }
00755             if (s->restart_interval && !--s->restart_count) {
00756                 align_get_bits(&s->gb);
00757                 skip_bits(&s->gb, 16); /* skip RSTn */
00758             }
00759         }
00760     }
00761     return 0;
00762 }
00763 
00764 static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah, int Al){
00765     int i, mb_x, mb_y;
00766     uint8_t* data[MAX_COMPONENTS];
00767     int linesize[MAX_COMPONENTS];
00768 
00769     for(i=0; i < nb_components; i++) {
00770         int c = s->comp_index[i];
00771         data[c] = s->picture.data[c];
00772         linesize[c]=s->linesize[c];
00773         s->coefs_finished[c] |= 1;
00774         if(s->avctx->codec->id==CODEC_ID_AMV) {
00775             //picture should be flipped upside-down for this codec
00776             assert(!(s->avctx->flags & CODEC_FLAG_EMU_EDGE));
00777             data[c] += (linesize[c] * (s->v_scount[i] * (8 * s->mb_height -((s->height/s->v_max)&7)) - 1 ));
00778             linesize[c] *= -1;
00779         }
00780     }
00781 
00782     for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
00783         for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
00784             if (s->restart_interval && !s->restart_count)
00785                 s->restart_count = s->restart_interval;
00786 
00787             if(get_bits_count(&s->gb)>s->gb.size_in_bits){
00788                 av_log(s->avctx, AV_LOG_ERROR, "overread %d\n", get_bits_count(&s->gb) - s->gb.size_in_bits);
00789                 return -1;
00790             }
00791             for(i=0;i<nb_components;i++) {
00792                 uint8_t *ptr;
00793                 int n, h, v, x, y, c, j;
00794                 n = s->nb_blocks[i];
00795                 c = s->comp_index[i];
00796                 h = s->h_scount[i];
00797                 v = s->v_scount[i];
00798                 x = 0;
00799                 y = 0;
00800                 for(j=0;j<n;j++) {
00801                     ptr = data[c] +
00802                         (((linesize[c] * (v * mb_y + y) * 8) +
00803                         (h * mb_x + x) * 8) >> s->avctx->lowres);
00804                     if(s->interlaced && s->bottom_field)
00805                         ptr += linesize[c] >> 1;
00806                     if(!s->progressive) {
00807                         s->dsp.clear_block(s->block);
00808                         if(decode_block(s, s->block, i,
00809                                      s->dc_index[i], s->ac_index[i],
00810                                      s->quant_matrixes[ s->quant_index[c] ]) < 0) {
00811                             av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
00812                             return -1;
00813                         }
00814                         s->dsp.idct_put(ptr, linesize[c], s->block);
00815                     } else {
00816                         int block_idx = s->block_stride[c] * (v * mb_y + y) + (h * mb_x + x);
00817                         DCTELEM *block = s->blocks[c][block_idx];
00818                         if(Ah)
00819                             block[0] += get_bits1(&s->gb) * s->quant_matrixes[ s->quant_index[c] ][0] << Al;
00820                         else if(decode_dc_progressive(s, block, i, s->dc_index[i], s->quant_matrixes[ s->quant_index[c] ], Al) < 0) {
00821                             av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
00822                             return -1;
00823                         }
00824                     }
00825 //                    av_log(s->avctx, AV_LOG_DEBUG, "mb: %d %d processed\n", mb_y, mb_x);
00826 //av_log(NULL, AV_LOG_DEBUG, "%d %d %d %d %d %d %d %d \n", mb_x, mb_y, x, y, c, s->bottom_field, (v * mb_y + y) * 8, (h * mb_x + x) * 8);
00827                     if (++x == h) {
00828                         x = 0;
00829                         y++;
00830                     }
00831                 }
00832             }
00833             /* (< 1350) buggy workaround for Spectralfan.mov, should be fixed */
00834             if (s->restart_interval && (s->restart_interval < 1350) &&
00835                 !--s->restart_count) {
00836                 align_get_bits(&s->gb);
00837                 skip_bits(&s->gb, 16); /* skip RSTn */
00838                 for (i=0; i<nb_components; i++) /* reset dc */
00839                     s->last_dc[i] = 1024;
00840             }
00841         }
00842     }
00843     return 0;
00844 }
00845 
00846 static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss, int se, int Ah, int Al){
00847     int mb_x, mb_y;
00848     int EOBRUN = 0;
00849     int c = s->comp_index[0];
00850     uint8_t* data = s->picture.data[c];
00851     int linesize = s->linesize[c];
00852     int last_scan = 0;
00853     int16_t *quant_matrix = s->quant_matrixes[ s->quant_index[c] ];
00854 
00855     if(!Al) {
00856         s->coefs_finished[c] |= (1LL<<(se+1))-(1LL<<ss);
00857         last_scan = !~s->coefs_finished[c];
00858     }
00859 
00860     if(s->interlaced && s->bottom_field)
00861         data += linesize >> 1;
00862 
00863     for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
00864         uint8_t *ptr = data + (mb_y*linesize*8 >> s->avctx->lowres);
00865         int block_idx = mb_y * s->block_stride[c];
00866         DCTELEM (*block)[64] = &s->blocks[c][block_idx];
00867         uint8_t *last_nnz = &s->last_nnz[c][block_idx];
00868         for(mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
00869             int ret;
00870             if(Ah)
00871                 ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0],
00872                                               quant_matrix, ss, se, Al, &EOBRUN);
00873             else
00874                 ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0],
00875                                                quant_matrix, ss, se, Al, &EOBRUN);
00876             if(ret < 0) {
00877                 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
00878                 return -1;
00879             }
00880             if(last_scan) {
00881                 s->dsp.idct_put(ptr, linesize, *block);
00882                 ptr += 8 >> s->avctx->lowres;
00883             }
00884         }
00885     }
00886     return 0;
00887 }
00888 
00889 int ff_mjpeg_decode_sos(MJpegDecodeContext *s)
00890 {
00891     int len, nb_components, i, h, v, predictor, point_transform;
00892     int vmax, hmax, index, id;
00893     const int block_size= s->lossless ? 1 : 8;
00894     int ilv, prev_shift;
00895 
00896     /* XXX: verify len field validity */
00897     len = get_bits(&s->gb, 16);
00898     nb_components = get_bits(&s->gb, 8);
00899     if (len != 6+2*nb_components)
00900     {
00901         av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len);
00902         return -1;
00903     }
00904     vmax = 0;
00905     hmax = 0;
00906     for(i=0;i<nb_components;i++) {
00907         id = get_bits(&s->gb, 8) - 1;
00908         av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
00909         /* find component index */
00910         for(index=0;index<s->nb_components;index++)
00911             if (id == s->component_id[index])
00912                 break;
00913         if (index == s->nb_components)
00914         {
00915             av_log(s->avctx, AV_LOG_ERROR, "decode_sos: index(%d) out of components\n", index);
00916             return -1;
00917         }
00918 
00919         s->comp_index[i] = index;
00920 
00921         s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
00922         s->h_scount[i] = s->h_count[index];
00923         s->v_scount[i] = s->v_count[index];
00924 
00925         s->dc_index[i] = get_bits(&s->gb, 4);
00926         s->ac_index[i] = get_bits(&s->gb, 4);
00927 
00928         if (s->dc_index[i] <  0 || s->ac_index[i] < 0 ||
00929             s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
00930             goto out_of_range;
00931 #if 0 //buggy
00932         switch(s->start_code)
00933         {
00934             case SOF0:
00935                 if (dc_index[i] > 1 || ac_index[i] > 1)
00936                     goto out_of_range;
00937                 break;
00938             case SOF1:
00939             case SOF2:
00940                 if (dc_index[i] > 3 || ac_index[i] > 3)
00941                     goto out_of_range;
00942                 break;
00943             case SOF3:
00944                 if (dc_index[i] > 3 || ac_index[i] != 0)
00945                     goto out_of_range;
00946                 break;
00947         }
00948 #endif
00949     }
00950 
00951     predictor= get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
00952     ilv= get_bits(&s->gb, 8);    /* JPEG Se / JPEG-LS ILV */
00953     prev_shift = get_bits(&s->gb, 4); /* Ah */
00954     point_transform= get_bits(&s->gb, 4); /* Al */
00955 
00956     for(i=0;i<nb_components;i++)
00957         s->last_dc[i] = 1024;
00958 
00959     if (nb_components > 1) {
00960         /* interleaved stream */
00961         s->mb_width  = (s->width  + s->h_max * block_size - 1) / (s->h_max * block_size);
00962         s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
00963     } else if(!s->ls) { /* skip this for JPEG-LS */
00964         h = s->h_max / s->h_scount[0];
00965         v = s->v_max / s->v_scount[0];
00966         s->mb_width  = (s->width  + h * block_size - 1) / (h * block_size);
00967         s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
00968         s->nb_blocks[0] = 1;
00969         s->h_scount[0] = 1;
00970         s->v_scount[0] = 1;
00971     }
00972 
00973     if(s->avctx->debug & FF_DEBUG_PICT_INFO)
00974         av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d %s\n", s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "",
00975                predictor, point_transform, ilv, s->bits,
00976                s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""));
00977 
00978 
00979     /* mjpeg-b can have padding bytes between sos and image data, skip them */
00980     for (i = s->mjpb_skiptosod; i > 0; i--)
00981         skip_bits(&s->gb, 8);
00982 
00983     if(s->lossless){
00984         if(CONFIG_JPEGLS_DECODER && s->ls){
00985 //            for(){
00986 //            reset_ls_coding_parameters(s, 0);
00987 
00988             if(ff_jpegls_decode_picture(s, predictor, point_transform, ilv) < 0)
00989                 return -1;
00990         }else{
00991             if(s->rgb){
00992                 if(ljpeg_decode_rgb_scan(s, predictor, point_transform) < 0)
00993                     return -1;
00994             }else{
00995                 if(ljpeg_decode_yuv_scan(s, predictor, point_transform) < 0)
00996                     return -1;
00997             }
00998         }
00999     }else{
01000         if(s->progressive && predictor) {
01001             if(mjpeg_decode_scan_progressive_ac(s, predictor, ilv, prev_shift, point_transform) < 0)
01002                 return -1;
01003         } else {
01004             if(mjpeg_decode_scan(s, nb_components, prev_shift, point_transform) < 0)
01005                 return -1;
01006         }
01007     }
01008     emms_c();
01009     return 0;
01010  out_of_range:
01011     av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
01012     return -1;
01013 }
01014 
01015 static int mjpeg_decode_dri(MJpegDecodeContext *s)
01016 {
01017     if (get_bits(&s->gb, 16) != 4)
01018         return -1;
01019     s->restart_interval = get_bits(&s->gb, 16);
01020     s->restart_count = 0;
01021     av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n", s->restart_interval);
01022 
01023     return 0;
01024 }
01025 
01026 static int mjpeg_decode_app(MJpegDecodeContext *s)
01027 {
01028     int len, id, i;
01029 
01030     len = get_bits(&s->gb, 16);
01031     if (len < 5)
01032         return -1;
01033     if(8*len + get_bits_count(&s->gb) > s->gb.size_in_bits)
01034         return -1;
01035 
01036     id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
01037     id = be2me_32(id);
01038     len -= 6;
01039 
01040     if(s->avctx->debug & FF_DEBUG_STARTCODE){
01041         av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id);
01042     }
01043 
01044     /* buggy AVID, it puts EOI only at every 10th frame */
01045     /* also this fourcc is used by non-avid files too, it holds some
01046        informations, but it's always present in AVID creates files */
01047     if (id == AV_RL32("AVI1"))
01048     {
01049         /* structure:
01050             4bytes      AVI1
01051             1bytes      polarity
01052             1bytes      always zero
01053             4bytes      field_size
01054             4bytes      field_size_less_padding
01055         */
01056             s->buggy_avid = 1;
01057 //        if (s->first_picture)
01058 //            printf("mjpeg: workarounding buggy AVID\n");
01059         i = get_bits(&s->gb, 8);
01060         if     (i==2) s->bottom_field= 1;
01061         else if(i==1) s->bottom_field= 0;
01062 #if 0
01063         skip_bits(&s->gb, 8);
01064         skip_bits(&s->gb, 32);
01065         skip_bits(&s->gb, 32);
01066         len -= 10;
01067 #endif
01068 //        if (s->interlace_polarity)
01069 //            printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity);
01070         goto out;
01071     }
01072 
01073 //    len -= 2;
01074 
01075     if (id == AV_RL32("JFIF"))
01076     {
01077         int t_w, t_h, v1, v2;
01078         skip_bits(&s->gb, 8); /* the trailing zero-byte */
01079         v1= get_bits(&s->gb, 8);
01080         v2= get_bits(&s->gb, 8);
01081         skip_bits(&s->gb, 8);
01082 
01083         s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 16);
01084         s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 16);
01085 
01086         if (s->avctx->debug & FF_DEBUG_PICT_INFO)
01087             av_log(s->avctx, AV_LOG_INFO, "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
01088                 v1, v2,
01089                 s->avctx->sample_aspect_ratio.num,
01090                 s->avctx->sample_aspect_ratio.den
01091             );
01092 
01093         t_w = get_bits(&s->gb, 8);
01094         t_h = get_bits(&s->gb, 8);
01095         if (t_w && t_h)
01096         {
01097             /* skip thumbnail */
01098             if (len-10-(t_w*t_h*3) > 0)
01099                 len -= t_w*t_h*3;
01100         }
01101         len -= 10;
01102         goto out;
01103     }
01104 
01105     if (id == AV_RL32("Adob") && (get_bits(&s->gb, 8) == 'e'))
01106     {
01107         if (s->avctx->debug & FF_DEBUG_PICT_INFO)
01108             av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n");
01109         skip_bits(&s->gb, 16); /* version */
01110         skip_bits(&s->gb, 16); /* flags0 */
01111         skip_bits(&s->gb, 16); /* flags1 */
01112         skip_bits(&s->gb, 8);  /* transform */
01113         len -= 7;
01114         goto out;
01115     }
01116 
01117     if (id == AV_RL32("LJIF")){
01118         if (s->avctx->debug & FF_DEBUG_PICT_INFO)
01119             av_log(s->avctx, AV_LOG_INFO, "Pegasus lossless jpeg header found\n");
01120         skip_bits(&s->gb, 16); /* version ? */
01121         skip_bits(&s->gb, 16); /* unknwon always 0? */
01122         skip_bits(&s->gb, 16); /* unknwon always 0? */
01123         skip_bits(&s->gb, 16); /* unknwon always 0? */
01124         switch( get_bits(&s->gb, 8)){
01125         case 1:
01126             s->rgb= 1;
01127             s->pegasus_rct=0;
01128             break;
01129         case 2:
01130             s->rgb= 1;
01131             s->pegasus_rct=1;
01132             break;
01133         default:
01134             av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n");
01135         }
01136         len -= 9;
01137         goto out;
01138     }
01139 
01140     /* Apple MJPEG-A */
01141     if ((s->start_code == APP1) && (len > (0x28 - 8)))
01142     {
01143         id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
01144         id = be2me_32(id);
01145         len -= 4;
01146         if (id == AV_RL32("mjpg")) /* Apple MJPEG-A */
01147         {
01148 #if 0
01149             skip_bits(&s->gb, 32); /* field size */
01150             skip_bits(&s->gb, 32); /* pad field size */
01151             skip_bits(&s->gb, 32); /* next off */
01152             skip_bits(&s->gb, 32); /* quant off */
01153             skip_bits(&s->gb, 32); /* huff off */
01154             skip_bits(&s->gb, 32); /* image off */
01155             skip_bits(&s->gb, 32); /* scan off */
01156             skip_bits(&s->gb, 32); /* data off */
01157 #endif
01158             if (s->avctx->debug & FF_DEBUG_PICT_INFO)
01159                 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
01160         }
01161     }
01162 
01163 out:
01164     /* slow but needed for extreme adobe jpegs */
01165     if (len < 0)
01166         av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error, decode_app parser read over the end\n");
01167     while(--len > 0)
01168         skip_bits(&s->gb, 8);
01169 
01170     return 0;
01171 }
01172 
01173 static int mjpeg_decode_com(MJpegDecodeContext *s)
01174 {
01175     int len = get_bits(&s->gb, 16);
01176     if (len >= 2 && 8*len - 16 + get_bits_count(&s->gb) <= s->gb.size_in_bits) {
01177         char *cbuf = av_malloc(len - 1);
01178         if (cbuf) {
01179             int i;
01180             for (i = 0; i < len - 2; i++)
01181                 cbuf[i] = get_bits(&s->gb, 8);
01182             if (i > 0 && cbuf[i-1] == '\n')
01183                 cbuf[i-1] = 0;
01184             else
01185                 cbuf[i] = 0;
01186 
01187             if(s->avctx->debug & FF_DEBUG_PICT_INFO)
01188                 av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf);
01189 
01190             /* buggy avid, it puts EOI only at every 10th frame */
01191             if (!strcmp(cbuf, "AVID"))
01192             {
01193                 s->buggy_avid = 1;
01194                 //        if (s->first_picture)
01195                 //            printf("mjpeg: workarounding buggy AVID\n");
01196             }
01197             else if(!strcmp(cbuf, "CS=ITU601")){
01198                 s->cs_itu601= 1;
01199             }
01200 
01201             av_free(cbuf);
01202         }
01203     }
01204 
01205     return 0;
01206 }
01207 
01208 #if 0
01209 static int valid_marker_list[] =
01210 {
01211         /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */
01212 /* 0 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01213 /* 1 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01214 /* 2 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01215 /* 3 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01216 /* 4 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01217 /* 5 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01218 /* 6 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01219 /* 7 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01220 /* 8 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01221 /* 9 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01222 /* a */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01223 /* b */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01224 /* c */    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
01225 /* d */    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
01226 /* e */    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
01227 /* f */    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
01228 }
01229 #endif
01230 
01231 /* return the 8 bit start code value and update the search
01232    state. Return -1 if no start code found */
01233 static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
01234 {
01235     const uint8_t *buf_ptr;
01236     unsigned int v, v2;
01237     int val;
01238 #ifdef DEBUG
01239     int skipped=0;
01240 #endif
01241 
01242     buf_ptr = *pbuf_ptr;
01243     while (buf_ptr < buf_end) {
01244         v = *buf_ptr++;
01245         v2 = *buf_ptr;
01246         if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
01247             val = *buf_ptr++;
01248             goto found;
01249         }
01250 #ifdef DEBUG
01251         skipped++;
01252 #endif
01253     }
01254     val = -1;
01255 found:
01256 #ifdef DEBUG
01257     av_log(NULL, AV_LOG_VERBOSE, "find_marker skipped %d bytes\n", skipped);
01258 #endif
01259     *pbuf_ptr = buf_ptr;
01260     return val;
01261 }
01262 
01263 int ff_mjpeg_decode_frame(AVCodecContext *avctx,
01264                               void *data, int *data_size,
01265                               const uint8_t *buf, int buf_size)
01266 {
01267     MJpegDecodeContext *s = avctx->priv_data;
01268     const uint8_t *buf_end, *buf_ptr;
01269     int start_code;
01270     AVFrame *picture = data;
01271 
01272     buf_ptr = buf;
01273     buf_end = buf + buf_size;
01274     while (buf_ptr < buf_end) {
01275         /* find start next marker */
01276         start_code = find_marker(&buf_ptr, buf_end);
01277         {
01278             /* EOF */
01279             if (start_code < 0) {
01280                 goto the_end;
01281             } else {
01282                 av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n", start_code, buf_end - buf_ptr);
01283 
01284                 if ((buf_end - buf_ptr) > s->buffer_size)
01285                 {
01286                     av_free(s->buffer);
01287                     s->buffer_size = buf_end-buf_ptr;
01288                     s->buffer = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE);
01289                     av_log(avctx, AV_LOG_DEBUG, "buffer too small, expanding to %d bytes\n",
01290                         s->buffer_size);
01291                 }
01292 
01293                 /* unescape buffer of SOS, use special treatment for JPEG-LS */
01294                 if (start_code == SOS && !s->ls)
01295                 {
01296                     const uint8_t *src = buf_ptr;
01297                     uint8_t *dst = s->buffer;
01298 
01299                     while (src<buf_end)
01300                     {
01301                         uint8_t x = *(src++);
01302 
01303                         *(dst++) = x;
01304                         if (avctx->codec_id != CODEC_ID_THP)
01305                         {
01306                             if (x == 0xff) {
01307                                 while (src < buf_end && x == 0xff)
01308                                     x = *(src++);
01309 
01310                                 if (x >= 0xd0 && x <= 0xd7)
01311                                     *(dst++) = x;
01312                                 else if (x)
01313                                     break;
01314                             }
01315                         }
01316                     }
01317                     init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8);
01318 
01319                     av_log(avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n",
01320                            (buf_end - buf_ptr) - (dst - s->buffer));
01321                 }
01322                 else if(start_code == SOS && s->ls){
01323                     const uint8_t *src = buf_ptr;
01324                     uint8_t *dst = s->buffer;
01325                     int bit_count = 0;
01326                     int t = 0, b = 0;
01327                     PutBitContext pb;
01328 
01329                     s->cur_scan++;
01330 
01331                     /* find marker */
01332                     while (src + t < buf_end){
01333                         uint8_t x = src[t++];
01334                         if (x == 0xff){
01335                             while((src + t < buf_end) && x == 0xff)
01336                                 x = src[t++];
01337                             if (x & 0x80) {
01338                                 t -= 2;
01339                                 break;
01340                             }
01341                         }
01342                     }
01343                     bit_count = t * 8;
01344 
01345                     init_put_bits(&pb, dst, t);
01346 
01347                     /* unescape bitstream */
01348                     while(b < t){
01349                         uint8_t x = src[b++];
01350                         put_bits(&pb, 8, x);
01351                         if(x == 0xFF){
01352                             x = src[b++];
01353                             put_bits(&pb, 7, x);
01354                             bit_count--;
01355                         }
01356                     }
01357                     flush_put_bits(&pb);
01358 
01359                     init_get_bits(&s->gb, dst, bit_count);
01360                 }
01361                 else
01362                     init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8);
01363 
01364                 s->start_code = start_code;
01365                 if(s->avctx->debug & FF_DEBUG_STARTCODE){
01366                     av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
01367                 }
01368 
01369                 /* process markers */
01370                 if (start_code >= 0xd0 && start_code <= 0xd7) {
01371                     av_log(avctx, AV_LOG_DEBUG, "restart marker: %d\n", start_code&0x0f);
01372                     /* APP fields */
01373                 } else if (start_code >= APP0 && start_code <= APP15) {
01374                     mjpeg_decode_app(s);
01375                     /* Comment */
01376                 } else if (start_code == COM){
01377                     mjpeg_decode_com(s);
01378                 }
01379 
01380                 switch(start_code) {
01381                 case SOI:
01382                     s->restart_interval = 0;
01383 
01384                     s->restart_count = 0;
01385                     /* nothing to do on SOI */
01386                     break;
01387                 case DQT:
01388                     ff_mjpeg_decode_dqt(s);
01389                     break;
01390                 case DHT:
01391                     if(ff_mjpeg_decode_dht(s) < 0){
01392                         av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
01393                         return -1;
01394                     }
01395                     break;
01396                 case SOF0:
01397                     s->lossless=0;
01398                     s->ls=0;
01399                     s->progressive=0;
01400                     if (ff_mjpeg_decode_sof(s) < 0)
01401                         return -1;
01402                     break;
01403                 case SOF2:
01404                     s->lossless=0;
01405                     s->ls=0;
01406                     s->progressive=1;
01407                     if (ff_mjpeg_decode_sof(s) < 0)
01408                         return -1;
01409                     break;
01410                 case SOF3:
01411                     s->lossless=1;
01412                     s->ls=0;
01413                     s->progressive=0;
01414                     if (ff_mjpeg_decode_sof(s) < 0)
01415                         return -1;
01416                     break;
01417                 case SOF48:
01418                     s->lossless=1;
01419                     s->ls=1;
01420                     s->progressive=0;
01421                     if (ff_mjpeg_decode_sof(s) < 0)
01422                         return -1;
01423                     break;
01424                 case LSE:
01425                     if (!CONFIG_JPEGLS_DECODER || ff_jpegls_decode_lse(s) < 0)
01426                         return -1;
01427                     break;
01428                 case EOI:
01429                     s->cur_scan = 0;
01430                     if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
01431                         break;
01432 eoi_parser:
01433                     {
01434                         if (s->interlaced) {
01435                             s->bottom_field ^= 1;
01436                             /* if not bottom field, do not output image yet */
01437                             if (s->bottom_field == !s->interlace_polarity)
01438                                 goto not_the_end;
01439                         }
01440                         *picture = s->picture;
01441                         *data_size = sizeof(AVFrame);
01442 
01443                         if(!s->lossless){
01444                             picture->quality= FFMAX3(s->qscale[0], s->qscale[1], s->qscale[2]);
01445                             picture->qstride= 0;
01446                             picture->qscale_table= s->qscale_table;
01447                             memset(picture->qscale_table, picture->quality, (s->width+15)/16);
01448                             if(avctx->debug & FF_DEBUG_QP)
01449                                 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
01450                             picture->quality*= FF_QP2LAMBDA;
01451                         }
01452 
01453                         goto the_end;
01454                     }
01455                     break;
01456                 case SOS:
01457                     ff_mjpeg_decode_sos(s);
01458                     /* buggy avid puts EOI every 10-20th frame */
01459                     /* if restart period is over process EOI */
01460                     if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
01461                         goto eoi_parser;
01462                     break;
01463                 case DRI:
01464                     mjpeg_decode_dri(s);
01465                     break;
01466                 case SOF1:
01467                 case SOF5:
01468                 case SOF6:
01469                 case SOF7:
01470                 case SOF9:
01471                 case SOF10:
01472                 case SOF11:
01473                 case SOF13:
01474                 case SOF14:
01475                 case SOF15:
01476                 case JPG:
01477                     av_log(avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\n", start_code);
01478                     break;
01479 //                default:
01480 //                    printf("mjpeg: unsupported marker (%x)\n", start_code);
01481 //                    break;
01482                 }
01483 
01484 not_the_end:
01485                 /* eof process start code */
01486                 buf_ptr += (get_bits_count(&s->gb)+7)/8;
01487                 av_log(avctx, AV_LOG_DEBUG, "marker parser used %d bytes (%d bits)\n",
01488                        (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb));
01489             }
01490         }
01491     }
01492 the_end:
01493     av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %td bytes\n", buf_end - buf_ptr);
01494 //    return buf_end - buf_ptr;
01495     return buf_ptr - buf;
01496 }
01497 
01498 av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
01499 {
01500     MJpegDecodeContext *s = avctx->priv_data;
01501     int i, j;
01502 
01503     av_free(s->buffer);
01504     av_free(s->qscale_table);
01505 
01506     for(i=0;i<2;i++) {
01507         for(j=0;j<4;j++)
01508             free_vlc(&s->vlcs[i][j]);
01509     }
01510     for(i=0; i<MAX_COMPONENTS; i++) {
01511         av_freep(&s->blocks[i]);
01512         av_freep(&s->last_nnz[i]);
01513     }
01514     return 0;
01515 }
01516 
01517 AVCodec mjpeg_decoder = {
01518     "mjpeg",
01519     CODEC_TYPE_VIDEO,
01520     CODEC_ID_MJPEG,
01521     sizeof(MJpegDecodeContext),
01522     ff_mjpeg_decode_init,
01523     NULL,
01524     ff_mjpeg_decode_end,
01525     ff_mjpeg_decode_frame,
01526     CODEC_CAP_DR1,
01527     NULL,
01528     .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
01529 };
01530 
01531 AVCodec thp_decoder = {
01532     "thp",
01533     CODEC_TYPE_VIDEO,
01534     CODEC_ID_THP,
01535     sizeof(MJpegDecodeContext),
01536     ff_mjpeg_decode_init,
01537     NULL,
01538     ff_mjpeg_decode_end,
01539     ff_mjpeg_decode_frame,
01540     CODEC_CAP_DR1,
01541     NULL,
01542     .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"),
01543 };

Generated on Sat Feb 16 2013 09:23:12 for ffmpeg by  doxygen 1.7.1