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.

1568 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. if(get_bits_count(&s->gb)>s->gb.size_in_bits){
  701. av_log(s->avctx, AV_LOG_ERROR, "overread %d\n", get_bits_count(&s->gb) - s->gb.size_in_bits);
  702. return -1;
  703. }
  704. for(i=0;i<nb_components;i++) {
  705. uint8_t *ptr;
  706. int n, h, v, x, y, c, j;
  707. n = s->nb_blocks[i];
  708. c = s->comp_index[i];
  709. h = s->h_scount[i];
  710. v = s->v_scount[i];
  711. x = 0;
  712. y = 0;
  713. for(j=0;j<n;j++) {
  714. ptr = data[c] +
  715. (((linesize[c] * (v * mb_y + y) * 8) +
  716. (h * mb_x + x) * 8) >> s->avctx->lowres);
  717. if(s->interlaced && s->bottom_field)
  718. ptr += linesize[c] >> 1;
  719. if(!s->progressive) {
  720. s->dsp.clear_block(s->block);
  721. if(decode_block(s, s->block, i,
  722. s->dc_index[i], s->ac_index[i],
  723. s->quant_matrixes[ s->quant_index[c] ]) < 0) {
  724. av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
  725. return -1;
  726. }
  727. s->dsp.idct_put(ptr, linesize[c], s->block);
  728. } else {
  729. int block_idx = s->block_stride[c] * (v * mb_y + y) + (h * mb_x + x);
  730. DCTELEM *block = s->blocks[c][block_idx];
  731. if(Ah)
  732. block[0] += get_bits1(&s->gb) * s->quant_matrixes[ s->quant_index[c] ][0] << Al;
  733. else if(decode_dc_progressive(s, block, i, s->dc_index[i], s->quant_matrixes[ s->quant_index[c] ], Al) < 0) {
  734. av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
  735. return -1;
  736. }
  737. }
  738. // av_log(s->avctx, AV_LOG_DEBUG, "mb: %d %d processed\n", mb_y, mb_x);
  739. //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);
  740. if (++x == h) {
  741. x = 0;
  742. y++;
  743. }
  744. }
  745. }
  746. if (s->restart_interval && show_bits(&s->gb, 8) == 0xFF){ /* skip RSTn */
  747. --s->restart_count;
  748. align_get_bits(&s->gb);
  749. while(show_bits(&s->gb, 8) == 0xFF)
  750. skip_bits(&s->gb, 8);
  751. skip_bits(&s->gb, 8);
  752. for (i=0; i<nb_components; i++) /* reset dc */
  753. s->last_dc[i] = 1024;
  754. }
  755. }
  756. }
  757. return 0;
  758. }
  759. static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss, int se, int Ah, int Al){
  760. int mb_x, mb_y;
  761. int EOBRUN = 0;
  762. int c = s->comp_index[0];
  763. uint8_t* data = s->picture.data[c];
  764. int linesize = s->linesize[c];
  765. int last_scan = 0;
  766. int16_t *quant_matrix = s->quant_matrixes[ s->quant_index[c] ];
  767. if(!Al) {
  768. s->coefs_finished[c] |= (1LL<<(se+1))-(1LL<<ss);
  769. last_scan = !~s->coefs_finished[c];
  770. }
  771. if(s->interlaced && s->bottom_field)
  772. data += linesize >> 1;
  773. for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
  774. uint8_t *ptr = data + (mb_y*linesize*8 >> s->avctx->lowres);
  775. int block_idx = mb_y * s->block_stride[c];
  776. DCTELEM (*block)[64] = &s->blocks[c][block_idx];
  777. uint8_t *last_nnz = &s->last_nnz[c][block_idx];
  778. for(mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
  779. int ret;
  780. if(Ah)
  781. ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0],
  782. quant_matrix, ss, se, Al, &EOBRUN);
  783. else
  784. ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0],
  785. quant_matrix, ss, se, Al, &EOBRUN);
  786. if(ret < 0) {
  787. av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
  788. return -1;
  789. }
  790. if(last_scan) {
  791. s->dsp.idct_put(ptr, linesize, *block);
  792. ptr += 8 >> s->avctx->lowres;
  793. }
  794. }
  795. }
  796. return 0;
  797. }
  798. int ff_mjpeg_decode_sos(MJpegDecodeContext *s)
  799. {
  800. int len, nb_components, i, h, v, predictor, point_transform;
  801. int index, id;
  802. const int block_size= s->lossless ? 1 : 8;
  803. int ilv, prev_shift;
  804. /* XXX: verify len field validity */
  805. len = get_bits(&s->gb, 16);
  806. nb_components = get_bits(&s->gb, 8);
  807. if (nb_components == 0 || nb_components > MAX_COMPONENTS){
  808. av_log(s->avctx, AV_LOG_ERROR, "decode_sos: nb_components (%d) unsupported\n", nb_components);
  809. return -1;
  810. }
  811. if (len != 6+2*nb_components)
  812. {
  813. av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len);
  814. return -1;
  815. }
  816. for(i=0;i<nb_components;i++) {
  817. id = get_bits(&s->gb, 8) - 1;
  818. av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
  819. /* find component index */
  820. for(index=0;index<s->nb_components;index++)
  821. if (id == s->component_id[index])
  822. break;
  823. if (index == s->nb_components)
  824. {
  825. av_log(s->avctx, AV_LOG_ERROR, "decode_sos: index(%d) out of components\n", index);
  826. return -1;
  827. }
  828. /* Metasoft MJPEG codec has Cb and Cr swapped */
  829. if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J')
  830. && nb_components == 3 && s->nb_components == 3 && i)
  831. index = 3 - i;
  832. s->comp_index[i] = index;
  833. s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
  834. s->h_scount[i] = s->h_count[index];
  835. s->v_scount[i] = s->v_count[index];
  836. s->dc_index[i] = get_bits(&s->gb, 4);
  837. s->ac_index[i] = get_bits(&s->gb, 4);
  838. if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
  839. s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
  840. goto out_of_range;
  841. if (!s->vlcs[0][s->dc_index[i]].table || !s->vlcs[1][s->ac_index[i]].table)
  842. goto out_of_range;
  843. }
  844. predictor= get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
  845. ilv= get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */
  846. prev_shift = get_bits(&s->gb, 4); /* Ah */
  847. point_transform= get_bits(&s->gb, 4); /* Al */
  848. for(i=0;i<nb_components;i++)
  849. s->last_dc[i] = 1024;
  850. if (nb_components > 1) {
  851. /* interleaved stream */
  852. s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
  853. s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
  854. } else if(!s->ls) { /* skip this for JPEG-LS */
  855. h = s->h_max / s->h_scount[0];
  856. v = s->v_max / s->v_scount[0];
  857. s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
  858. s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
  859. s->nb_blocks[0] = 1;
  860. s->h_scount[0] = 1;
  861. s->v_scount[0] = 1;
  862. }
  863. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  864. 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" : "",
  865. predictor, point_transform, ilv, s->bits,
  866. s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""));
  867. /* mjpeg-b can have padding bytes between sos and image data, skip them */
  868. for (i = s->mjpb_skiptosod; i > 0; i--)
  869. skip_bits(&s->gb, 8);
  870. if(s->lossless){
  871. if(CONFIG_JPEGLS_DECODER && s->ls){
  872. // for(){
  873. // reset_ls_coding_parameters(s, 0);
  874. if(ff_jpegls_decode_picture(s, predictor, point_transform, ilv) < 0)
  875. return -1;
  876. }else{
  877. if(s->rgb){
  878. if(ljpeg_decode_rgb_scan(s, predictor, point_transform) < 0)
  879. return -1;
  880. }else{
  881. if(ljpeg_decode_yuv_scan(s, predictor, point_transform) < 0)
  882. return -1;
  883. }
  884. }
  885. }else{
  886. if(s->progressive && predictor) {
  887. if(mjpeg_decode_scan_progressive_ac(s, predictor, ilv, prev_shift, point_transform) < 0)
  888. return -1;
  889. } else {
  890. if(mjpeg_decode_scan(s, nb_components, prev_shift, point_transform) < 0)
  891. return -1;
  892. }
  893. }
  894. emms_c();
  895. return 0;
  896. out_of_range:
  897. av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
  898. return -1;
  899. }
  900. static int mjpeg_decode_dri(MJpegDecodeContext *s)
  901. {
  902. if (get_bits(&s->gb, 16) != 4)
  903. return -1;
  904. s->restart_interval = get_bits(&s->gb, 16);
  905. s->restart_count = 0;
  906. av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n", s->restart_interval);
  907. return 0;
  908. }
  909. static int mjpeg_decode_app(MJpegDecodeContext *s)
  910. {
  911. int len, id, i;
  912. len = get_bits(&s->gb, 16);
  913. if (len < 5)
  914. return -1;
  915. if(8*len + get_bits_count(&s->gb) > s->gb.size_in_bits)
  916. return -1;
  917. id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
  918. id = be2me_32(id);
  919. len -= 6;
  920. if(s->avctx->debug & FF_DEBUG_STARTCODE){
  921. av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id);
  922. }
  923. /* buggy AVID, it puts EOI only at every 10th frame */
  924. /* also this fourcc is used by non-avid files too, it holds some
  925. informations, but it's always present in AVID creates files */
  926. if (id == AV_RL32("AVI1"))
  927. {
  928. /* structure:
  929. 4bytes AVI1
  930. 1bytes polarity
  931. 1bytes always zero
  932. 4bytes field_size
  933. 4bytes field_size_less_padding
  934. */
  935. s->buggy_avid = 1;
  936. // if (s->first_picture)
  937. // printf("mjpeg: workarounding buggy AVID\n");
  938. i = get_bits(&s->gb, 8);
  939. if (i==2) s->bottom_field= 1;
  940. else if(i==1) s->bottom_field= 0;
  941. #if 0
  942. skip_bits(&s->gb, 8);
  943. skip_bits(&s->gb, 32);
  944. skip_bits(&s->gb, 32);
  945. len -= 10;
  946. #endif
  947. // if (s->interlace_polarity)
  948. // printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity);
  949. goto out;
  950. }
  951. // len -= 2;
  952. if (id == AV_RL32("JFIF"))
  953. {
  954. int t_w, t_h, v1, v2;
  955. skip_bits(&s->gb, 8); /* the trailing zero-byte */
  956. v1= get_bits(&s->gb, 8);
  957. v2= get_bits(&s->gb, 8);
  958. skip_bits(&s->gb, 8);
  959. s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 16);
  960. s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 16);
  961. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  962. av_log(s->avctx, AV_LOG_INFO, "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
  963. v1, v2,
  964. s->avctx->sample_aspect_ratio.num,
  965. s->avctx->sample_aspect_ratio.den
  966. );
  967. t_w = get_bits(&s->gb, 8);
  968. t_h = get_bits(&s->gb, 8);
  969. if (t_w && t_h)
  970. {
  971. /* skip thumbnail */
  972. if (len-10-(t_w*t_h*3) > 0)
  973. len -= t_w*t_h*3;
  974. }
  975. len -= 10;
  976. goto out;
  977. }
  978. if (id == AV_RL32("Adob") && (get_bits(&s->gb, 8) == 'e'))
  979. {
  980. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  981. av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n");
  982. skip_bits(&s->gb, 16); /* version */
  983. skip_bits(&s->gb, 16); /* flags0 */
  984. skip_bits(&s->gb, 16); /* flags1 */
  985. skip_bits(&s->gb, 8); /* transform */
  986. len -= 7;
  987. goto out;
  988. }
  989. if (id == AV_RL32("LJIF")){
  990. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  991. av_log(s->avctx, AV_LOG_INFO, "Pegasus lossless jpeg header found\n");
  992. skip_bits(&s->gb, 16); /* version ? */
  993. skip_bits(&s->gb, 16); /* unknwon always 0? */
  994. skip_bits(&s->gb, 16); /* unknwon always 0? */
  995. skip_bits(&s->gb, 16); /* unknwon always 0? */
  996. switch( get_bits(&s->gb, 8)){
  997. case 1:
  998. s->rgb= 1;
  999. s->pegasus_rct=0;
  1000. break;
  1001. case 2:
  1002. s->rgb= 1;
  1003. s->pegasus_rct=1;
  1004. break;
  1005. default:
  1006. av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n");
  1007. }
  1008. len -= 9;
  1009. goto out;
  1010. }
  1011. /* Apple MJPEG-A */
  1012. if ((s->start_code == APP1) && (len > (0x28 - 8)))
  1013. {
  1014. id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
  1015. id = be2me_32(id);
  1016. len -= 4;
  1017. if (id == AV_RL32("mjpg")) /* Apple MJPEG-A */
  1018. {
  1019. #if 0
  1020. skip_bits(&s->gb, 32); /* field size */
  1021. skip_bits(&s->gb, 32); /* pad field size */
  1022. skip_bits(&s->gb, 32); /* next off */
  1023. skip_bits(&s->gb, 32); /* quant off */
  1024. skip_bits(&s->gb, 32); /* huff off */
  1025. skip_bits(&s->gb, 32); /* image off */
  1026. skip_bits(&s->gb, 32); /* scan off */
  1027. skip_bits(&s->gb, 32); /* data off */
  1028. #endif
  1029. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1030. av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
  1031. }
  1032. }
  1033. out:
  1034. /* slow but needed for extreme adobe jpegs */
  1035. if (len < 0)
  1036. av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error, decode_app parser read over the end\n");
  1037. while(--len > 0)
  1038. skip_bits(&s->gb, 8);
  1039. return 0;
  1040. }
  1041. static int mjpeg_decode_com(MJpegDecodeContext *s)
  1042. {
  1043. int len = get_bits(&s->gb, 16);
  1044. if (len >= 2 && 8*len - 16 + get_bits_count(&s->gb) <= s->gb.size_in_bits) {
  1045. char *cbuf = av_malloc(len - 1);
  1046. if (cbuf) {
  1047. int i;
  1048. for (i = 0; i < len - 2; i++)
  1049. cbuf[i] = get_bits(&s->gb, 8);
  1050. if (i > 0 && cbuf[i-1] == '\n')
  1051. cbuf[i-1] = 0;
  1052. else
  1053. cbuf[i] = 0;
  1054. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1055. av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf);
  1056. /* buggy avid, it puts EOI only at every 10th frame */
  1057. if (!strcmp(cbuf, "AVID"))
  1058. {
  1059. s->buggy_avid = 1;
  1060. // if (s->first_picture)
  1061. // printf("mjpeg: workarounding buggy AVID\n");
  1062. }
  1063. else if(!strcmp(cbuf, "CS=ITU601")){
  1064. s->cs_itu601= 1;
  1065. }
  1066. else if((len > 20 && !strncmp(cbuf, "Intel(R) JPEG Library", 21)) ||
  1067. (len > 19 && !strncmp(cbuf, "Metasoft MJPEG Codec", 20))){
  1068. s->flipped = 1;
  1069. }
  1070. av_free(cbuf);
  1071. }
  1072. }
  1073. return 0;
  1074. }
  1075. #if 0
  1076. static int valid_marker_list[] =
  1077. {
  1078. /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */
  1079. /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1080. /* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1081. /* 2 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1082. /* 3 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1083. /* 4 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1084. /* 5 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1085. /* 6 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1086. /* 7 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1087. /* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1088. /* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1089. /* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1090. /* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1091. /* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1092. /* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1093. /* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1094. /* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
  1095. }
  1096. #endif
  1097. /* return the 8 bit start code value and update the search
  1098. state. Return -1 if no start code found */
  1099. static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
  1100. {
  1101. const uint8_t *buf_ptr;
  1102. unsigned int v, v2;
  1103. int val;
  1104. #ifdef DEBUG
  1105. int skipped=0;
  1106. #endif
  1107. buf_ptr = *pbuf_ptr;
  1108. while (buf_ptr < buf_end) {
  1109. v = *buf_ptr++;
  1110. v2 = *buf_ptr;
  1111. if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
  1112. val = *buf_ptr++;
  1113. goto found;
  1114. }
  1115. #ifdef DEBUG
  1116. skipped++;
  1117. #endif
  1118. }
  1119. val = -1;
  1120. found:
  1121. dprintf(NULL, "find_marker skipped %d bytes\n", skipped);
  1122. *pbuf_ptr = buf_ptr;
  1123. return val;
  1124. }
  1125. int ff_mjpeg_decode_frame(AVCodecContext *avctx,
  1126. void *data, int *data_size,
  1127. AVPacket *avpkt)
  1128. {
  1129. const uint8_t *buf = avpkt->data;
  1130. int buf_size = avpkt->size;
  1131. MJpegDecodeContext *s = avctx->priv_data;
  1132. const uint8_t *buf_end, *buf_ptr;
  1133. int start_code;
  1134. AVFrame *picture = data;
  1135. s->got_picture = 0; // picture from previous image can not be reused
  1136. buf_ptr = buf;
  1137. buf_end = buf + buf_size;
  1138. while (buf_ptr < buf_end) {
  1139. /* find start next marker */
  1140. start_code = find_marker(&buf_ptr, buf_end);
  1141. {
  1142. /* EOF */
  1143. if (start_code < 0) {
  1144. goto the_end;
  1145. } else {
  1146. av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n", start_code, buf_end - buf_ptr);
  1147. if ((buf_end - buf_ptr) > s->buffer_size)
  1148. {
  1149. av_free(s->buffer);
  1150. s->buffer_size = buf_end-buf_ptr;
  1151. s->buffer = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE);
  1152. av_log(avctx, AV_LOG_DEBUG, "buffer too small, expanding to %d bytes\n",
  1153. s->buffer_size);
  1154. }
  1155. /* unescape buffer of SOS, use special treatment for JPEG-LS */
  1156. if (start_code == SOS && !s->ls)
  1157. {
  1158. const uint8_t *src = buf_ptr;
  1159. uint8_t *dst = s->buffer;
  1160. while (src<buf_end)
  1161. {
  1162. uint8_t x = *(src++);
  1163. *(dst++) = x;
  1164. if (avctx->codec_id != CODEC_ID_THP)
  1165. {
  1166. if (x == 0xff) {
  1167. while (src < buf_end && x == 0xff)
  1168. x = *(src++);
  1169. if (x >= 0xd0 && x <= 0xd7)
  1170. *(dst++) = x;
  1171. else if (x)
  1172. break;
  1173. }
  1174. }
  1175. }
  1176. init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8);
  1177. av_log(avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n",
  1178. (buf_end - buf_ptr) - (dst - s->buffer));
  1179. }
  1180. else if(start_code == SOS && s->ls){
  1181. const uint8_t *src = buf_ptr;
  1182. uint8_t *dst = s->buffer;
  1183. int bit_count = 0;
  1184. int t = 0, b = 0;
  1185. PutBitContext pb;
  1186. s->cur_scan++;
  1187. /* find marker */
  1188. while (src + t < buf_end){
  1189. uint8_t x = src[t++];
  1190. if (x == 0xff){
  1191. while((src + t < buf_end) && x == 0xff)
  1192. x = src[t++];
  1193. if (x & 0x80) {
  1194. t -= 2;
  1195. break;
  1196. }
  1197. }
  1198. }
  1199. bit_count = t * 8;
  1200. init_put_bits(&pb, dst, t);
  1201. /* unescape bitstream */
  1202. while(b < t){
  1203. uint8_t x = src[b++];
  1204. put_bits(&pb, 8, x);
  1205. if(x == 0xFF){
  1206. x = src[b++];
  1207. put_bits(&pb, 7, x);
  1208. bit_count--;
  1209. }
  1210. }
  1211. flush_put_bits(&pb);
  1212. init_get_bits(&s->gb, dst, bit_count);
  1213. }
  1214. else
  1215. init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8);
  1216. s->start_code = start_code;
  1217. if(s->avctx->debug & FF_DEBUG_STARTCODE){
  1218. av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
  1219. }
  1220. /* process markers */
  1221. if (start_code >= 0xd0 && start_code <= 0xd7) {
  1222. av_log(avctx, AV_LOG_DEBUG, "restart marker: %d\n", start_code&0x0f);
  1223. /* APP fields */
  1224. } else if (start_code >= APP0 && start_code <= APP15) {
  1225. mjpeg_decode_app(s);
  1226. /* Comment */
  1227. } else if (start_code == COM){
  1228. mjpeg_decode_com(s);
  1229. }
  1230. switch(start_code) {
  1231. case SOI:
  1232. s->restart_interval = 0;
  1233. s->restart_count = 0;
  1234. /* nothing to do on SOI */
  1235. break;
  1236. case DQT:
  1237. ff_mjpeg_decode_dqt(s);
  1238. break;
  1239. case DHT:
  1240. if(ff_mjpeg_decode_dht(s) < 0){
  1241. av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
  1242. return -1;
  1243. }
  1244. break;
  1245. case SOF0:
  1246. case SOF1:
  1247. s->lossless=0;
  1248. s->ls=0;
  1249. s->progressive=0;
  1250. if (ff_mjpeg_decode_sof(s) < 0)
  1251. return -1;
  1252. break;
  1253. case SOF2:
  1254. s->lossless=0;
  1255. s->ls=0;
  1256. s->progressive=1;
  1257. if (ff_mjpeg_decode_sof(s) < 0)
  1258. return -1;
  1259. break;
  1260. case SOF3:
  1261. s->lossless=1;
  1262. s->ls=0;
  1263. s->progressive=0;
  1264. if (ff_mjpeg_decode_sof(s) < 0)
  1265. return -1;
  1266. break;
  1267. case SOF48:
  1268. s->lossless=1;
  1269. s->ls=1;
  1270. s->progressive=0;
  1271. if (ff_mjpeg_decode_sof(s) < 0)
  1272. return -1;
  1273. break;
  1274. case LSE:
  1275. if (!CONFIG_JPEGLS_DECODER || ff_jpegls_decode_lse(s) < 0)
  1276. return -1;
  1277. break;
  1278. case EOI:
  1279. s->cur_scan = 0;
  1280. if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
  1281. break;
  1282. eoi_parser:
  1283. if (!s->got_picture) {
  1284. av_log(avctx, AV_LOG_WARNING, "Found EOI before any SOF, ignoring\n");
  1285. break;
  1286. }
  1287. {
  1288. if (s->interlaced) {
  1289. s->bottom_field ^= 1;
  1290. /* if not bottom field, do not output image yet */
  1291. if (s->bottom_field == !s->interlace_polarity)
  1292. goto not_the_end;
  1293. }
  1294. *picture = s->picture;
  1295. *data_size = sizeof(AVFrame);
  1296. if(!s->lossless){
  1297. picture->quality= FFMAX3(s->qscale[0], s->qscale[1], s->qscale[2]);
  1298. picture->qstride= 0;
  1299. picture->qscale_table= s->qscale_table;
  1300. memset(picture->qscale_table, picture->quality, (s->width+15)/16);
  1301. if(avctx->debug & FF_DEBUG_QP)
  1302. av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
  1303. picture->quality*= FF_QP2LAMBDA;
  1304. }
  1305. goto the_end;
  1306. }
  1307. break;
  1308. case SOS:
  1309. if (!s->got_picture) {
  1310. av_log(avctx, AV_LOG_WARNING, "Can not process SOS before SOF, skipping\n");
  1311. break;
  1312. }
  1313. ff_mjpeg_decode_sos(s);
  1314. /* buggy avid puts EOI every 10-20th frame */
  1315. /* if restart period is over process EOI */
  1316. if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
  1317. goto eoi_parser;
  1318. break;
  1319. case DRI:
  1320. mjpeg_decode_dri(s);
  1321. break;
  1322. case SOF5:
  1323. case SOF6:
  1324. case SOF7:
  1325. case SOF9:
  1326. case SOF10:
  1327. case SOF11:
  1328. case SOF13:
  1329. case SOF14:
  1330. case SOF15:
  1331. case JPG:
  1332. av_log(avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\n", start_code);
  1333. break;
  1334. // default:
  1335. // printf("mjpeg: unsupported marker (%x)\n", start_code);
  1336. // break;
  1337. }
  1338. not_the_end:
  1339. /* eof process start code */
  1340. buf_ptr += (get_bits_count(&s->gb)+7)/8;
  1341. av_log(avctx, AV_LOG_DEBUG, "marker parser used %d bytes (%d bits)\n",
  1342. (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb));
  1343. }
  1344. }
  1345. }
  1346. if (s->got_picture) {
  1347. av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n");
  1348. goto eoi_parser;
  1349. }
  1350. av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n");
  1351. return -1;
  1352. the_end:
  1353. av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %td bytes\n", buf_end - buf_ptr);
  1354. // return buf_end - buf_ptr;
  1355. return buf_ptr - buf;
  1356. }
  1357. av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
  1358. {
  1359. MJpegDecodeContext *s = avctx->priv_data;
  1360. int i, j;
  1361. if (s->picture.data[0])
  1362. avctx->release_buffer(avctx, &s->picture);
  1363. av_free(s->buffer);
  1364. av_free(s->qscale_table);
  1365. av_freep(&s->ljpeg_buffer);
  1366. s->ljpeg_buffer_size=0;
  1367. for(i=0;i<2;i++) {
  1368. for(j=0;j<4;j++)
  1369. free_vlc(&s->vlcs[i][j]);
  1370. }
  1371. for(i=0; i<MAX_COMPONENTS; i++) {
  1372. av_freep(&s->blocks[i]);
  1373. av_freep(&s->last_nnz[i]);
  1374. }
  1375. return 0;
  1376. }
  1377. AVCodec mjpeg_decoder = {
  1378. "mjpeg",
  1379. AVMEDIA_TYPE_VIDEO,
  1380. CODEC_ID_MJPEG,
  1381. sizeof(MJpegDecodeContext),
  1382. ff_mjpeg_decode_init,
  1383. NULL,
  1384. ff_mjpeg_decode_end,
  1385. ff_mjpeg_decode_frame,
  1386. CODEC_CAP_DR1,
  1387. NULL,
  1388. .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
  1389. };
  1390. AVCodec thp_decoder = {
  1391. "thp",
  1392. AVMEDIA_TYPE_VIDEO,
  1393. CODEC_ID_THP,
  1394. sizeof(MJpegDecodeContext),
  1395. ff_mjpeg_decode_init,
  1396. NULL,
  1397. ff_mjpeg_decode_end,
  1398. ff_mjpeg_decode_frame,
  1399. CODEC_CAP_DR1,
  1400. NULL,
  1401. .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"),
  1402. };