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