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.

1561 lines
52KB

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