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.

1538 lines
51KB

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