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.

1384 lines
46KB

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