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