You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1643 lines
55KB

  1. /*
  2. * MJPEG decoder
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. * Copyright (c) 2003 Alex Beregszaszi
  5. * Copyright (c) 2003-2004 Michael Niedermayer
  6. *
  7. * Support for external huffman table, various fixes (AVID workaround),
  8. * aspecting, new decode_frame mechanism and apple mjpeg-b support
  9. * by Alex Beregszaszi
  10. *
  11. * This file is part of FFmpeg.
  12. *
  13. * FFmpeg is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2.1 of the License, or (at your option) any later version.
  17. *
  18. * FFmpeg is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with FFmpeg; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. /**
  28. * @file
  29. * MJPEG decoder.
  30. */
  31. //#define DEBUG
  32. #include <assert.h>
  33. #include "libavutil/imgutils.h"
  34. #include "libavutil/avassert.h"
  35. #include "libavutil/opt.h"
  36. #include "avcodec.h"
  37. #include "dsputil.h"
  38. #include "mjpeg.h"
  39. #include "mjpegdec.h"
  40. #include "jpeglsdec.h"
  41. static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table,
  42. int nb_codes, int use_static, int is_ac)
  43. {
  44. uint8_t huff_size[256];
  45. uint16_t huff_code[256];
  46. uint16_t huff_sym[256];
  47. int i;
  48. assert(nb_codes <= 256);
  49. memset(huff_size, 0, sizeof(huff_size));
  50. ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
  51. for(i=0; i<256; i++)
  52. huff_sym[i]= i + 16*is_ac;
  53. if(is_ac) huff_sym[0]= 16*256;
  54. return init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2, huff_sym, 2, 2, use_static);
  55. }
  56. static void build_basic_mjpeg_vlc(MJpegDecodeContext * s) {
  57. build_vlc(&s->vlcs[0][0], ff_mjpeg_bits_dc_luminance,
  58. ff_mjpeg_val_dc, 12, 0, 0);
  59. build_vlc(&s->vlcs[0][1], ff_mjpeg_bits_dc_chrominance,
  60. ff_mjpeg_val_dc, 12, 0, 0);
  61. build_vlc(&s->vlcs[1][0], ff_mjpeg_bits_ac_luminance,
  62. ff_mjpeg_val_ac_luminance, 251, 0, 1);
  63. build_vlc(&s->vlcs[1][1], ff_mjpeg_bits_ac_chrominance,
  64. ff_mjpeg_val_ac_chrominance, 251, 0, 1);
  65. build_vlc(&s->vlcs[2][0], ff_mjpeg_bits_ac_luminance,
  66. ff_mjpeg_val_ac_luminance, 251, 0, 0);
  67. build_vlc(&s->vlcs[2][1], ff_mjpeg_bits_ac_chrominance,
  68. ff_mjpeg_val_ac_chrominance, 251, 0, 0);
  69. }
  70. av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
  71. {
  72. MJpegDecodeContext *s = avctx->priv_data;
  73. if (!s->picture_ptr)
  74. s->picture_ptr = &s->picture;
  75. avcodec_get_frame_defaults(&s->picture);
  76. s->avctx = avctx;
  77. dsputil_init(&s->dsp, avctx);
  78. ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
  79. s->buffer_size = 0;
  80. s->buffer = NULL;
  81. s->start_code = -1;
  82. s->first_picture = 1;
  83. s->org_height = avctx->coded_height;
  84. avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
  85. build_basic_mjpeg_vlc(s);
  86. #if FF_API_MJPEG_GLOBAL_OPTS
  87. if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
  88. s->extern_huff = 1;
  89. #endif
  90. if (s->extern_huff)
  91. {
  92. av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n");
  93. init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8);
  94. if (ff_mjpeg_decode_dht(s)) {
  95. av_log(avctx, AV_LOG_ERROR, "mjpeg: error using external huffman table, switching back to internal\n");
  96. build_basic_mjpeg_vlc(s);
  97. }
  98. }
  99. if (avctx->extradata_size > 9 &&
  100. AV_RL32(avctx->extradata + 4) == MKTAG('f','i','e','l')) {
  101. if (avctx->extradata[9] == 6) { /* quicktime icefloe 019 */
  102. s->interlace_polarity = 1; /* bottom field first */
  103. av_log(avctx, AV_LOG_DEBUG, "mjpeg bottom field first\n");
  104. }
  105. }
  106. if (avctx->codec->id == CODEC_ID_AMV)
  107. s->flipped = 1;
  108. return 0;
  109. }
  110. /* quantize tables */
  111. int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
  112. {
  113. int len, index, i, j;
  114. len = get_bits(&s->gb, 16) - 2;
  115. while (len >= 65) {
  116. /* only 8 bit precision handled */
  117. if (get_bits(&s->gb, 4) != 0)
  118. {
  119. av_log(s->avctx, AV_LOG_ERROR, "dqt: 16bit precision\n");
  120. return -1;
  121. }
  122. index = get_bits(&s->gb, 4);
  123. if (index >= 4)
  124. return -1;
  125. av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index);
  126. /* read quant table */
  127. for(i=0;i<64;i++) {
  128. j = s->scantable.permutated[i];
  129. s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
  130. }
  131. //XXX FIXME finetune, and perhaps add dc too
  132. s->qscale[index]= FFMAX(
  133. s->quant_matrixes[index][s->scantable.permutated[1]],
  134. s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1;
  135. av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n", index, s->qscale[index]);
  136. len -= 65;
  137. }
  138. return 0;
  139. }
  140. /* decode huffman tables and build VLC decoders */
  141. int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
  142. {
  143. int len, index, i, class, n, v, code_max;
  144. uint8_t bits_table[17];
  145. uint8_t val_table[256];
  146. len = get_bits(&s->gb, 16) - 2;
  147. while (len > 0) {
  148. if (len < 17)
  149. return -1;
  150. class = get_bits(&s->gb, 4);
  151. if (class >= 2)
  152. return -1;
  153. index = get_bits(&s->gb, 4);
  154. if (index >= 4)
  155. return -1;
  156. n = 0;
  157. for(i=1;i<=16;i++) {
  158. bits_table[i] = get_bits(&s->gb, 8);
  159. n += bits_table[i];
  160. }
  161. len -= 17;
  162. if (len < n || n > 256)
  163. return -1;
  164. code_max = 0;
  165. for(i=0;i<n;i++) {
  166. v = get_bits(&s->gb, 8);
  167. if (v > code_max)
  168. code_max = v;
  169. val_table[i] = v;
  170. }
  171. len -= n;
  172. /* build VLC and flush previous vlc if present */
  173. free_vlc(&s->vlcs[class][index]);
  174. av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n",
  175. class, index, code_max + 1);
  176. if(build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1, 0, class > 0) < 0){
  177. return -1;
  178. }
  179. if(class>0){
  180. free_vlc(&s->vlcs[2][index]);
  181. if(build_vlc(&s->vlcs[2][index], bits_table, val_table, code_max + 1, 0, 0) < 0){
  182. return -1;
  183. }
  184. }
  185. }
  186. return 0;
  187. }
  188. int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
  189. {
  190. int len, nb_components, i, width, height, pix_fmt_id;
  191. s->cur_scan = 0;
  192. /* XXX: verify len field validity */
  193. len = get_bits(&s->gb, 16);
  194. s->bits= get_bits(&s->gb, 8);
  195. if(s->pegasus_rct) s->bits=9;
  196. if(s->bits==9 && !s->pegasus_rct) s->rct=1; //FIXME ugly
  197. if (s->bits != 8 && !s->lossless){
  198. av_log(s->avctx, AV_LOG_ERROR, "only 8 bits/component accepted\n");
  199. return -1;
  200. }
  201. if(s->lossless && s->avctx->lowres){
  202. av_log(s->avctx, AV_LOG_ERROR, "lowres is not possible with lossless jpeg\n");
  203. return -1;
  204. }
  205. height = get_bits(&s->gb, 16);
  206. width = get_bits(&s->gb, 16);
  207. //HACK for odd_height.mov
  208. if(s->interlaced && s->width == width && s->height == height + 1)
  209. height= s->height;
  210. av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height);
  211. if(av_image_check_size(width, height, 0, s->avctx))
  212. return -1;
  213. nb_components = get_bits(&s->gb, 8);
  214. if (nb_components <= 0 ||
  215. nb_components > MAX_COMPONENTS)
  216. return -1;
  217. if (s->ls && !(s->bits <= 8 || nb_components == 1)){
  218. av_log(s->avctx, AV_LOG_ERROR, "only <= 8 bits/component or 16-bit gray accepted for JPEG-LS\n");
  219. return -1;
  220. }
  221. s->nb_components = nb_components;
  222. s->h_max = 1;
  223. s->v_max = 1;
  224. for(i=0;i<nb_components;i++) {
  225. /* component id */
  226. s->component_id[i] = get_bits(&s->gb, 8) - 1;
  227. s->h_count[i] = get_bits(&s->gb, 4);
  228. s->v_count[i] = get_bits(&s->gb, 4);
  229. /* compute hmax and vmax (only used in interleaved case) */
  230. if (s->h_count[i] > s->h_max)
  231. s->h_max = s->h_count[i];
  232. if (s->v_count[i] > s->v_max)
  233. s->v_max = s->v_count[i];
  234. s->quant_index[i] = get_bits(&s->gb, 8);
  235. if (s->quant_index[i] >= 4)
  236. return -1;
  237. av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n", i, s->h_count[i],
  238. s->v_count[i], s->component_id[i], s->quant_index[i]);
  239. }
  240. if(s->ls && (s->h_max > 1 || s->v_max > 1)) {
  241. av_log(s->avctx, AV_LOG_ERROR, "Subsampling in JPEG-LS is not supported.\n");
  242. return -1;
  243. }
  244. if(s->v_max==1 && s->h_max==1 && s->lossless==1) s->rgb=1;
  245. /* if different size, realloc/alloc picture */
  246. /* XXX: also check h_count and v_count */
  247. if (width != s->width || height != s->height) {
  248. av_freep(&s->qscale_table);
  249. s->width = width;
  250. s->height = height;
  251. s->interlaced = 0;
  252. /* test interlaced mode */
  253. if (s->first_picture &&
  254. s->org_height != 0 &&
  255. s->height < ((s->org_height * 3) / 4)) {
  256. s->interlaced = 1;
  257. s->bottom_field = s->interlace_polarity;
  258. s->picture_ptr->interlaced_frame = 1;
  259. s->picture_ptr->top_field_first = !s->interlace_polarity;
  260. height *= 2;
  261. }
  262. avcodec_set_dimensions(s->avctx, width, height);
  263. s->qscale_table= av_mallocz((s->width+15)/16);
  264. s->first_picture = 0;
  265. }
  266. if(s->interlaced && (s->bottom_field == !s->interlace_polarity))
  267. return 0;
  268. /* XXX: not complete test ! */
  269. pix_fmt_id = (s->h_count[0] << 28) | (s->v_count[0] << 24) |
  270. (s->h_count[1] << 20) | (s->v_count[1] << 16) |
  271. (s->h_count[2] << 12) | (s->v_count[2] << 8) |
  272. (s->h_count[3] << 4) | s->v_count[3];
  273. av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id);
  274. //NOTE we do not allocate pictures large enough for the possible padding of h/v_count being 4
  275. if(!(pix_fmt_id & 0xD0D0D0D0))
  276. pix_fmt_id-= (pix_fmt_id & 0xF0F0F0F0)>>1;
  277. if(!(pix_fmt_id & 0x0D0D0D0D))
  278. pix_fmt_id-= (pix_fmt_id & 0x0F0F0F0F)>>1;
  279. switch(pix_fmt_id){
  280. case 0x11111100:
  281. if(s->rgb){
  282. s->avctx->pix_fmt = PIX_FMT_BGR24;
  283. }else{
  284. if(s->component_id[0] == 'Q' && s->component_id[1] == 'F' && s->component_id[2] == 'A'){
  285. s->avctx->pix_fmt = PIX_FMT_GBR24P;
  286. }else{
  287. s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV444P : PIX_FMT_YUVJ444P;
  288. s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
  289. }
  290. }
  291. assert(s->nb_components==3);
  292. break;
  293. case 0x11000000:
  294. s->avctx->pix_fmt = PIX_FMT_GRAY8;
  295. break;
  296. case 0x12111100:
  297. s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV440P : PIX_FMT_YUVJ440P;
  298. s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
  299. break;
  300. case 0x21111100:
  301. s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV422P : PIX_FMT_YUVJ422P;
  302. s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
  303. break;
  304. case 0x22111100:
  305. s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420P;
  306. s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
  307. break;
  308. default:
  309. av_log(s->avctx, AV_LOG_ERROR, "Unhandled pixel format 0x%x\n", pix_fmt_id);
  310. return -1;
  311. }
  312. if(s->ls){
  313. if(s->nb_components > 1)
  314. s->avctx->pix_fmt = PIX_FMT_RGB24;
  315. else if(s->bits <= 8)
  316. s->avctx->pix_fmt = PIX_FMT_GRAY8;
  317. else
  318. s->avctx->pix_fmt = PIX_FMT_GRAY16;
  319. }
  320. if(s->picture_ptr->data[0])
  321. s->avctx->release_buffer(s->avctx, s->picture_ptr);
  322. if(s->avctx->get_buffer(s->avctx, s->picture_ptr) < 0){
  323. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  324. return -1;
  325. }
  326. s->picture_ptr->pict_type= AV_PICTURE_TYPE_I;
  327. s->picture_ptr->key_frame= 1;
  328. s->got_picture = 1;
  329. for(i=0; i<3; i++){
  330. s->linesize[i]= s->picture_ptr->linesize[i] << s->interlaced;
  331. }
  332. // printf("%d %d %d %d %d %d\n", s->width, s->height, s->linesize[0], s->linesize[1], s->interlaced, s->avctx->height);
  333. if (len != (8+(3*nb_components)))
  334. {
  335. av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len);
  336. }
  337. /* totally blank picture as progressive JPEG will only add details to it */
  338. if(s->progressive){
  339. int bw = (width + s->h_max*8-1) / (s->h_max*8);
  340. int bh = (height + s->v_max*8-1) / (s->v_max*8);
  341. for(i=0; i<s->nb_components; i++) {
  342. int size = bw * bh * s->h_count[i] * s->v_count[i];
  343. av_freep(&s->blocks[i]);
  344. av_freep(&s->last_nnz[i]);
  345. s->blocks[i] = av_malloc(size * sizeof(**s->blocks));
  346. s->last_nnz[i] = av_mallocz(size * sizeof(**s->last_nnz));
  347. s->block_stride[i] = bw * s->h_count[i];
  348. }
  349. memset(s->coefs_finished, 0, sizeof(s->coefs_finished));
  350. }
  351. return 0;
  352. }
  353. static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
  354. {
  355. int code;
  356. code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
  357. if (code < 0)
  358. {
  359. av_log(s->avctx, AV_LOG_WARNING, "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index,
  360. &s->vlcs[0][dc_index]);
  361. return 0xffff;
  362. }
  363. if(code)
  364. return get_xbits(&s->gb, code);
  365. else
  366. return 0;
  367. }
  368. /* decode block and dequantize */
  369. static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
  370. int component, int dc_index, int ac_index, int16_t *quant_matrix)
  371. {
  372. int code, i, j, level, val;
  373. /* DC coef */
  374. val = mjpeg_decode_dc(s, dc_index);
  375. if (val == 0xffff) {
  376. av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
  377. return -1;
  378. }
  379. val = val * quant_matrix[0] + s->last_dc[component];
  380. s->last_dc[component] = val;
  381. block[0] = val;
  382. /* AC coefs */
  383. i = 0;
  384. {OPEN_READER(re, &s->gb);
  385. do {
  386. UPDATE_CACHE(re, &s->gb);
  387. GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2);
  388. i += ((unsigned)code) >> 4;
  389. code &= 0xf;
  390. if(code){
  391. if(code > MIN_CACHE_BITS - 16){
  392. UPDATE_CACHE(re, &s->gb);
  393. }
  394. {
  395. int cache=GET_CACHE(re,&s->gb);
  396. int sign=(~cache)>>31;
  397. level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
  398. }
  399. LAST_SKIP_BITS(re, &s->gb, code);
  400. if (i > 63) {
  401. av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
  402. return -1;
  403. }
  404. j = s->scantable.permutated[i];
  405. block[j] = level * quant_matrix[j];
  406. }
  407. }while(i<63);
  408. CLOSE_READER(re, &s->gb);}
  409. return 0;
  410. }
  411. static int decode_dc_progressive(MJpegDecodeContext *s, DCTELEM *block, int component,
  412. int dc_index, int16_t *quant_matrix, int Al)
  413. {
  414. int val;
  415. s->dsp.clear_block(block);
  416. val = mjpeg_decode_dc(s, dc_index);
  417. if (val == 0xffff) {
  418. av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
  419. return -1;
  420. }
  421. val = (val * quant_matrix[0] << Al) + s->last_dc[component];
  422. s->last_dc[component] = val;
  423. block[0] = val;
  424. return 0;
  425. }
  426. /* decode block and dequantize - progressive JPEG version */
  427. static int decode_block_progressive(MJpegDecodeContext *s, DCTELEM *block, uint8_t *last_nnz,
  428. int ac_index, int16_t *quant_matrix,
  429. int ss, int se, int Al, int *EOBRUN)
  430. {
  431. int code, i, j, level, val, run;
  432. if(*EOBRUN){
  433. (*EOBRUN)--;
  434. return 0;
  435. }
  436. {OPEN_READER(re, &s->gb);
  437. for(i=ss;;i++) {
  438. UPDATE_CACHE(re, &s->gb);
  439. GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
  440. run = ((unsigned) code) >> 4;
  441. code &= 0xF;
  442. if(code) {
  443. i += run;
  444. if(code > MIN_CACHE_BITS - 16){
  445. UPDATE_CACHE(re, &s->gb);
  446. }
  447. {
  448. int cache=GET_CACHE(re,&s->gb);
  449. int sign=(~cache)>>31;
  450. level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
  451. }
  452. LAST_SKIP_BITS(re, &s->gb, code);
  453. if (i >= se) {
  454. if(i == se){
  455. j = s->scantable.permutated[se];
  456. block[j] = level * quant_matrix[j] << Al;
  457. break;
  458. }
  459. av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
  460. return -1;
  461. }
  462. j = s->scantable.permutated[i];
  463. block[j] = level * quant_matrix[j] << Al;
  464. }else{
  465. if(run == 0xF){// ZRL - skip 15 coefficients
  466. i += 15;
  467. if (i >= se) {
  468. av_log(s->avctx, AV_LOG_ERROR, "ZRL overflow: %d\n", i);
  469. return -1;
  470. }
  471. }else{
  472. val = (1 << run);
  473. if(run){
  474. UPDATE_CACHE(re, &s->gb);
  475. val += NEG_USR32(GET_CACHE(re, &s->gb), run);
  476. LAST_SKIP_BITS(re, &s->gb, run);
  477. }
  478. *EOBRUN = val - 1;
  479. break;
  480. }
  481. }
  482. }
  483. CLOSE_READER(re, &s->gb);}
  484. if(i > *last_nnz)
  485. *last_nnz = i;
  486. return 0;
  487. }
  488. #define REFINE_BIT(j) {\
  489. UPDATE_CACHE(re, &s->gb);\
  490. sign = block[j]>>15;\
  491. block[j] += SHOW_UBITS(re, &s->gb, 1) * ((quant_matrix[j]^sign)-sign) << Al;\
  492. LAST_SKIP_BITS(re, &s->gb, 1);\
  493. }
  494. #define ZERO_RUN \
  495. for(;;i++) {\
  496. if(i > last) {\
  497. i += run;\
  498. if(i > se) {\
  499. av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);\
  500. return -1;\
  501. }\
  502. break;\
  503. }\
  504. j = s->scantable.permutated[i];\
  505. if(block[j])\
  506. REFINE_BIT(j)\
  507. else if(run-- == 0)\
  508. break;\
  509. }
  510. /* decode block and dequantize - progressive JPEG refinement pass */
  511. static int decode_block_refinement(MJpegDecodeContext *s, DCTELEM *block, uint8_t *last_nnz,
  512. int ac_index, int16_t *quant_matrix,
  513. int ss, int se, int Al, int *EOBRUN)
  514. {
  515. int code, i=ss, j, sign, val, run;
  516. int last = FFMIN(se, *last_nnz);
  517. OPEN_READER(re, &s->gb);
  518. if(*EOBRUN)
  519. (*EOBRUN)--;
  520. else {
  521. for(;;i++) {
  522. UPDATE_CACHE(re, &s->gb);
  523. GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
  524. if(code & 0xF) {
  525. run = ((unsigned) code) >> 4;
  526. UPDATE_CACHE(re, &s->gb);
  527. val = SHOW_UBITS(re, &s->gb, 1);
  528. LAST_SKIP_BITS(re, &s->gb, 1);
  529. ZERO_RUN;
  530. j = s->scantable.permutated[i];
  531. val--;
  532. block[j] = ((quant_matrix[j]^val)-val) << Al;
  533. if(i == se) {
  534. if(i > *last_nnz)
  535. *last_nnz = i;
  536. CLOSE_READER(re, &s->gb);
  537. return 0;
  538. }
  539. }else{
  540. run = ((unsigned) code) >> 4;
  541. if(run == 0xF){
  542. ZERO_RUN;
  543. }else{
  544. val = run;
  545. run = (1 << run);
  546. if(val) {
  547. UPDATE_CACHE(re, &s->gb);
  548. run += SHOW_UBITS(re, &s->gb, val);
  549. LAST_SKIP_BITS(re, &s->gb, val);
  550. }
  551. *EOBRUN = run - 1;
  552. break;
  553. }
  554. }
  555. }
  556. if(i > *last_nnz)
  557. *last_nnz = i;
  558. }
  559. for(;i<=last;i++) {
  560. j = s->scantable.permutated[i];
  561. if(block[j])
  562. REFINE_BIT(j)
  563. }
  564. CLOSE_READER(re, &s->gb);
  565. return 0;
  566. }
  567. #undef REFINE_BIT
  568. #undef ZERO_RUN
  569. static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor, int point_transform){
  570. int i, mb_x, mb_y;
  571. uint16_t (*buffer)[4];
  572. int left[3], top[3], topleft[3];
  573. const int linesize= s->linesize[0];
  574. const int mask= (1<<s->bits)-1;
  575. av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size, (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
  576. buffer= s->ljpeg_buffer;
  577. for(i=0; i<3; i++){
  578. buffer[0][i]= 1 << (s->bits + point_transform - 1);
  579. }
  580. for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
  581. const int modified_predictor= mb_y ? predictor : 1;
  582. uint8_t *ptr = s->picture.data[0] + (linesize * mb_y);
  583. if (s->interlaced && s->bottom_field)
  584. ptr += linesize >> 1;
  585. for(i=0; i<3; i++){
  586. top[i]= left[i]= topleft[i]= buffer[0][i];
  587. }
  588. for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
  589. if (s->restart_interval && !s->restart_count)
  590. s->restart_count = s->restart_interval;
  591. for(i=0;i<3;i++) {
  592. int pred;
  593. topleft[i]= top[i];
  594. top[i]= buffer[mb_x][i];
  595. PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
  596. left[i]=
  597. buffer[mb_x][i]= mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform));
  598. }
  599. if (s->restart_interval && !--s->restart_count) {
  600. align_get_bits(&s->gb);
  601. skip_bits(&s->gb, 16); /* skip RSTn */
  602. }
  603. }
  604. if(s->rct){
  605. for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
  606. ptr[3*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200)>>2);
  607. ptr[3*mb_x+0] = buffer[mb_x][1] + ptr[3*mb_x+1];
  608. ptr[3*mb_x+2] = buffer[mb_x][2] + ptr[3*mb_x+1];
  609. }
  610. }else if(s->pegasus_rct){
  611. for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
  612. ptr[3*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2])>>2);
  613. ptr[3*mb_x+0] = buffer[mb_x][1] + ptr[3*mb_x+1];
  614. ptr[3*mb_x+2] = buffer[mb_x][2] + ptr[3*mb_x+1];
  615. }
  616. }else{
  617. for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
  618. ptr[3*mb_x+0] = buffer[mb_x][2];
  619. ptr[3*mb_x+1] = buffer[mb_x][1];
  620. ptr[3*mb_x+2] = buffer[mb_x][0];
  621. }
  622. }
  623. }
  624. return 0;
  625. }
  626. static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, int point_transform){
  627. int i, mb_x, mb_y;
  628. const int nb_components=3;
  629. for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
  630. for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
  631. if (s->restart_interval && !s->restart_count)
  632. s->restart_count = s->restart_interval;
  633. if(mb_x==0 || mb_y==0 || s->interlaced){
  634. for(i=0;i<nb_components;i++) {
  635. uint8_t *ptr;
  636. int n, h, v, x, y, c, j, linesize;
  637. n = s->nb_blocks[i];
  638. c = s->comp_index[i];
  639. h = s->h_scount[i];
  640. v = s->v_scount[i];
  641. x = 0;
  642. y = 0;
  643. linesize= s->linesize[c];
  644. for(j=0; j<n; j++) {
  645. int pred;
  646. ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  647. if(y==0 && mb_y==0){
  648. if(x==0 && mb_x==0){
  649. pred= 128 << point_transform;
  650. }else{
  651. pred= ptr[-1];
  652. }
  653. }else{
  654. if(x==0 && mb_x==0){
  655. pred= ptr[-linesize];
  656. }else{
  657. PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
  658. }
  659. }
  660. if (s->interlaced && s->bottom_field)
  661. ptr += linesize >> 1;
  662. *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
  663. if (++x == h) {
  664. x = 0;
  665. y++;
  666. }
  667. }
  668. }
  669. }else{
  670. for(i=0;i<nb_components;i++) {
  671. uint8_t *ptr;
  672. int n, h, v, x, y, c, j, linesize;
  673. n = s->nb_blocks[i];
  674. c = s->comp_index[i];
  675. h = s->h_scount[i];
  676. v = s->v_scount[i];
  677. x = 0;
  678. y = 0;
  679. linesize= s->linesize[c];
  680. for(j=0; j<n; j++) {
  681. int pred;
  682. ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  683. PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
  684. *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
  685. if (++x == h) {
  686. x = 0;
  687. y++;
  688. }
  689. }
  690. }
  691. }
  692. if (s->restart_interval && !--s->restart_count) {
  693. align_get_bits(&s->gb);
  694. skip_bits(&s->gb, 16); /* skip RSTn */
  695. }
  696. }
  697. }
  698. return 0;
  699. }
  700. static av_always_inline void mjpeg_copy_block(uint8_t *dst, const uint8_t *src,
  701. int linesize, int lowres)
  702. {
  703. switch (lowres) {
  704. case 0: copy_block8(dst, src, linesize, linesize, 8);
  705. break;
  706. case 1: copy_block4(dst, src, linesize, linesize, 4);
  707. break;
  708. case 2: copy_block2(dst, src, linesize, linesize, 2);
  709. break;
  710. case 3: *dst = *src;
  711. break;
  712. }
  713. }
  714. static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah, int Al,
  715. const uint8_t *mb_bitmask, const AVFrame *reference){
  716. int i, mb_x, mb_y;
  717. uint8_t* data[MAX_COMPONENTS];
  718. const uint8_t *reference_data[MAX_COMPONENTS];
  719. int linesize[MAX_COMPONENTS];
  720. GetBitContext mb_bitmask_gb;
  721. if (mb_bitmask) {
  722. init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width*s->mb_height);
  723. }
  724. if(s->flipped && s->avctx->flags & CODEC_FLAG_EMU_EDGE) {
  725. av_log(s->avctx, AV_LOG_ERROR, "Can not flip image with CODEC_FLAG_EMU_EDGE set!\n");
  726. s->flipped = 0;
  727. }
  728. for(i=0; i < nb_components; i++) {
  729. int c = s->comp_index[i];
  730. data[c] = s->picture_ptr->data[c];
  731. reference_data[c] = reference ? reference->data[c] : NULL;
  732. linesize[c]=s->linesize[c];
  733. s->coefs_finished[c] |= 1;
  734. if(s->flipped) {
  735. //picture should be flipped upside-down for this codec
  736. int offset = (linesize[c] * (s->v_scount[i] * (8 * s->mb_height -((s->height/s->v_max)&7)) - 1 ));
  737. data[c] += offset;
  738. reference_data[c] += offset;
  739. linesize[c] *= -1;
  740. }
  741. }
  742. for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
  743. for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
  744. const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
  745. if (s->restart_interval && !s->restart_count)
  746. s->restart_count = s->restart_interval;
  747. if(get_bits_count(&s->gb)>s->gb.size_in_bits){
  748. av_log(s->avctx, AV_LOG_ERROR, "overread %d\n", get_bits_count(&s->gb) - s->gb.size_in_bits);
  749. return -1;
  750. }
  751. for(i=0;i<nb_components;i++) {
  752. uint8_t *ptr;
  753. int n, h, v, x, y, c, j;
  754. int block_offset;
  755. n = s->nb_blocks[i];
  756. c = s->comp_index[i];
  757. h = s->h_scount[i];
  758. v = s->v_scount[i];
  759. x = 0;
  760. y = 0;
  761. for(j=0;j<n;j++) {
  762. block_offset = (((linesize[c] * (v * mb_y + y) * 8) +
  763. (h * mb_x + x) * 8) >> s->avctx->lowres);
  764. if(s->interlaced && s->bottom_field)
  765. block_offset += linesize[c] >> 1;
  766. ptr = data[c] + block_offset;
  767. if(!s->progressive) {
  768. if (copy_mb) {
  769. mjpeg_copy_block(ptr, reference_data[c] + block_offset, linesize[c], s->avctx->lowres);
  770. } else {
  771. s->dsp.clear_block(s->block);
  772. if(decode_block(s, s->block, i,
  773. s->dc_index[i], s->ac_index[i],
  774. s->quant_matrixes[ s->quant_index[c] ]) < 0) {
  775. av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
  776. return -1;
  777. }
  778. s->dsp.idct_put(ptr, linesize[c], s->block);
  779. }
  780. } else {
  781. int block_idx = s->block_stride[c] * (v * mb_y + y) + (h * mb_x + x);
  782. DCTELEM *block = s->blocks[c][block_idx];
  783. if(Ah)
  784. block[0] += get_bits1(&s->gb) * s->quant_matrixes[ s->quant_index[c] ][0] << Al;
  785. else if(decode_dc_progressive(s, block, i, s->dc_index[i], s->quant_matrixes[ s->quant_index[c] ], Al) < 0) {
  786. av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
  787. return -1;
  788. }
  789. }
  790. // av_log(s->avctx, AV_LOG_DEBUG, "mb: %d %d processed\n", mb_y, mb_x);
  791. //av_log(NULL, AV_LOG_DEBUG, "%d %d %d %d %d %d %d %d \n", mb_x, mb_y, x, y, c, s->bottom_field, (v * mb_y + y) * 8, (h * mb_x + x) * 8);
  792. if (++x == h) {
  793. x = 0;
  794. y++;
  795. }
  796. }
  797. }
  798. if (s->restart_interval) --s->restart_count;
  799. i= 8+((-get_bits_count(&s->gb))&7);
  800. if (s->restart_interval && show_bits(&s->gb, i) == (1<<i)-1){ /* skip RSTn */
  801. int pos= get_bits_count(&s->gb);
  802. align_get_bits(&s->gb);
  803. while(get_bits_count(&s->gb) < s->gb.size_in_bits && show_bits(&s->gb, 8) == 0xFF)
  804. skip_bits(&s->gb, 8);
  805. if(get_bits_count(&s->gb) < s->gb.size_in_bits && (get_bits(&s->gb, 8)&0xF8) == 0xD0){
  806. for (i=0; i<nb_components; i++) /* reset dc */
  807. s->last_dc[i] = 1024;
  808. }else{
  809. skip_bits_long(&s->gb, pos - get_bits_count(&s->gb));
  810. }
  811. }
  812. }
  813. }
  814. return 0;
  815. }
  816. static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss, int se, int Ah, int Al){
  817. int mb_x, mb_y;
  818. int EOBRUN = 0;
  819. int c = s->comp_index[0];
  820. uint8_t* data = s->picture.data[c];
  821. int linesize = s->linesize[c];
  822. int last_scan = 0;
  823. int16_t *quant_matrix = s->quant_matrixes[ s->quant_index[c] ];
  824. if(!Al) {
  825. s->coefs_finished[c] |= (1LL<<(se+1))-(1LL<<ss);
  826. last_scan = !~s->coefs_finished[c];
  827. }
  828. if(s->interlaced && s->bottom_field)
  829. data += linesize >> 1;
  830. for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
  831. uint8_t *ptr = data + (mb_y*linesize*8 >> s->avctx->lowres);
  832. int block_idx = mb_y * s->block_stride[c];
  833. DCTELEM (*block)[64] = &s->blocks[c][block_idx];
  834. uint8_t *last_nnz = &s->last_nnz[c][block_idx];
  835. for(mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
  836. int ret;
  837. if(Ah)
  838. ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0],
  839. quant_matrix, ss, se, Al, &EOBRUN);
  840. else
  841. ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0],
  842. quant_matrix, ss, se, Al, &EOBRUN);
  843. if(ret < 0) {
  844. av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
  845. return -1;
  846. }
  847. if(last_scan) {
  848. s->dsp.idct_put(ptr, linesize, *block);
  849. ptr += 8 >> s->avctx->lowres;
  850. }
  851. }
  852. }
  853. return 0;
  854. }
  855. int ff_mjpeg_decode_sos(MJpegDecodeContext *s,
  856. const uint8_t *mb_bitmask, const AVFrame *reference)
  857. {
  858. int len, nb_components, i, h, v, predictor, point_transform;
  859. int index, id;
  860. const int block_size= s->lossless ? 1 : 8;
  861. int ilv, prev_shift;
  862. /* XXX: verify len field validity */
  863. len = get_bits(&s->gb, 16);
  864. nb_components = get_bits(&s->gb, 8);
  865. if (nb_components == 0 || nb_components > MAX_COMPONENTS){
  866. av_log(s->avctx, AV_LOG_ERROR, "decode_sos: nb_components (%d) unsupported\n", nb_components);
  867. return -1;
  868. }
  869. if (len != 6+2*nb_components)
  870. {
  871. av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len);
  872. return -1;
  873. }
  874. for(i=0;i<nb_components;i++) {
  875. id = get_bits(&s->gb, 8) - 1;
  876. av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
  877. /* find component index */
  878. for(index=0;index<s->nb_components;index++)
  879. if (id == s->component_id[index])
  880. break;
  881. if (index == s->nb_components)
  882. {
  883. av_log(s->avctx, AV_LOG_ERROR, "decode_sos: index(%d) out of components\n", index);
  884. return -1;
  885. }
  886. /* Metasoft MJPEG codec has Cb and Cr swapped */
  887. if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J')
  888. && nb_components == 3 && s->nb_components == 3 && i)
  889. index = 3 - i;
  890. if(nb_components == 3 && s->nb_components == 3 && s->avctx->pix_fmt == PIX_FMT_GBR24P)
  891. index = (i+2)%3;
  892. s->comp_index[i] = index;
  893. s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
  894. s->h_scount[i] = s->h_count[index];
  895. s->v_scount[i] = s->v_count[index];
  896. s->dc_index[i] = get_bits(&s->gb, 4);
  897. s->ac_index[i] = get_bits(&s->gb, 4);
  898. if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
  899. s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
  900. goto out_of_range;
  901. if (!s->vlcs[0][s->dc_index[i]].table || !s->vlcs[1][s->ac_index[i]].table)
  902. goto out_of_range;
  903. }
  904. predictor= get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
  905. ilv= get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */
  906. if(s->avctx->codec_tag != AV_RL32("CJPG")){
  907. prev_shift = get_bits(&s->gb, 4); /* Ah */
  908. point_transform= get_bits(&s->gb, 4); /* Al */
  909. }else
  910. prev_shift= point_transform= 0;
  911. for(i=0;i<nb_components;i++)
  912. s->last_dc[i] = 1024;
  913. if (nb_components > 1) {
  914. /* interleaved stream */
  915. s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
  916. s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
  917. } else if(!s->ls) { /* skip this for JPEG-LS */
  918. h = s->h_max / s->h_scount[0];
  919. v = s->v_max / s->v_scount[0];
  920. s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
  921. s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
  922. s->nb_blocks[0] = 1;
  923. s->h_scount[0] = 1;
  924. s->v_scount[0] = 1;
  925. }
  926. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  927. av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d skip:%d %s comp:%d\n", s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "",
  928. predictor, point_transform, ilv, s->bits, s->mjpb_skiptosod,
  929. s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""), nb_components);
  930. /* mjpeg-b can have padding bytes between sos and image data, skip them */
  931. for (i = s->mjpb_skiptosod; i > 0; i--)
  932. skip_bits(&s->gb, 8);
  933. if(s->lossless){
  934. av_assert0(s->picture_ptr == &s->picture);
  935. if(CONFIG_JPEGLS_DECODER && s->ls){
  936. // for(){
  937. // reset_ls_coding_parameters(s, 0);
  938. if(ff_jpegls_decode_picture(s, predictor, point_transform, ilv) < 0)
  939. return -1;
  940. }else{
  941. if(s->rgb){
  942. if(ljpeg_decode_rgb_scan(s, predictor, point_transform) < 0)
  943. return -1;
  944. }else{
  945. if(ljpeg_decode_yuv_scan(s, predictor, point_transform) < 0)
  946. return -1;
  947. }
  948. }
  949. }else{
  950. if(s->progressive && predictor) {
  951. av_assert0(s->picture_ptr == &s->picture);
  952. if(mjpeg_decode_scan_progressive_ac(s, predictor, ilv, prev_shift, point_transform) < 0)
  953. return -1;
  954. } else {
  955. if(mjpeg_decode_scan(s, nb_components, prev_shift, point_transform,
  956. mb_bitmask, reference) < 0)
  957. return -1;
  958. }
  959. }
  960. emms_c();
  961. return 0;
  962. out_of_range:
  963. av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
  964. return -1;
  965. }
  966. static int mjpeg_decode_dri(MJpegDecodeContext *s)
  967. {
  968. if (get_bits(&s->gb, 16) != 4)
  969. return -1;
  970. s->restart_interval = get_bits(&s->gb, 16);
  971. s->restart_count = 0;
  972. av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n", s->restart_interval);
  973. return 0;
  974. }
  975. static int mjpeg_decode_app(MJpegDecodeContext *s)
  976. {
  977. int len, id, i;
  978. len = get_bits(&s->gb, 16);
  979. if (len < 5)
  980. return -1;
  981. if(8*len + get_bits_count(&s->gb) > s->gb.size_in_bits)
  982. return -1;
  983. id = get_bits_long(&s->gb, 32);
  984. id = av_be2ne32(id);
  985. len -= 6;
  986. if(s->avctx->debug & FF_DEBUG_STARTCODE){
  987. av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id);
  988. }
  989. /* buggy AVID, it puts EOI only at every 10th frame */
  990. /* also this fourcc is used by non-avid files too, it holds some
  991. informations, but it's always present in AVID creates files */
  992. if (id == AV_RL32("AVI1"))
  993. {
  994. /* structure:
  995. 4bytes AVI1
  996. 1bytes polarity
  997. 1bytes always zero
  998. 4bytes field_size
  999. 4bytes field_size_less_padding
  1000. */
  1001. s->buggy_avid = 1;
  1002. // if (s->first_picture)
  1003. // printf("mjpeg: workarounding buggy AVID\n");
  1004. i = get_bits(&s->gb, 8); len--;
  1005. av_log(s->avctx, AV_LOG_DEBUG, "polarity %d\n", i);
  1006. #if 0
  1007. skip_bits(&s->gb, 8);
  1008. skip_bits(&s->gb, 32);
  1009. skip_bits(&s->gb, 32);
  1010. len -= 10;
  1011. #endif
  1012. // if (s->interlace_polarity)
  1013. // printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity);
  1014. goto out;
  1015. }
  1016. // len -= 2;
  1017. if (id == AV_RL32("JFIF"))
  1018. {
  1019. int t_w, t_h, v1, v2;
  1020. skip_bits(&s->gb, 8); /* the trailing zero-byte */
  1021. v1= get_bits(&s->gb, 8);
  1022. v2= get_bits(&s->gb, 8);
  1023. skip_bits(&s->gb, 8);
  1024. s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 16);
  1025. s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 16);
  1026. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1027. av_log(s->avctx, AV_LOG_INFO, "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
  1028. v1, v2,
  1029. s->avctx->sample_aspect_ratio.num,
  1030. s->avctx->sample_aspect_ratio.den
  1031. );
  1032. t_w = get_bits(&s->gb, 8);
  1033. t_h = get_bits(&s->gb, 8);
  1034. if (t_w && t_h)
  1035. {
  1036. /* skip thumbnail */
  1037. if (len-10-(t_w*t_h*3) > 0)
  1038. len -= t_w*t_h*3;
  1039. }
  1040. len -= 10;
  1041. goto out;
  1042. }
  1043. if (id == AV_RL32("Adob") && (get_bits(&s->gb, 8) == 'e'))
  1044. {
  1045. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1046. av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n");
  1047. skip_bits(&s->gb, 16); /* version */
  1048. skip_bits(&s->gb, 16); /* flags0 */
  1049. skip_bits(&s->gb, 16); /* flags1 */
  1050. skip_bits(&s->gb, 8); /* transform */
  1051. len -= 7;
  1052. goto out;
  1053. }
  1054. if (id == AV_RL32("LJIF")){
  1055. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1056. av_log(s->avctx, AV_LOG_INFO, "Pegasus lossless jpeg header found\n");
  1057. skip_bits(&s->gb, 16); /* version ? */
  1058. skip_bits(&s->gb, 16); /* unknwon always 0? */
  1059. skip_bits(&s->gb, 16); /* unknwon always 0? */
  1060. skip_bits(&s->gb, 16); /* unknwon always 0? */
  1061. switch( get_bits(&s->gb, 8)){
  1062. case 1:
  1063. s->rgb= 1;
  1064. s->pegasus_rct=0;
  1065. break;
  1066. case 2:
  1067. s->rgb= 1;
  1068. s->pegasus_rct=1;
  1069. break;
  1070. default:
  1071. av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n");
  1072. }
  1073. len -= 9;
  1074. goto out;
  1075. }
  1076. /* Apple MJPEG-A */
  1077. if ((s->start_code == APP1) && (len > (0x28 - 8)))
  1078. {
  1079. id = get_bits_long(&s->gb, 32);
  1080. id = av_be2ne32(id);
  1081. len -= 4;
  1082. if (id == AV_RL32("mjpg")) /* Apple MJPEG-A */
  1083. {
  1084. #if 0
  1085. skip_bits(&s->gb, 32); /* field size */
  1086. skip_bits(&s->gb, 32); /* pad field size */
  1087. skip_bits(&s->gb, 32); /* next off */
  1088. skip_bits(&s->gb, 32); /* quant off */
  1089. skip_bits(&s->gb, 32); /* huff off */
  1090. skip_bits(&s->gb, 32); /* image off */
  1091. skip_bits(&s->gb, 32); /* scan off */
  1092. skip_bits(&s->gb, 32); /* data off */
  1093. #endif
  1094. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1095. av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
  1096. }
  1097. }
  1098. out:
  1099. /* slow but needed for extreme adobe jpegs */
  1100. if (len < 0)
  1101. av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error, decode_app parser read over the end\n");
  1102. while(--len > 0)
  1103. skip_bits(&s->gb, 8);
  1104. return 0;
  1105. }
  1106. static int mjpeg_decode_com(MJpegDecodeContext *s)
  1107. {
  1108. int len = get_bits(&s->gb, 16);
  1109. if (len >= 2 && 8*len - 16 + get_bits_count(&s->gb) <= s->gb.size_in_bits) {
  1110. char *cbuf = av_malloc(len - 1);
  1111. if (cbuf) {
  1112. int i;
  1113. for (i = 0; i < len - 2; i++)
  1114. cbuf[i] = get_bits(&s->gb, 8);
  1115. if (i > 0 && cbuf[i-1] == '\n')
  1116. cbuf[i-1] = 0;
  1117. else
  1118. cbuf[i] = 0;
  1119. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1120. av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf);
  1121. /* buggy avid, it puts EOI only at every 10th frame */
  1122. if (!strcmp(cbuf, "AVID"))
  1123. {
  1124. s->buggy_avid = 1;
  1125. // if (s->first_picture)
  1126. // printf("mjpeg: workarounding buggy AVID\n");
  1127. }
  1128. else if(!strcmp(cbuf, "CS=ITU601")){
  1129. s->cs_itu601= 1;
  1130. }
  1131. else if((len > 20 && !strncmp(cbuf, "Intel(R) JPEG Library", 21)) ||
  1132. (len > 19 && !strncmp(cbuf, "Metasoft MJPEG Codec", 20))){
  1133. s->flipped = 1;
  1134. }
  1135. av_free(cbuf);
  1136. }
  1137. }
  1138. return 0;
  1139. }
  1140. /* return the 8 bit start code value and update the search
  1141. state. Return -1 if no start code found */
  1142. static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
  1143. {
  1144. const uint8_t *buf_ptr;
  1145. unsigned int v, v2;
  1146. int val;
  1147. int skipped=0;
  1148. buf_ptr = *pbuf_ptr;
  1149. while (buf_ptr < buf_end) {
  1150. v = *buf_ptr++;
  1151. v2 = *buf_ptr;
  1152. if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
  1153. val = *buf_ptr++;
  1154. goto found;
  1155. }
  1156. skipped++;
  1157. }
  1158. val = -1;
  1159. found:
  1160. av_dlog(NULL, "find_marker skipped %d bytes\n", skipped);
  1161. *pbuf_ptr = buf_ptr;
  1162. return val;
  1163. }
  1164. int ff_mjpeg_find_marker(MJpegDecodeContext *s,
  1165. const uint8_t **buf_ptr, const uint8_t *buf_end,
  1166. const uint8_t **unescaped_buf_ptr, int *unescaped_buf_size)
  1167. {
  1168. int start_code;
  1169. start_code = find_marker(buf_ptr, buf_end);
  1170. if ((buf_end - *buf_ptr) > s->buffer_size)
  1171. {
  1172. av_free(s->buffer);
  1173. s->buffer_size = buf_end - *buf_ptr;
  1174. s->buffer = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE);
  1175. av_log(s->avctx, AV_LOG_DEBUG, "buffer too small, expanding to %d bytes\n",
  1176. s->buffer_size);
  1177. }
  1178. /* unescape buffer of SOS, use special treatment for JPEG-LS */
  1179. if (start_code == SOS && !s->ls)
  1180. {
  1181. const uint8_t *src = *buf_ptr;
  1182. uint8_t *dst = s->buffer;
  1183. while (src<buf_end)
  1184. {
  1185. uint8_t x = *(src++);
  1186. *(dst++) = x;
  1187. if (s->avctx->codec_id != CODEC_ID_THP)
  1188. {
  1189. if (x == 0xff) {
  1190. while (src < buf_end && x == 0xff)
  1191. x = *(src++);
  1192. if (x >= 0xd0 && x <= 0xd7)
  1193. *(dst++) = x;
  1194. else if (x)
  1195. break;
  1196. }
  1197. }
  1198. }
  1199. *unescaped_buf_ptr = s->buffer;
  1200. *unescaped_buf_size = dst - s->buffer;
  1201. av_log(s->avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n",
  1202. (buf_end - *buf_ptr) - (dst - s->buffer));
  1203. }
  1204. else if(start_code == SOS && s->ls){
  1205. const uint8_t *src = *buf_ptr;
  1206. uint8_t *dst = s->buffer;
  1207. int bit_count = 0;
  1208. int t = 0, b = 0;
  1209. PutBitContext pb;
  1210. s->cur_scan++;
  1211. /* find marker */
  1212. while (src + t < buf_end){
  1213. uint8_t x = src[t++];
  1214. if (x == 0xff){
  1215. while((src + t < buf_end) && x == 0xff)
  1216. x = src[t++];
  1217. if (x & 0x80) {
  1218. t -= 2;
  1219. break;
  1220. }
  1221. }
  1222. }
  1223. bit_count = t * 8;
  1224. init_put_bits(&pb, dst, t);
  1225. /* unescape bitstream */
  1226. while(b < t){
  1227. uint8_t x = src[b++];
  1228. put_bits(&pb, 8, x);
  1229. if(x == 0xFF){
  1230. x = src[b++];
  1231. put_bits(&pb, 7, x);
  1232. bit_count--;
  1233. }
  1234. }
  1235. flush_put_bits(&pb);
  1236. *unescaped_buf_ptr = dst;
  1237. *unescaped_buf_size = (bit_count + 7) >> 3;
  1238. }
  1239. else
  1240. {
  1241. *unescaped_buf_ptr = *buf_ptr;
  1242. *unescaped_buf_size = buf_end - *buf_ptr;
  1243. }
  1244. return start_code;
  1245. }
  1246. int ff_mjpeg_decode_frame(AVCodecContext *avctx,
  1247. void *data, int *data_size,
  1248. AVPacket *avpkt)
  1249. {
  1250. const uint8_t *buf = avpkt->data;
  1251. int buf_size = avpkt->size;
  1252. MJpegDecodeContext *s = avctx->priv_data;
  1253. const uint8_t *buf_end, *buf_ptr;
  1254. const uint8_t *unescaped_buf_ptr;
  1255. int unescaped_buf_size;
  1256. int start_code;
  1257. AVFrame *picture = data;
  1258. s->got_picture = 0; // picture from previous image can not be reused
  1259. buf_ptr = buf;
  1260. buf_end = buf + buf_size;
  1261. while (buf_ptr < buf_end) {
  1262. /* find start next marker */
  1263. start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end,
  1264. &unescaped_buf_ptr, &unescaped_buf_size);
  1265. {
  1266. /* EOF */
  1267. if (start_code < 0) {
  1268. goto the_end;
  1269. } else {
  1270. av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n", start_code, buf_end - buf_ptr);
  1271. init_get_bits(&s->gb, unescaped_buf_ptr, unescaped_buf_size*8);
  1272. s->start_code = start_code;
  1273. if(s->avctx->debug & FF_DEBUG_STARTCODE){
  1274. av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
  1275. }
  1276. /* process markers */
  1277. if (start_code >= 0xd0 && start_code <= 0xd7) {
  1278. av_log(avctx, AV_LOG_DEBUG, "restart marker: %d\n", start_code&0x0f);
  1279. /* APP fields */
  1280. } else if (start_code >= APP0 && start_code <= APP15) {
  1281. mjpeg_decode_app(s);
  1282. /* Comment */
  1283. } else if (start_code == COM){
  1284. mjpeg_decode_com(s);
  1285. }
  1286. switch(start_code) {
  1287. case SOI:
  1288. s->restart_interval = 0;
  1289. s->restart_count = 0;
  1290. /* nothing to do on SOI */
  1291. break;
  1292. case DQT:
  1293. ff_mjpeg_decode_dqt(s);
  1294. break;
  1295. case DHT:
  1296. if(ff_mjpeg_decode_dht(s) < 0){
  1297. av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
  1298. return -1;
  1299. }
  1300. break;
  1301. case SOF0:
  1302. case SOF1:
  1303. s->lossless=0;
  1304. s->ls=0;
  1305. s->progressive=0;
  1306. if (ff_mjpeg_decode_sof(s) < 0)
  1307. return -1;
  1308. break;
  1309. case SOF2:
  1310. s->lossless=0;
  1311. s->ls=0;
  1312. s->progressive=1;
  1313. if (ff_mjpeg_decode_sof(s) < 0)
  1314. return -1;
  1315. break;
  1316. case SOF3:
  1317. s->lossless=1;
  1318. s->ls=0;
  1319. s->progressive=0;
  1320. if (ff_mjpeg_decode_sof(s) < 0)
  1321. return -1;
  1322. break;
  1323. case SOF48:
  1324. s->lossless=1;
  1325. s->ls=1;
  1326. s->progressive=0;
  1327. if (ff_mjpeg_decode_sof(s) < 0)
  1328. return -1;
  1329. break;
  1330. case LSE:
  1331. if (!CONFIG_JPEGLS_DECODER || ff_jpegls_decode_lse(s) < 0)
  1332. return -1;
  1333. break;
  1334. case EOI:
  1335. eoi_parser:
  1336. s->cur_scan = 0;
  1337. if (!s->got_picture) {
  1338. av_log(avctx, AV_LOG_WARNING, "Found EOI before any SOF, ignoring\n");
  1339. break;
  1340. }
  1341. if (s->interlaced) {
  1342. s->bottom_field ^= 1;
  1343. /* if not bottom field, do not output image yet */
  1344. if (s->bottom_field == !s->interlace_polarity)
  1345. break;
  1346. }
  1347. *picture = *s->picture_ptr;
  1348. *data_size = sizeof(AVFrame);
  1349. if(!s->lossless){
  1350. picture->quality= FFMAX3(s->qscale[0], s->qscale[1], s->qscale[2]);
  1351. picture->qstride= 0;
  1352. picture->qscale_table= s->qscale_table;
  1353. memset(picture->qscale_table, picture->quality, (s->width+15)/16);
  1354. if(avctx->debug & FF_DEBUG_QP)
  1355. av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
  1356. picture->quality*= FF_QP2LAMBDA;
  1357. }
  1358. goto the_end;
  1359. case SOS:
  1360. if (!s->got_picture) {
  1361. av_log(avctx, AV_LOG_WARNING, "Can not process SOS before SOF, skipping\n");
  1362. break;
  1363. }
  1364. if (ff_mjpeg_decode_sos(s, NULL, NULL) < 0 &&
  1365. (avctx->err_recognition & AV_EF_EXPLODE))
  1366. return AVERROR_INVALIDDATA;
  1367. break;
  1368. case DRI:
  1369. mjpeg_decode_dri(s);
  1370. break;
  1371. case SOF5:
  1372. case SOF6:
  1373. case SOF7:
  1374. case SOF9:
  1375. case SOF10:
  1376. case SOF11:
  1377. case SOF13:
  1378. case SOF14:
  1379. case SOF15:
  1380. case JPG:
  1381. av_log(avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\n", start_code);
  1382. break;
  1383. // default:
  1384. // printf("mjpeg: unsupported marker (%x)\n", start_code);
  1385. // break;
  1386. }
  1387. /* eof process start code */
  1388. buf_ptr += (get_bits_count(&s->gb)+7)/8;
  1389. av_log(avctx, AV_LOG_DEBUG, "marker parser used %d bytes (%d bits)\n",
  1390. (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb));
  1391. }
  1392. }
  1393. }
  1394. if (s->got_picture) {
  1395. av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n");
  1396. goto eoi_parser;
  1397. }
  1398. av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n");
  1399. return -1;
  1400. the_end:
  1401. av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %td bytes\n", buf_end - buf_ptr);
  1402. // return buf_end - buf_ptr;
  1403. return buf_ptr - buf;
  1404. }
  1405. av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
  1406. {
  1407. MJpegDecodeContext *s = avctx->priv_data;
  1408. int i, j;
  1409. if (s->picture_ptr && s->picture_ptr->data[0])
  1410. avctx->release_buffer(avctx, s->picture_ptr);
  1411. av_free(s->buffer);
  1412. av_free(s->qscale_table);
  1413. av_freep(&s->ljpeg_buffer);
  1414. s->ljpeg_buffer_size=0;
  1415. for(i=0;i<3;i++) {
  1416. for(j=0;j<4;j++)
  1417. free_vlc(&s->vlcs[i][j]);
  1418. }
  1419. for(i=0; i<MAX_COMPONENTS; i++) {
  1420. av_freep(&s->blocks[i]);
  1421. av_freep(&s->last_nnz[i]);
  1422. }
  1423. return 0;
  1424. }
  1425. #define OFFSET(x) offsetof(MJpegDecodeContext, x)
  1426. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  1427. static const AVOption options[] = {
  1428. { "extern_huff", "Use external huffman table.", OFFSET(extern_huff), AV_OPT_TYPE_INT, { 0 }, 0, 1, VD },
  1429. { NULL },
  1430. };
  1431. static const AVClass mjpegdec_class = {
  1432. .class_name = "MJPEG decoder",
  1433. .item_name = av_default_item_name,
  1434. .option = options,
  1435. .version = LIBAVUTIL_VERSION_INT,
  1436. };
  1437. AVCodec ff_mjpeg_decoder = {
  1438. .name = "mjpeg",
  1439. .type = AVMEDIA_TYPE_VIDEO,
  1440. .id = CODEC_ID_MJPEG,
  1441. .priv_data_size = sizeof(MJpegDecodeContext),
  1442. .init = ff_mjpeg_decode_init,
  1443. .close = ff_mjpeg_decode_end,
  1444. .decode = ff_mjpeg_decode_frame,
  1445. .capabilities = CODEC_CAP_DR1,
  1446. .max_lowres = 3,
  1447. .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
  1448. .priv_class = &mjpegdec_class,
  1449. };
  1450. AVCodec ff_thp_decoder = {
  1451. .name = "thp",
  1452. .type = AVMEDIA_TYPE_VIDEO,
  1453. .id = CODEC_ID_THP,
  1454. .priv_data_size = sizeof(MJpegDecodeContext),
  1455. .init = ff_mjpeg_decode_init,
  1456. .close = ff_mjpeg_decode_end,
  1457. .decode = ff_mjpeg_decode_frame,
  1458. .capabilities = CODEC_CAP_DR1,
  1459. .max_lowres = 3,
  1460. .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"),
  1461. };