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