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