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.

1645 lines
55KB

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