mjpegdec.c 51 KB

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