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