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.

2571 lines
80KB

  1. /*
  2. * MJPEG encoder and decoder
  3. * Copyright (c) 2000, 2001 Fabrice Bellard.
  4. * Copyright (c) 2003 Alex Beregszaszi
  5. * Copyright (c) 2003-2004 Michael Niedermayer
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Support for external huffman table, various fixes (AVID workaround),
  24. * aspecting, new decode_frame mechanism and apple mjpeg-b support
  25. * by Alex Beregszaszi <alex@naxine.org>
  26. */
  27. /**
  28. * @file mjpeg.c
  29. * MJPEG encoder and decoder.
  30. */
  31. //#define DEBUG
  32. #include <assert.h>
  33. #include "avcodec.h"
  34. #include "dsputil.h"
  35. #include "mpegvideo.h"
  36. /* use two quantizer tables (one for luminance and one for chrominance) */
  37. /* not yet working */
  38. #undef TWOMATRIXES
  39. typedef struct MJpegContext {
  40. uint8_t huff_size_dc_luminance[12]; //FIXME use array [3] instead of lumi / chrom, for easier addressing
  41. uint16_t huff_code_dc_luminance[12];
  42. uint8_t huff_size_dc_chrominance[12];
  43. uint16_t huff_code_dc_chrominance[12];
  44. uint8_t huff_size_ac_luminance[256];
  45. uint16_t huff_code_ac_luminance[256];
  46. uint8_t huff_size_ac_chrominance[256];
  47. uint16_t huff_code_ac_chrominance[256];
  48. } MJpegContext;
  49. /* JPEG marker codes */
  50. typedef enum {
  51. /* start of frame */
  52. SOF0 = 0xc0, /* baseline */
  53. SOF1 = 0xc1, /* extended sequential, huffman */
  54. SOF2 = 0xc2, /* progressive, huffman */
  55. SOF3 = 0xc3, /* lossless, huffman */
  56. SOF5 = 0xc5, /* differential sequential, huffman */
  57. SOF6 = 0xc6, /* differential progressive, huffman */
  58. SOF7 = 0xc7, /* differential lossless, huffman */
  59. JPG = 0xc8, /* reserved for JPEG extension */
  60. SOF9 = 0xc9, /* extended sequential, arithmetic */
  61. SOF10 = 0xca, /* progressive, arithmetic */
  62. SOF11 = 0xcb, /* lossless, arithmetic */
  63. SOF13 = 0xcd, /* differential sequential, arithmetic */
  64. SOF14 = 0xce, /* differential progressive, arithmetic */
  65. SOF15 = 0xcf, /* differential lossless, arithmetic */
  66. DHT = 0xc4, /* define huffman tables */
  67. DAC = 0xcc, /* define arithmetic-coding conditioning */
  68. /* restart with modulo 8 count "m" */
  69. RST0 = 0xd0,
  70. RST1 = 0xd1,
  71. RST2 = 0xd2,
  72. RST3 = 0xd3,
  73. RST4 = 0xd4,
  74. RST5 = 0xd5,
  75. RST6 = 0xd6,
  76. RST7 = 0xd7,
  77. SOI = 0xd8, /* start of image */
  78. EOI = 0xd9, /* end of image */
  79. SOS = 0xda, /* start of scan */
  80. DQT = 0xdb, /* define quantization tables */
  81. DNL = 0xdc, /* define number of lines */
  82. DRI = 0xdd, /* define restart interval */
  83. DHP = 0xde, /* define hierarchical progression */
  84. EXP = 0xdf, /* expand reference components */
  85. APP0 = 0xe0,
  86. APP1 = 0xe1,
  87. APP2 = 0xe2,
  88. APP3 = 0xe3,
  89. APP4 = 0xe4,
  90. APP5 = 0xe5,
  91. APP6 = 0xe6,
  92. APP7 = 0xe7,
  93. APP8 = 0xe8,
  94. APP9 = 0xe9,
  95. APP10 = 0xea,
  96. APP11 = 0xeb,
  97. APP12 = 0xec,
  98. APP13 = 0xed,
  99. APP14 = 0xee,
  100. APP15 = 0xef,
  101. JPG0 = 0xf0,
  102. JPG1 = 0xf1,
  103. JPG2 = 0xf2,
  104. JPG3 = 0xf3,
  105. JPG4 = 0xf4,
  106. JPG5 = 0xf5,
  107. JPG6 = 0xf6,
  108. SOF48 = 0xf7, ///< JPEG-LS
  109. LSE = 0xf8, ///< JPEG-LS extension parameters
  110. JPG9 = 0xf9,
  111. JPG10 = 0xfa,
  112. JPG11 = 0xfb,
  113. JPG12 = 0xfc,
  114. JPG13 = 0xfd,
  115. COM = 0xfe, /* comment */
  116. TEM = 0x01, /* temporary private use for arithmetic coding */
  117. /* 0x02 -> 0xbf reserved */
  118. } JPEG_MARKER;
  119. #if 0
  120. /* These are the sample quantization tables given in JPEG spec section K.1.
  121. * The spec says that the values given produce "good" quality, and
  122. * when divided by 2, "very good" quality.
  123. */
  124. static const unsigned char std_luminance_quant_tbl[64] = {
  125. 16, 11, 10, 16, 24, 40, 51, 61,
  126. 12, 12, 14, 19, 26, 58, 60, 55,
  127. 14, 13, 16, 24, 40, 57, 69, 56,
  128. 14, 17, 22, 29, 51, 87, 80, 62,
  129. 18, 22, 37, 56, 68, 109, 103, 77,
  130. 24, 35, 55, 64, 81, 104, 113, 92,
  131. 49, 64, 78, 87, 103, 121, 120, 101,
  132. 72, 92, 95, 98, 112, 100, 103, 99
  133. };
  134. static const unsigned char std_chrominance_quant_tbl[64] = {
  135. 17, 18, 24, 47, 99, 99, 99, 99,
  136. 18, 21, 26, 66, 99, 99, 99, 99,
  137. 24, 26, 56, 99, 99, 99, 99, 99,
  138. 47, 66, 99, 99, 99, 99, 99, 99,
  139. 99, 99, 99, 99, 99, 99, 99, 99,
  140. 99, 99, 99, 99, 99, 99, 99, 99,
  141. 99, 99, 99, 99, 99, 99, 99, 99,
  142. 99, 99, 99, 99, 99, 99, 99, 99
  143. };
  144. #endif
  145. /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
  146. /* IMPORTANT: these are only valid for 8-bit data precision! */
  147. static const uint8_t bits_dc_luminance[17] =
  148. { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
  149. static const uint8_t val_dc_luminance[] =
  150. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  151. static const uint8_t bits_dc_chrominance[17] =
  152. { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
  153. static const uint8_t val_dc_chrominance[] =
  154. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  155. static const uint8_t bits_ac_luminance[17] =
  156. { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
  157. static const uint8_t val_ac_luminance[] =
  158. { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  159. 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  160. 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  161. 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  162. 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  163. 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  164. 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  165. 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  166. 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  167. 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  168. 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  169. 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  170. 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  171. 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  172. 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  173. 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  174. 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  175. 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  176. 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  177. 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  178. 0xf9, 0xfa
  179. };
  180. static const uint8_t bits_ac_chrominance[17] =
  181. { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
  182. static const uint8_t val_ac_chrominance[] =
  183. { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
  184. 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  185. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  186. 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
  187. 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
  188. 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
  189. 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
  190. 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  191. 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  192. 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  193. 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  194. 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  195. 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
  196. 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  197. 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  198. 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
  199. 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
  200. 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
  201. 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  202. 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  203. 0xf9, 0xfa
  204. };
  205. /* isn't this function nicer than the one in the libjpeg ? */
  206. static void build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code,
  207. const uint8_t *bits_table, const uint8_t *val_table)
  208. {
  209. int i, j, k,nb, code, sym;
  210. code = 0;
  211. k = 0;
  212. for(i=1;i<=16;i++) {
  213. nb = bits_table[i];
  214. for(j=0;j<nb;j++) {
  215. sym = val_table[k++];
  216. huff_size[sym] = i;
  217. huff_code[sym] = code;
  218. code++;
  219. }
  220. code <<= 1;
  221. }
  222. }
  223. #ifdef CONFIG_ENCODERS
  224. int mjpeg_init(MpegEncContext *s)
  225. {
  226. MJpegContext *m;
  227. m = av_malloc(sizeof(MJpegContext));
  228. if (!m)
  229. return -1;
  230. s->min_qcoeff=-1023;
  231. s->max_qcoeff= 1023;
  232. /* build all the huffman tables */
  233. build_huffman_codes(m->huff_size_dc_luminance,
  234. m->huff_code_dc_luminance,
  235. bits_dc_luminance,
  236. val_dc_luminance);
  237. build_huffman_codes(m->huff_size_dc_chrominance,
  238. m->huff_code_dc_chrominance,
  239. bits_dc_chrominance,
  240. val_dc_chrominance);
  241. build_huffman_codes(m->huff_size_ac_luminance,
  242. m->huff_code_ac_luminance,
  243. bits_ac_luminance,
  244. val_ac_luminance);
  245. build_huffman_codes(m->huff_size_ac_chrominance,
  246. m->huff_code_ac_chrominance,
  247. bits_ac_chrominance,
  248. val_ac_chrominance);
  249. s->mjpeg_ctx = m;
  250. return 0;
  251. }
  252. void mjpeg_close(MpegEncContext *s)
  253. {
  254. av_free(s->mjpeg_ctx);
  255. }
  256. #endif //CONFIG_ENCODERS
  257. #define PREDICT(ret, topleft, top, left, predictor)\
  258. switch(predictor){\
  259. case 1: ret= left; break;\
  260. case 2: ret= top; break;\
  261. case 3: ret= topleft; break;\
  262. case 4: ret= left + top - topleft; break;\
  263. case 5: ret= left + ((top - topleft)>>1); break;\
  264. case 6: ret= top + ((left - topleft)>>1); break;\
  265. default:\
  266. case 7: ret= (left + top)>>1; break;\
  267. }
  268. #ifdef CONFIG_ENCODERS
  269. static inline void put_marker(PutBitContext *p, int code)
  270. {
  271. put_bits(p, 8, 0xff);
  272. put_bits(p, 8, code);
  273. }
  274. /* table_class: 0 = DC coef, 1 = AC coefs */
  275. static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
  276. const uint8_t *bits_table, const uint8_t *value_table)
  277. {
  278. PutBitContext *p = &s->pb;
  279. int n, i;
  280. put_bits(p, 4, table_class);
  281. put_bits(p, 4, table_id);
  282. n = 0;
  283. for(i=1;i<=16;i++) {
  284. n += bits_table[i];
  285. put_bits(p, 8, bits_table[i]);
  286. }
  287. for(i=0;i<n;i++)
  288. put_bits(p, 8, value_table[i]);
  289. return n + 17;
  290. }
  291. static void jpeg_table_header(MpegEncContext *s)
  292. {
  293. PutBitContext *p = &s->pb;
  294. int i, j, size;
  295. uint8_t *ptr;
  296. /* quant matrixes */
  297. put_marker(p, DQT);
  298. #ifdef TWOMATRIXES
  299. put_bits(p, 16, 2 + 2 * (1 + 64));
  300. #else
  301. put_bits(p, 16, 2 + 1 * (1 + 64));
  302. #endif
  303. put_bits(p, 4, 0); /* 8 bit precision */
  304. put_bits(p, 4, 0); /* table 0 */
  305. for(i=0;i<64;i++) {
  306. j = s->intra_scantable.permutated[i];
  307. put_bits(p, 8, s->intra_matrix[j]);
  308. }
  309. #ifdef TWOMATRIXES
  310. put_bits(p, 4, 0); /* 8 bit precision */
  311. put_bits(p, 4, 1); /* table 1 */
  312. for(i=0;i<64;i++) {
  313. j = s->intra_scantable.permutated[i];
  314. put_bits(p, 8, s->chroma_intra_matrix[j]);
  315. }
  316. #endif
  317. /* huffman table */
  318. put_marker(p, DHT);
  319. flush_put_bits(p);
  320. ptr = pbBufPtr(p);
  321. put_bits(p, 16, 0); /* patched later */
  322. size = 2;
  323. size += put_huffman_table(s, 0, 0, bits_dc_luminance, val_dc_luminance);
  324. size += put_huffman_table(s, 0, 1, bits_dc_chrominance, val_dc_chrominance);
  325. size += put_huffman_table(s, 1, 0, bits_ac_luminance, val_ac_luminance);
  326. size += put_huffman_table(s, 1, 1, bits_ac_chrominance, val_ac_chrominance);
  327. ptr[0] = size >> 8;
  328. ptr[1] = size;
  329. }
  330. static void jpeg_put_comments(MpegEncContext *s)
  331. {
  332. PutBitContext *p = &s->pb;
  333. int size;
  334. uint8_t *ptr;
  335. if (s->aspect_ratio_info /* && !lossless */)
  336. {
  337. /* JFIF header */
  338. put_marker(p, APP0);
  339. put_bits(p, 16, 16);
  340. ff_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
  341. put_bits(p, 16, 0x0201); /* v 1.02 */
  342. put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
  343. put_bits(p, 16, s->avctx->sample_aspect_ratio.num);
  344. put_bits(p, 16, s->avctx->sample_aspect_ratio.den);
  345. put_bits(p, 8, 0); /* thumbnail width */
  346. put_bits(p, 8, 0); /* thumbnail height */
  347. }
  348. /* comment */
  349. if(!(s->flags & CODEC_FLAG_BITEXACT)){
  350. put_marker(p, COM);
  351. flush_put_bits(p);
  352. ptr = pbBufPtr(p);
  353. put_bits(p, 16, 0); /* patched later */
  354. ff_put_string(p, LIBAVCODEC_IDENT, 1);
  355. size = strlen(LIBAVCODEC_IDENT)+3;
  356. ptr[0] = size >> 8;
  357. ptr[1] = size;
  358. }
  359. if( s->avctx->pix_fmt == PIX_FMT_YUV420P
  360. ||s->avctx->pix_fmt == PIX_FMT_YUV422P
  361. ||s->avctx->pix_fmt == PIX_FMT_YUV444P){
  362. put_marker(p, COM);
  363. flush_put_bits(p);
  364. ptr = pbBufPtr(p);
  365. put_bits(p, 16, 0); /* patched later */
  366. ff_put_string(p, "CS=ITU601", 1);
  367. size = strlen("CS=ITU601")+3;
  368. ptr[0] = size >> 8;
  369. ptr[1] = size;
  370. }
  371. }
  372. void mjpeg_picture_header(MpegEncContext *s)
  373. {
  374. const int lossless= s->avctx->codec_id != CODEC_ID_MJPEG;
  375. const int ls = s->avctx->codec_id == CODEC_ID_JPEGLS;
  376. assert(!(ls && s->mjpeg_write_tables));
  377. put_marker(&s->pb, SOI);
  378. if (!s->mjpeg_data_only_frames)
  379. {
  380. jpeg_put_comments(s);
  381. if (s->mjpeg_write_tables) jpeg_table_header(s);
  382. switch(s->avctx->codec_id){
  383. case CODEC_ID_MJPEG: put_marker(&s->pb, SOF0 ); break;
  384. case CODEC_ID_LJPEG: put_marker(&s->pb, SOF3 ); break;
  385. case CODEC_ID_JPEGLS: put_marker(&s->pb, SOF48); break;
  386. default: assert(0);
  387. }
  388. put_bits(&s->pb, 16, 17);
  389. if(lossless && s->avctx->pix_fmt == PIX_FMT_RGBA32)
  390. put_bits(&s->pb, 8, 9); /* 9 bits/component RCT */
  391. else
  392. put_bits(&s->pb, 8, 8); /* 8 bits/component */
  393. put_bits(&s->pb, 16, s->height);
  394. put_bits(&s->pb, 16, s->width);
  395. put_bits(&s->pb, 8, 3); /* 3 components */
  396. /* Y component */
  397. put_bits(&s->pb, 8, 1); /* component number */
  398. put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
  399. put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
  400. put_bits(&s->pb, 8, 0); /* select matrix */
  401. /* Cb component */
  402. put_bits(&s->pb, 8, 2); /* component number */
  403. put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
  404. put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
  405. #ifdef TWOMATRIXES
  406. put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
  407. #else
  408. put_bits(&s->pb, 8, 0); /* select matrix */
  409. #endif
  410. /* Cr component */
  411. put_bits(&s->pb, 8, 3); /* component number */
  412. put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
  413. put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
  414. #ifdef TWOMATRIXES
  415. put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
  416. #else
  417. put_bits(&s->pb, 8, 0); /* select matrix */
  418. #endif
  419. }
  420. /* scan header */
  421. put_marker(&s->pb, SOS);
  422. put_bits(&s->pb, 16, 12); /* length */
  423. put_bits(&s->pb, 8, 3); /* 3 components */
  424. /* Y component */
  425. put_bits(&s->pb, 8, 1); /* index */
  426. put_bits(&s->pb, 4, 0); /* DC huffman table index */
  427. put_bits(&s->pb, 4, 0); /* AC huffman table index */
  428. /* Cb component */
  429. put_bits(&s->pb, 8, 2); /* index */
  430. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  431. put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
  432. /* Cr component */
  433. put_bits(&s->pb, 8, 3); /* index */
  434. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  435. put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
  436. put_bits(&s->pb, 8, (lossless && !ls) ? s->avctx->prediction_method+1 : 0); /* Ss (not used) */
  437. switch(s->avctx->codec_id){
  438. case CODEC_ID_MJPEG: put_bits(&s->pb, 8, 63); break; /* Se (not used) */
  439. case CODEC_ID_LJPEG: put_bits(&s->pb, 8, 0); break; /* not used */
  440. case CODEC_ID_JPEGLS: put_bits(&s->pb, 8, 1); break; /* ILV = line interleaved */
  441. default: assert(0);
  442. }
  443. put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
  444. //FIXME DC/AC entropy table selectors stuff in jpegls
  445. }
  446. static void escape_FF(MpegEncContext *s, int start)
  447. {
  448. int size= put_bits_count(&s->pb) - start*8;
  449. int i, ff_count;
  450. uint8_t *buf= s->pb.buf + start;
  451. int align= (-(size_t)(buf))&3;
  452. assert((size&7) == 0);
  453. size >>= 3;
  454. ff_count=0;
  455. for(i=0; i<size && i<align; i++){
  456. if(buf[i]==0xFF) ff_count++;
  457. }
  458. for(; i<size-15; i+=16){
  459. int acc, v;
  460. v= *(uint32_t*)(&buf[i]);
  461. acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  462. v= *(uint32_t*)(&buf[i+4]);
  463. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  464. v= *(uint32_t*)(&buf[i+8]);
  465. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  466. v= *(uint32_t*)(&buf[i+12]);
  467. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  468. acc>>=4;
  469. acc+= (acc>>16);
  470. acc+= (acc>>8);
  471. ff_count+= acc&0xFF;
  472. }
  473. for(; i<size; i++){
  474. if(buf[i]==0xFF) ff_count++;
  475. }
  476. if(ff_count==0) return;
  477. /* skip put bits */
  478. for(i=0; i<ff_count-3; i+=4)
  479. put_bits(&s->pb, 32, 0);
  480. put_bits(&s->pb, (ff_count-i)*8, 0);
  481. flush_put_bits(&s->pb);
  482. for(i=size-1; ff_count; i--){
  483. int v= buf[i];
  484. if(v==0xFF){
  485. //printf("%d %d\n", i, ff_count);
  486. buf[i+ff_count]= 0;
  487. ff_count--;
  488. }
  489. buf[i+ff_count]= v;
  490. }
  491. }
  492. void ff_mjpeg_stuffing(PutBitContext * pbc)
  493. {
  494. int length;
  495. length= (-put_bits_count(pbc))&7;
  496. if(length) put_bits(pbc, length, (1<<length)-1);
  497. }
  498. void mjpeg_picture_trailer(MpegEncContext *s)
  499. {
  500. ff_mjpeg_stuffing(&s->pb);
  501. flush_put_bits(&s->pb);
  502. assert((s->header_bits&7)==0);
  503. escape_FF(s, s->header_bits>>3);
  504. put_marker(&s->pb, EOI);
  505. }
  506. static inline void mjpeg_encode_dc(MpegEncContext *s, int val,
  507. uint8_t *huff_size, uint16_t *huff_code)
  508. {
  509. int mant, nbits;
  510. if (val == 0) {
  511. put_bits(&s->pb, huff_size[0], huff_code[0]);
  512. } else {
  513. mant = val;
  514. if (val < 0) {
  515. val = -val;
  516. mant--;
  517. }
  518. nbits= av_log2_16bit(val) + 1;
  519. put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
  520. put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
  521. }
  522. }
  523. static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
  524. {
  525. int mant, nbits, code, i, j;
  526. int component, dc, run, last_index, val;
  527. MJpegContext *m = s->mjpeg_ctx;
  528. uint8_t *huff_size_ac;
  529. uint16_t *huff_code_ac;
  530. /* DC coef */
  531. component = (n <= 3 ? 0 : (n&1) + 1);
  532. dc = block[0]; /* overflow is impossible */
  533. val = dc - s->last_dc[component];
  534. if (n < 4) {
  535. mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
  536. huff_size_ac = m->huff_size_ac_luminance;
  537. huff_code_ac = m->huff_code_ac_luminance;
  538. } else {
  539. mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  540. huff_size_ac = m->huff_size_ac_chrominance;
  541. huff_code_ac = m->huff_code_ac_chrominance;
  542. }
  543. s->last_dc[component] = dc;
  544. /* AC coefs */
  545. run = 0;
  546. last_index = s->block_last_index[n];
  547. for(i=1;i<=last_index;i++) {
  548. j = s->intra_scantable.permutated[i];
  549. val = block[j];
  550. if (val == 0) {
  551. run++;
  552. } else {
  553. while (run >= 16) {
  554. put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
  555. run -= 16;
  556. }
  557. mant = val;
  558. if (val < 0) {
  559. val = -val;
  560. mant--;
  561. }
  562. nbits= av_log2(val) + 1;
  563. code = (run << 4) | nbits;
  564. put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
  565. put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
  566. run = 0;
  567. }
  568. }
  569. /* output EOB only if not already 64 values */
  570. if (last_index < 63 || run != 0)
  571. put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
  572. }
  573. void mjpeg_encode_mb(MpegEncContext *s,
  574. DCTELEM block[6][64])
  575. {
  576. int i;
  577. for(i=0;i<5;i++) {
  578. encode_block(s, block[i], i);
  579. }
  580. if (s->chroma_format == CHROMA_420) {
  581. encode_block(s, block[5], 5);
  582. } else {
  583. encode_block(s, block[6], 6);
  584. encode_block(s, block[5], 5);
  585. encode_block(s, block[7], 7);
  586. }
  587. }
  588. static int encode_picture_lossless(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  589. MpegEncContext * const s = avctx->priv_data;
  590. MJpegContext * const m = s->mjpeg_ctx;
  591. AVFrame *pict = data;
  592. const int width= s->width;
  593. const int height= s->height;
  594. AVFrame * const p= (AVFrame*)&s->current_picture;
  595. const int predictor= avctx->prediction_method+1;
  596. init_put_bits(&s->pb, buf, buf_size);
  597. *p = *pict;
  598. p->pict_type= FF_I_TYPE;
  599. p->key_frame= 1;
  600. mjpeg_picture_header(s);
  601. s->header_bits= put_bits_count(&s->pb);
  602. if(avctx->pix_fmt == PIX_FMT_RGBA32){
  603. int x, y, i;
  604. const int linesize= p->linesize[0];
  605. uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
  606. int left[3], top[3], topleft[3];
  607. for(i=0; i<3; i++){
  608. buffer[0][i]= 1 << (9 - 1);
  609. }
  610. for(y = 0; y < height; y++) {
  611. const int modified_predictor= y ? predictor : 1;
  612. uint8_t *ptr = p->data[0] + (linesize * y);
  613. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
  614. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  615. return -1;
  616. }
  617. for(i=0; i<3; i++){
  618. top[i]= left[i]= topleft[i]= buffer[0][i];
  619. }
  620. for(x = 0; x < width; x++) {
  621. buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
  622. buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
  623. buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
  624. for(i=0;i<3;i++) {
  625. int pred, diff;
  626. PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
  627. topleft[i]= top[i];
  628. top[i]= buffer[x+1][i];
  629. left[i]= buffer[x][i];
  630. diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
  631. if(i==0)
  632. mjpeg_encode_dc(s, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
  633. else
  634. mjpeg_encode_dc(s, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  635. }
  636. }
  637. }
  638. }else{
  639. int mb_x, mb_y, i;
  640. const int mb_width = (width + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0];
  641. const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0];
  642. for(mb_y = 0; mb_y < mb_height; mb_y++) {
  643. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < mb_width * 4 * 3 * s->mjpeg_hsample[0] * s->mjpeg_vsample[0]){
  644. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  645. return -1;
  646. }
  647. for(mb_x = 0; mb_x < mb_width; mb_x++) {
  648. if(mb_x==0 || mb_y==0){
  649. for(i=0;i<3;i++) {
  650. uint8_t *ptr;
  651. int x, y, h, v, linesize;
  652. h = s->mjpeg_hsample[i];
  653. v = s->mjpeg_vsample[i];
  654. linesize= p->linesize[i];
  655. for(y=0; y<v; y++){
  656. for(x=0; x<h; x++){
  657. int pred;
  658. ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  659. if(y==0 && mb_y==0){
  660. if(x==0 && mb_x==0){
  661. pred= 128;
  662. }else{
  663. pred= ptr[-1];
  664. }
  665. }else{
  666. if(x==0 && mb_x==0){
  667. pred= ptr[-linesize];
  668. }else{
  669. PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
  670. }
  671. }
  672. if(i==0)
  673. mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
  674. else
  675. mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  676. }
  677. }
  678. }
  679. }else{
  680. for(i=0;i<3;i++) {
  681. uint8_t *ptr;
  682. int x, y, h, v, linesize;
  683. h = s->mjpeg_hsample[i];
  684. v = s->mjpeg_vsample[i];
  685. linesize= p->linesize[i];
  686. for(y=0; y<v; y++){
  687. for(x=0; x<h; x++){
  688. int pred;
  689. ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  690. //printf("%d %d %d %d %8X\n", mb_x, mb_y, x, y, ptr);
  691. PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
  692. if(i==0)
  693. mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
  694. else
  695. mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  696. }
  697. }
  698. }
  699. }
  700. }
  701. }
  702. }
  703. emms_c();
  704. mjpeg_picture_trailer(s);
  705. s->picture_number++;
  706. flush_put_bits(&s->pb);
  707. return pbBufPtr(&s->pb) - s->pb.buf;
  708. // return (put_bits_count(&f->pb)+7)/8;
  709. }
  710. #endif //CONFIG_ENCODERS
  711. /******************************************/
  712. /* decoding */
  713. #define MAX_COMPONENTS 4
  714. typedef struct MJpegDecodeContext {
  715. AVCodecContext *avctx;
  716. GetBitContext gb;
  717. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  718. int start_code; /* current start code */
  719. int buffer_size;
  720. uint8_t *buffer;
  721. int16_t quant_matrixes[4][64];
  722. VLC vlcs[2][4];
  723. int qscale[4]; ///< quantizer scale calculated from quant_matrixes
  724. int org_height; /* size given at codec init */
  725. int first_picture; /* true if decoding first picture */
  726. int interlaced; /* true if interlaced */
  727. int bottom_field; /* true if bottom field */
  728. int lossless;
  729. int ls;
  730. int progressive;
  731. int rgb;
  732. int rct; /* standard rct */
  733. int pegasus_rct; /* pegasus reversible colorspace transform */
  734. int bits; /* bits per component */
  735. int maxval;
  736. int near; ///< near lossless bound (si 0 for lossless)
  737. int t1,t2,t3;
  738. int reset; ///< context halfing intervall ?rename
  739. int width, height;
  740. int mb_width, mb_height;
  741. int nb_components;
  742. int component_id[MAX_COMPONENTS];
  743. int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */
  744. int v_count[MAX_COMPONENTS];
  745. int comp_index[MAX_COMPONENTS];
  746. int dc_index[MAX_COMPONENTS];
  747. int ac_index[MAX_COMPONENTS];
  748. int nb_blocks[MAX_COMPONENTS];
  749. int h_scount[MAX_COMPONENTS];
  750. int v_scount[MAX_COMPONENTS];
  751. int h_max, v_max; /* maximum h and v counts */
  752. int quant_index[4]; /* quant table index for each component */
  753. int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */
  754. AVFrame picture; /* picture structure */
  755. int linesize[MAX_COMPONENTS]; ///< linesize << interlaced
  756. int8_t *qscale_table;
  757. DECLARE_ALIGNED_8(DCTELEM, block[64]);
  758. ScanTable scantable;
  759. void (*idct_put)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  760. void (*idct_add)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  761. int restart_interval;
  762. int restart_count;
  763. int buggy_avid;
  764. int cs_itu601;
  765. int interlace_polarity;
  766. int mjpb_skiptosod;
  767. int cur_scan; /* current scan, used by JPEG-LS */
  768. } MJpegDecodeContext;
  769. #include "jpeg_ls.c" //FIXME make jpeg-ls more independant
  770. static int mjpeg_decode_dht(MJpegDecodeContext *s);
  771. static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table,
  772. int nb_codes, int use_static, int is_ac)
  773. {
  774. uint8_t huff_size[256+16];
  775. uint16_t huff_code[256+16];
  776. assert(nb_codes <= 256);
  777. memset(huff_size, 0, sizeof(huff_size));
  778. build_huffman_codes(huff_size, huff_code, bits_table, val_table);
  779. if(is_ac){
  780. memmove(huff_size+16, huff_size, sizeof(uint8_t)*nb_codes);
  781. memmove(huff_code+16, huff_code, sizeof(uint16_t)*nb_codes);
  782. memset(huff_size, 0, sizeof(uint8_t)*16);
  783. memset(huff_code, 0, sizeof(uint16_t)*16);
  784. nb_codes += 16;
  785. }
  786. return init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2, use_static);
  787. }
  788. static int mjpeg_decode_init(AVCodecContext *avctx)
  789. {
  790. MJpegDecodeContext *s = avctx->priv_data;
  791. MpegEncContext s2;
  792. memset(s, 0, sizeof(MJpegDecodeContext));
  793. s->avctx = avctx;
  794. /* ugly way to get the idct & scantable FIXME */
  795. memset(&s2, 0, sizeof(MpegEncContext));
  796. s2.avctx= avctx;
  797. // s2->out_format = FMT_MJPEG;
  798. dsputil_init(&s2.dsp, avctx);
  799. DCT_common_init(&s2);
  800. s->scantable= s2.intra_scantable;
  801. s->idct_put= s2.dsp.idct_put;
  802. s->idct_add= s2.dsp.idct_add;
  803. s->mpeg_enc_ctx_allocated = 0;
  804. s->buffer_size = 0;
  805. s->buffer = NULL;
  806. s->start_code = -1;
  807. s->first_picture = 1;
  808. s->org_height = avctx->coded_height;
  809. build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12, 0, 0);
  810. build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12, 0, 0);
  811. build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251, 0, 1);
  812. build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251, 0, 1);
  813. if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
  814. {
  815. av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n");
  816. init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8);
  817. mjpeg_decode_dht(s);
  818. /* should check for error - but dunno */
  819. }
  820. return 0;
  821. }
  822. /**
  823. * finds the end of the current frame in the bitstream.
  824. * @return the position of the first byte of the next frame, or -1
  825. */
  826. static int find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
  827. int vop_found, i;
  828. uint16_t state;
  829. vop_found= pc->frame_start_found;
  830. state= pc->state;
  831. i=0;
  832. if(!vop_found){
  833. for(i=0; i<buf_size; i++){
  834. state= (state<<8) | buf[i];
  835. if(state == 0xFFD8){
  836. i++;
  837. vop_found=1;
  838. break;
  839. }
  840. }
  841. }
  842. if(vop_found){
  843. /* EOF considered as end of frame */
  844. if (buf_size == 0)
  845. return 0;
  846. for(; i<buf_size; i++){
  847. state= (state<<8) | buf[i];
  848. if(state == 0xFFD8){
  849. pc->frame_start_found=0;
  850. pc->state=0;
  851. return i-1;
  852. }
  853. }
  854. }
  855. pc->frame_start_found= vop_found;
  856. pc->state= state;
  857. return END_NOT_FOUND;
  858. }
  859. static int jpeg_parse(AVCodecParserContext *s,
  860. AVCodecContext *avctx,
  861. uint8_t **poutbuf, int *poutbuf_size,
  862. const uint8_t *buf, int buf_size)
  863. {
  864. ParseContext *pc = s->priv_data;
  865. int next;
  866. next= find_frame_end(pc, buf, buf_size);
  867. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  868. *poutbuf = NULL;
  869. *poutbuf_size = 0;
  870. return buf_size;
  871. }
  872. *poutbuf = (uint8_t *)buf;
  873. *poutbuf_size = buf_size;
  874. return next;
  875. }
  876. /* quantize tables */
  877. static int mjpeg_decode_dqt(MJpegDecodeContext *s)
  878. {
  879. int len, index, i, j;
  880. len = get_bits(&s->gb, 16) - 2;
  881. while (len >= 65) {
  882. /* only 8 bit precision handled */
  883. if (get_bits(&s->gb, 4) != 0)
  884. {
  885. dprintf("dqt: 16bit precision\n");
  886. return -1;
  887. }
  888. index = get_bits(&s->gb, 4);
  889. if (index >= 4)
  890. return -1;
  891. dprintf("index=%d\n", index);
  892. /* read quant table */
  893. for(i=0;i<64;i++) {
  894. j = s->scantable.permutated[i];
  895. s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
  896. }
  897. //XXX FIXME finetune, and perhaps add dc too
  898. s->qscale[index]= FFMAX(
  899. s->quant_matrixes[index][s->scantable.permutated[1]],
  900. s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1;
  901. dprintf("qscale[%d]: %d\n", index, s->qscale[index]);
  902. len -= 65;
  903. }
  904. return 0;
  905. }
  906. /* decode huffman tables and build VLC decoders */
  907. static int mjpeg_decode_dht(MJpegDecodeContext *s)
  908. {
  909. int len, index, i, class, n, v, code_max;
  910. uint8_t bits_table[17];
  911. uint8_t val_table[256];
  912. len = get_bits(&s->gb, 16) - 2;
  913. while (len > 0) {
  914. if (len < 17)
  915. return -1;
  916. class = get_bits(&s->gb, 4);
  917. if (class >= 2)
  918. return -1;
  919. index = get_bits(&s->gb, 4);
  920. if (index >= 4)
  921. return -1;
  922. n = 0;
  923. for(i=1;i<=16;i++) {
  924. bits_table[i] = get_bits(&s->gb, 8);
  925. n += bits_table[i];
  926. }
  927. len -= 17;
  928. if (len < n || n > 256)
  929. return -1;
  930. code_max = 0;
  931. for(i=0;i<n;i++) {
  932. v = get_bits(&s->gb, 8);
  933. if (v > code_max)
  934. code_max = v;
  935. val_table[i] = v;
  936. }
  937. len -= n;
  938. /* build VLC and flush previous vlc if present */
  939. free_vlc(&s->vlcs[class][index]);
  940. dprintf("class=%d index=%d nb_codes=%d\n",
  941. class, index, code_max + 1);
  942. if(build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1, 0, class > 0) < 0){
  943. return -1;
  944. }
  945. }
  946. return 0;
  947. }
  948. static int mjpeg_decode_sof(MJpegDecodeContext *s)
  949. {
  950. int len, nb_components, i, width, height, pix_fmt_id;
  951. /* XXX: verify len field validity */
  952. len = get_bits(&s->gb, 16);
  953. s->bits= get_bits(&s->gb, 8);
  954. if(s->pegasus_rct) s->bits=9;
  955. if(s->bits==9 && !s->pegasus_rct) s->rct=1; //FIXME ugly
  956. if (s->bits != 8 && !s->lossless){
  957. av_log(s->avctx, AV_LOG_ERROR, "only 8 bits/component accepted\n");
  958. return -1;
  959. }
  960. height = get_bits(&s->gb, 16);
  961. width = get_bits(&s->gb, 16);
  962. dprintf("sof0: picture: %dx%d\n", width, height);
  963. if(avcodec_check_dimensions(s->avctx, width, height))
  964. return -1;
  965. nb_components = get_bits(&s->gb, 8);
  966. if (nb_components <= 0 ||
  967. nb_components > MAX_COMPONENTS)
  968. return -1;
  969. if (s->ls && !(s->bits <= 8 || nb_components == 1)){
  970. av_log(s->avctx, AV_LOG_ERROR, "only <= 8 bits/component or 16-bit gray accepted for JPEG-LS\n");
  971. return -1;
  972. }
  973. s->nb_components = nb_components;
  974. s->h_max = 1;
  975. s->v_max = 1;
  976. for(i=0;i<nb_components;i++) {
  977. /* component id */
  978. s->component_id[i] = get_bits(&s->gb, 8) - 1;
  979. s->h_count[i] = get_bits(&s->gb, 4);
  980. s->v_count[i] = get_bits(&s->gb, 4);
  981. /* compute hmax and vmax (only used in interleaved case) */
  982. if (s->h_count[i] > s->h_max)
  983. s->h_max = s->h_count[i];
  984. if (s->v_count[i] > s->v_max)
  985. s->v_max = s->v_count[i];
  986. s->quant_index[i] = get_bits(&s->gb, 8);
  987. if (s->quant_index[i] >= 4)
  988. return -1;
  989. dprintf("component %d %d:%d id: %d quant:%d\n", i, s->h_count[i],
  990. s->v_count[i], s->component_id[i], s->quant_index[i]);
  991. }
  992. if(s->ls && (s->h_max > 1 || s->v_max > 1)) {
  993. av_log(s->avctx, AV_LOG_ERROR, "Subsampling in JPEG-LS is not supported.\n");
  994. return -1;
  995. }
  996. if(s->v_max==1 && s->h_max==1 && s->lossless==1) s->rgb=1;
  997. /* if different size, realloc/alloc picture */
  998. /* XXX: also check h_count and v_count */
  999. if (width != s->width || height != s->height) {
  1000. av_freep(&s->qscale_table);
  1001. s->width = width;
  1002. s->height = height;
  1003. /* test interlaced mode */
  1004. if (s->first_picture &&
  1005. s->org_height != 0 &&
  1006. s->height < ((s->org_height * 3) / 4)) {
  1007. s->interlaced = 1;
  1008. // s->bottom_field = (s->interlace_polarity) ? 1 : 0;
  1009. s->bottom_field = 0;
  1010. height *= 2;
  1011. }
  1012. avcodec_set_dimensions(s->avctx, width, height);
  1013. s->qscale_table= av_mallocz((s->width+15)/16);
  1014. s->first_picture = 0;
  1015. }
  1016. if(s->interlaced && s->bottom_field)
  1017. return 0;
  1018. /* XXX: not complete test ! */
  1019. pix_fmt_id = (s->h_count[0] << 20) | (s->v_count[0] << 16) |
  1020. (s->h_count[1] << 12) | (s->v_count[1] << 8) |
  1021. (s->h_count[2] << 4) | s->v_count[2];
  1022. dprintf("pix fmt id %x\n", pix_fmt_id);
  1023. switch(pix_fmt_id){
  1024. case 0x222222:
  1025. case 0x111111:
  1026. if(s->rgb){
  1027. s->avctx->pix_fmt = PIX_FMT_RGBA32;
  1028. }else if(s->nb_components==3)
  1029. s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV444P : PIX_FMT_YUVJ444P;
  1030. else
  1031. s->avctx->pix_fmt = PIX_FMT_GRAY8;
  1032. break;
  1033. case 0x211111:
  1034. case 0x221212:
  1035. s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV422P : PIX_FMT_YUVJ422P;
  1036. break;
  1037. default:
  1038. case 0x221111:
  1039. s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420P;
  1040. break;
  1041. }
  1042. if(s->ls){
  1043. if(s->nb_components > 1)
  1044. s->avctx->pix_fmt = PIX_FMT_RGB24;
  1045. else if(s->bits <= 8)
  1046. s->avctx->pix_fmt = PIX_FMT_GRAY8;
  1047. else
  1048. s->avctx->pix_fmt = PIX_FMT_GRAY16;
  1049. }
  1050. if(s->picture.data[0])
  1051. s->avctx->release_buffer(s->avctx, &s->picture);
  1052. s->picture.reference= 0;
  1053. if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
  1054. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1055. return -1;
  1056. }
  1057. s->picture.pict_type= I_TYPE;
  1058. s->picture.key_frame= 1;
  1059. for(i=0; i<3; i++){
  1060. s->linesize[i]= s->picture.linesize[i] << s->interlaced;
  1061. }
  1062. // printf("%d %d %d %d %d %d\n", s->width, s->height, s->linesize[0], s->linesize[1], s->interlaced, s->avctx->height);
  1063. if (len != (8+(3*nb_components)))
  1064. {
  1065. dprintf("decode_sof0: error, len(%d) mismatch\n", len);
  1066. }
  1067. /* totally blank picture as progressive JPEG will only add details to it */
  1068. if(s->progressive){
  1069. memset(s->picture.data[0], 0, s->picture.linesize[0] * s->height);
  1070. memset(s->picture.data[1], 0, s->picture.linesize[1] * s->height >> (s->v_max - s->v_count[1]));
  1071. memset(s->picture.data[2], 0, s->picture.linesize[2] * s->height >> (s->v_max - s->v_count[2]));
  1072. }
  1073. return 0;
  1074. }
  1075. static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
  1076. {
  1077. int code;
  1078. code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
  1079. if (code < 0)
  1080. {
  1081. dprintf("mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index,
  1082. &s->vlcs[0][dc_index]);
  1083. return 0xffff;
  1084. }
  1085. if(code)
  1086. return get_xbits(&s->gb, code);
  1087. else
  1088. return 0;
  1089. }
  1090. /* decode block and dequantize */
  1091. static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
  1092. int component, int dc_index, int ac_index, int16_t *quant_matrix)
  1093. {
  1094. int code, i, j, level, val;
  1095. /* DC coef */
  1096. val = mjpeg_decode_dc(s, dc_index);
  1097. if (val == 0xffff) {
  1098. dprintf("error dc\n");
  1099. return -1;
  1100. }
  1101. val = val * quant_matrix[0] + s->last_dc[component];
  1102. s->last_dc[component] = val;
  1103. block[0] = val;
  1104. /* AC coefs */
  1105. i = 0;
  1106. {OPEN_READER(re, &s->gb)
  1107. for(;;) {
  1108. UPDATE_CACHE(re, &s->gb);
  1109. GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2)
  1110. /* EOB */
  1111. if (code == 0x10)
  1112. break;
  1113. i += ((unsigned)code) >> 4;
  1114. if(code != 0x100){
  1115. code &= 0xf;
  1116. if(code > MIN_CACHE_BITS - 16){
  1117. UPDATE_CACHE(re, &s->gb)
  1118. }
  1119. {
  1120. int cache=GET_CACHE(re,&s->gb);
  1121. int sign=(~cache)>>31;
  1122. level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
  1123. }
  1124. LAST_SKIP_BITS(re, &s->gb, code)
  1125. if (i >= 63) {
  1126. if(i == 63){
  1127. j = s->scantable.permutated[63];
  1128. block[j] = level * quant_matrix[j];
  1129. break;
  1130. }
  1131. dprintf("error count: %d\n", i);
  1132. return -1;
  1133. }
  1134. j = s->scantable.permutated[i];
  1135. block[j] = level * quant_matrix[j];
  1136. }
  1137. }
  1138. CLOSE_READER(re, &s->gb)}
  1139. return 0;
  1140. }
  1141. /* decode block and dequantize - progressive JPEG version */
  1142. static int decode_block_progressive(MJpegDecodeContext *s, DCTELEM *block,
  1143. int component, int dc_index, int ac_index, int16_t *quant_matrix,
  1144. int ss, int se, int Ah, int Al, int *EOBRUN)
  1145. {
  1146. int code, i, j, level, val, run;
  1147. /* DC coef */
  1148. if(!ss){
  1149. val = mjpeg_decode_dc(s, dc_index);
  1150. if (val == 0xffff) {
  1151. dprintf("error dc\n");
  1152. return -1;
  1153. }
  1154. val = (val * quant_matrix[0] << Al) + s->last_dc[component];
  1155. }else
  1156. val = 0;
  1157. s->last_dc[component] = val;
  1158. block[0] = val;
  1159. if(!se) return 0;
  1160. /* AC coefs */
  1161. if(*EOBRUN){
  1162. (*EOBRUN)--;
  1163. return 0;
  1164. }
  1165. {OPEN_READER(re, &s->gb)
  1166. for(i=ss;;i++) {
  1167. UPDATE_CACHE(re, &s->gb);
  1168. GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2)
  1169. /* Progressive JPEG use AC coeffs from zero and this decoder sets offset 16 by default */
  1170. code -= 16;
  1171. if(code & 0xF) {
  1172. i += ((unsigned) code) >> 4;
  1173. code &= 0xf;
  1174. if(code > MIN_CACHE_BITS - 16){
  1175. UPDATE_CACHE(re, &s->gb)
  1176. }
  1177. {
  1178. int cache=GET_CACHE(re,&s->gb);
  1179. int sign=(~cache)>>31;
  1180. level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
  1181. }
  1182. LAST_SKIP_BITS(re, &s->gb, code)
  1183. if (i >= se) {
  1184. if(i == se){
  1185. j = s->scantable.permutated[se];
  1186. block[j] = level * quant_matrix[j] << Al;
  1187. break;
  1188. }
  1189. dprintf("error count: %d\n", i);
  1190. return -1;
  1191. }
  1192. j = s->scantable.permutated[i];
  1193. block[j] = level * quant_matrix[j] << Al;
  1194. }else{
  1195. run = ((unsigned) code) >> 4;
  1196. if(run == 0xF){// ZRL - skip 15 coefficients
  1197. i += 15;
  1198. }else{
  1199. val = run;
  1200. run = (1 << run);
  1201. UPDATE_CACHE(re, &s->gb);
  1202. run += (GET_CACHE(re, &s->gb) >> (32 - val)) & (run - 1);
  1203. if(val)
  1204. LAST_SKIP_BITS(re, &s->gb, val);
  1205. *EOBRUN = run - 1;
  1206. break;
  1207. }
  1208. }
  1209. }
  1210. CLOSE_READER(re, &s->gb)}
  1211. return 0;
  1212. }
  1213. static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor, int point_transform){
  1214. int i, mb_x, mb_y;
  1215. uint16_t buffer[32768][4];
  1216. int left[3], top[3], topleft[3];
  1217. const int linesize= s->linesize[0];
  1218. const int mask= (1<<s->bits)-1;
  1219. if((unsigned)s->mb_width > 32768) //dynamic alloc
  1220. return -1;
  1221. for(i=0; i<3; i++){
  1222. buffer[0][i]= 1 << (s->bits + point_transform - 1);
  1223. }
  1224. for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1225. const int modified_predictor= mb_y ? predictor : 1;
  1226. uint8_t *ptr = s->picture.data[0] + (linesize * mb_y);
  1227. if (s->interlaced && s->bottom_field)
  1228. ptr += linesize >> 1;
  1229. for(i=0; i<3; i++){
  1230. top[i]= left[i]= topleft[i]= buffer[0][i];
  1231. }
  1232. for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1233. if (s->restart_interval && !s->restart_count)
  1234. s->restart_count = s->restart_interval;
  1235. for(i=0;i<3;i++) {
  1236. int pred;
  1237. topleft[i]= top[i];
  1238. top[i]= buffer[mb_x][i];
  1239. PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
  1240. left[i]=
  1241. buffer[mb_x][i]= mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform));
  1242. }
  1243. if (s->restart_interval && !--s->restart_count) {
  1244. align_get_bits(&s->gb);
  1245. skip_bits(&s->gb, 16); /* skip RSTn */
  1246. }
  1247. }
  1248. if(s->rct){
  1249. for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1250. ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200)>>2);
  1251. ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1];
  1252. ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1];
  1253. }
  1254. }else if(s->pegasus_rct){
  1255. for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1256. ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2])>>2);
  1257. ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1];
  1258. ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1];
  1259. }
  1260. }else{
  1261. for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1262. ptr[4*mb_x+0] = buffer[mb_x][0];
  1263. ptr[4*mb_x+1] = buffer[mb_x][1];
  1264. ptr[4*mb_x+2] = buffer[mb_x][2];
  1265. }
  1266. }
  1267. }
  1268. return 0;
  1269. }
  1270. static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, int point_transform){
  1271. int i, mb_x, mb_y;
  1272. const int nb_components=3;
  1273. for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1274. for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1275. if (s->restart_interval && !s->restart_count)
  1276. s->restart_count = s->restart_interval;
  1277. if(mb_x==0 || mb_y==0 || s->interlaced){
  1278. for(i=0;i<nb_components;i++) {
  1279. uint8_t *ptr;
  1280. int n, h, v, x, y, c, j, linesize;
  1281. n = s->nb_blocks[i];
  1282. c = s->comp_index[i];
  1283. h = s->h_scount[i];
  1284. v = s->v_scount[i];
  1285. x = 0;
  1286. y = 0;
  1287. linesize= s->linesize[c];
  1288. for(j=0; j<n; j++) {
  1289. int pred;
  1290. ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  1291. if(y==0 && mb_y==0){
  1292. if(x==0 && mb_x==0){
  1293. pred= 128 << point_transform;
  1294. }else{
  1295. pred= ptr[-1];
  1296. }
  1297. }else{
  1298. if(x==0 && mb_x==0){
  1299. pred= ptr[-linesize];
  1300. }else{
  1301. PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
  1302. }
  1303. }
  1304. if (s->interlaced && s->bottom_field)
  1305. ptr += linesize >> 1;
  1306. *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
  1307. if (++x == h) {
  1308. x = 0;
  1309. y++;
  1310. }
  1311. }
  1312. }
  1313. }else{
  1314. for(i=0;i<nb_components;i++) {
  1315. uint8_t *ptr;
  1316. int n, h, v, x, y, c, j, linesize;
  1317. n = s->nb_blocks[i];
  1318. c = s->comp_index[i];
  1319. h = s->h_scount[i];
  1320. v = s->v_scount[i];
  1321. x = 0;
  1322. y = 0;
  1323. linesize= s->linesize[c];
  1324. for(j=0; j<n; j++) {
  1325. int pred;
  1326. ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  1327. PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
  1328. *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
  1329. if (++x == h) {
  1330. x = 0;
  1331. y++;
  1332. }
  1333. }
  1334. }
  1335. }
  1336. if (s->restart_interval && !--s->restart_count) {
  1337. align_get_bits(&s->gb);
  1338. skip_bits(&s->gb, 16); /* skip RSTn */
  1339. }
  1340. }
  1341. }
  1342. return 0;
  1343. }
  1344. static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int ss, int se, int Ah, int Al){
  1345. int i, mb_x, mb_y;
  1346. int EOBRUN = 0;
  1347. if(Ah) return 0; /* TODO decode refinement planes too */
  1348. for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1349. for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1350. if (s->restart_interval && !s->restart_count)
  1351. s->restart_count = s->restart_interval;
  1352. for(i=0;i<nb_components;i++) {
  1353. uint8_t *ptr;
  1354. int n, h, v, x, y, c, j;
  1355. n = s->nb_blocks[i];
  1356. c = s->comp_index[i];
  1357. h = s->h_scount[i];
  1358. v = s->v_scount[i];
  1359. x = 0;
  1360. y = 0;
  1361. for(j=0;j<n;j++) {
  1362. memset(s->block, 0, sizeof(s->block));
  1363. if (!s->progressive && decode_block(s, s->block, i,
  1364. s->dc_index[i], s->ac_index[i],
  1365. s->quant_matrixes[ s->quant_index[c] ]) < 0) {
  1366. dprintf("error y=%d x=%d\n", mb_y, mb_x);
  1367. return -1;
  1368. }
  1369. if (s->progressive && decode_block_progressive(s, s->block, i,
  1370. s->dc_index[i], s->ac_index[i],
  1371. s->quant_matrixes[ s->quant_index[c] ], ss, se, Ah, Al, &EOBRUN) < 0) {
  1372. dprintf("error y=%d x=%d\n", mb_y, mb_x);
  1373. return -1;
  1374. }
  1375. // dprintf("mb: %d %d processed\n", mb_y, mb_x);
  1376. ptr = s->picture.data[c] +
  1377. (((s->linesize[c] * (v * mb_y + y) * 8) +
  1378. (h * mb_x + x) * 8) >> s->avctx->lowres);
  1379. if (s->interlaced && s->bottom_field)
  1380. ptr += s->linesize[c] >> 1;
  1381. //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);
  1382. if(!s->progressive)
  1383. s->idct_put(ptr, s->linesize[c], s->block);
  1384. else
  1385. s->idct_add(ptr, s->linesize[c], s->block);
  1386. if (++x == h) {
  1387. x = 0;
  1388. y++;
  1389. }
  1390. }
  1391. }
  1392. /* (< 1350) buggy workaround for Spectralfan.mov, should be fixed */
  1393. if (s->restart_interval && (s->restart_interval < 1350) &&
  1394. !--s->restart_count) {
  1395. align_get_bits(&s->gb);
  1396. skip_bits(&s->gb, 16); /* skip RSTn */
  1397. for (i=0; i<nb_components; i++) /* reset dc */
  1398. s->last_dc[i] = 1024;
  1399. }
  1400. }
  1401. }
  1402. return 0;
  1403. }
  1404. static int mjpeg_decode_sos(MJpegDecodeContext *s)
  1405. {
  1406. int len, nb_components, i, h, v, predictor, point_transform;
  1407. int vmax, hmax, index, id;
  1408. const int block_size= s->lossless ? 1 : 8;
  1409. int ilv, prev_shift;
  1410. /* XXX: verify len field validity */
  1411. len = get_bits(&s->gb, 16);
  1412. nb_components = get_bits(&s->gb, 8);
  1413. if (len != 6+2*nb_components)
  1414. {
  1415. dprintf("decode_sos: invalid len (%d)\n", len);
  1416. return -1;
  1417. }
  1418. /* XXX: only interleaved scan accepted */
  1419. if ((nb_components != s->nb_components) && !s->ls && !s->progressive)
  1420. {
  1421. dprintf("decode_sos: components(%d) mismatch\n", nb_components);
  1422. return -1;
  1423. }
  1424. vmax = 0;
  1425. hmax = 0;
  1426. for(i=0;i<nb_components;i++) {
  1427. id = get_bits(&s->gb, 8) - 1;
  1428. dprintf("component: %d\n", id);
  1429. /* find component index */
  1430. for(index=0;index<s->nb_components;index++)
  1431. if (id == s->component_id[index])
  1432. break;
  1433. if (index == s->nb_components)
  1434. {
  1435. dprintf("decode_sos: index(%d) out of components\n", index);
  1436. return -1;
  1437. }
  1438. s->comp_index[i] = index;
  1439. s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
  1440. s->h_scount[i] = s->h_count[index];
  1441. s->v_scount[i] = s->v_count[index];
  1442. s->dc_index[i] = get_bits(&s->gb, 4);
  1443. s->ac_index[i] = get_bits(&s->gb, 4);
  1444. if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
  1445. s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
  1446. goto out_of_range;
  1447. #if 0 //buggy
  1448. switch(s->start_code)
  1449. {
  1450. case SOF0:
  1451. if (dc_index[i] > 1 || ac_index[i] > 1)
  1452. goto out_of_range;
  1453. break;
  1454. case SOF1:
  1455. case SOF2:
  1456. if (dc_index[i] > 3 || ac_index[i] > 3)
  1457. goto out_of_range;
  1458. break;
  1459. case SOF3:
  1460. if (dc_index[i] > 3 || ac_index[i] != 0)
  1461. goto out_of_range;
  1462. break;
  1463. }
  1464. #endif
  1465. }
  1466. predictor= get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
  1467. ilv= get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */
  1468. prev_shift = get_bits(&s->gb, 4); /* Ah */
  1469. point_transform= get_bits(&s->gb, 4); /* Al */
  1470. for(i=0;i<nb_components;i++)
  1471. s->last_dc[i] = 1024;
  1472. if (nb_components > 1) {
  1473. /* interleaved stream */
  1474. s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
  1475. s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
  1476. } else if(!s->ls) { /* skip this for JPEG-LS */
  1477. h = s->h_max / s->h_scount[s->comp_index[0]];
  1478. v = s->v_max / s->v_scount[s->comp_index[0]];
  1479. s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
  1480. s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
  1481. s->nb_blocks[0] = 1;
  1482. s->h_scount[0] = 1;
  1483. s->v_scount[0] = 1;
  1484. }
  1485. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1486. 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" : "",
  1487. predictor, point_transform, ilv, s->bits,
  1488. s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""));
  1489. /* mjpeg-b can have padding bytes between sos and image data, skip them */
  1490. for (i = s->mjpb_skiptosod; i > 0; i--)
  1491. skip_bits(&s->gb, 8);
  1492. if(s->lossless){
  1493. if(s->ls){
  1494. // for(){
  1495. // reset_ls_coding_parameters(s, 0);
  1496. ls_decode_picture(s, predictor, point_transform, ilv);
  1497. }else{
  1498. if(s->rgb){
  1499. if(ljpeg_decode_rgb_scan(s, predictor, point_transform) < 0)
  1500. return -1;
  1501. }else{
  1502. if(ljpeg_decode_yuv_scan(s, predictor, point_transform) < 0)
  1503. return -1;
  1504. }
  1505. }
  1506. }else{
  1507. if(mjpeg_decode_scan(s, nb_components, predictor, ilv, prev_shift, point_transform) < 0)
  1508. return -1;
  1509. }
  1510. emms_c();
  1511. return 0;
  1512. out_of_range:
  1513. dprintf("decode_sos: ac/dc index out of range\n");
  1514. return -1;
  1515. }
  1516. static int mjpeg_decode_dri(MJpegDecodeContext *s)
  1517. {
  1518. if (get_bits(&s->gb, 16) != 4)
  1519. return -1;
  1520. s->restart_interval = get_bits(&s->gb, 16);
  1521. s->restart_count = 0;
  1522. dprintf("restart interval: %d\n", s->restart_interval);
  1523. return 0;
  1524. }
  1525. static int mjpeg_decode_app(MJpegDecodeContext *s)
  1526. {
  1527. int len, id;
  1528. len = get_bits(&s->gb, 16);
  1529. if (len < 5)
  1530. return -1;
  1531. if(8*len + get_bits_count(&s->gb) > s->gb.size_in_bits)
  1532. return -1;
  1533. id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
  1534. id = be2me_32(id);
  1535. len -= 6;
  1536. if(s->avctx->debug & FF_DEBUG_STARTCODE){
  1537. av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id);
  1538. }
  1539. /* buggy AVID, it puts EOI only at every 10th frame */
  1540. /* also this fourcc is used by non-avid files too, it holds some
  1541. informations, but it's always present in AVID creates files */
  1542. if (id == ff_get_fourcc("AVI1"))
  1543. {
  1544. /* structure:
  1545. 4bytes AVI1
  1546. 1bytes polarity
  1547. 1bytes always zero
  1548. 4bytes field_size
  1549. 4bytes field_size_less_padding
  1550. */
  1551. s->buggy_avid = 1;
  1552. // if (s->first_picture)
  1553. // printf("mjpeg: workarounding buggy AVID\n");
  1554. s->interlace_polarity = get_bits(&s->gb, 8);
  1555. #if 0
  1556. skip_bits(&s->gb, 8);
  1557. skip_bits(&s->gb, 32);
  1558. skip_bits(&s->gb, 32);
  1559. len -= 10;
  1560. #endif
  1561. // if (s->interlace_polarity)
  1562. // printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity);
  1563. goto out;
  1564. }
  1565. // len -= 2;
  1566. if (id == ff_get_fourcc("JFIF"))
  1567. {
  1568. int t_w, t_h, v1, v2;
  1569. skip_bits(&s->gb, 8); /* the trailing zero-byte */
  1570. v1= get_bits(&s->gb, 8);
  1571. v2= get_bits(&s->gb, 8);
  1572. skip_bits(&s->gb, 8);
  1573. s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 16);
  1574. s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 16);
  1575. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1576. av_log(s->avctx, AV_LOG_INFO, "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
  1577. v1, v2,
  1578. s->avctx->sample_aspect_ratio.num,
  1579. s->avctx->sample_aspect_ratio.den
  1580. );
  1581. t_w = get_bits(&s->gb, 8);
  1582. t_h = get_bits(&s->gb, 8);
  1583. if (t_w && t_h)
  1584. {
  1585. /* skip thumbnail */
  1586. if (len-10-(t_w*t_h*3) > 0)
  1587. len -= t_w*t_h*3;
  1588. }
  1589. len -= 10;
  1590. goto out;
  1591. }
  1592. if (id == ff_get_fourcc("Adob") && (get_bits(&s->gb, 8) == 'e'))
  1593. {
  1594. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1595. av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n");
  1596. skip_bits(&s->gb, 16); /* version */
  1597. skip_bits(&s->gb, 16); /* flags0 */
  1598. skip_bits(&s->gb, 16); /* flags1 */
  1599. skip_bits(&s->gb, 8); /* transform */
  1600. len -= 7;
  1601. goto out;
  1602. }
  1603. if (id == ff_get_fourcc("LJIF")){
  1604. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1605. av_log(s->avctx, AV_LOG_INFO, "Pegasus lossless jpeg header found\n");
  1606. skip_bits(&s->gb, 16); /* version ? */
  1607. skip_bits(&s->gb, 16); /* unknwon always 0? */
  1608. skip_bits(&s->gb, 16); /* unknwon always 0? */
  1609. skip_bits(&s->gb, 16); /* unknwon always 0? */
  1610. switch( get_bits(&s->gb, 8)){
  1611. case 1:
  1612. s->rgb= 1;
  1613. s->pegasus_rct=0;
  1614. break;
  1615. case 2:
  1616. s->rgb= 1;
  1617. s->pegasus_rct=1;
  1618. break;
  1619. default:
  1620. av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n");
  1621. }
  1622. len -= 9;
  1623. goto out;
  1624. }
  1625. /* Apple MJPEG-A */
  1626. if ((s->start_code == APP1) && (len > (0x28 - 8)))
  1627. {
  1628. id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
  1629. id = be2me_32(id);
  1630. len -= 4;
  1631. if (id == ff_get_fourcc("mjpg")) /* Apple MJPEG-A */
  1632. {
  1633. #if 0
  1634. skip_bits(&s->gb, 32); /* field size */
  1635. skip_bits(&s->gb, 32); /* pad field size */
  1636. skip_bits(&s->gb, 32); /* next off */
  1637. skip_bits(&s->gb, 32); /* quant off */
  1638. skip_bits(&s->gb, 32); /* huff off */
  1639. skip_bits(&s->gb, 32); /* image off */
  1640. skip_bits(&s->gb, 32); /* scan off */
  1641. skip_bits(&s->gb, 32); /* data off */
  1642. #endif
  1643. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1644. av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
  1645. }
  1646. }
  1647. out:
  1648. /* slow but needed for extreme adobe jpegs */
  1649. if (len < 0)
  1650. av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error, decode_app parser read over the end\n");
  1651. while(--len > 0)
  1652. skip_bits(&s->gb, 8);
  1653. return 0;
  1654. }
  1655. static int mjpeg_decode_com(MJpegDecodeContext *s)
  1656. {
  1657. int len = get_bits(&s->gb, 16);
  1658. if (len >= 2 && 8*len - 16 + get_bits_count(&s->gb) <= s->gb.size_in_bits) {
  1659. char *cbuf = av_malloc(len - 1);
  1660. if (cbuf) {
  1661. int i;
  1662. for (i = 0; i < len - 2; i++)
  1663. cbuf[i] = get_bits(&s->gb, 8);
  1664. if (i > 0 && cbuf[i-1] == '\n')
  1665. cbuf[i-1] = 0;
  1666. else
  1667. cbuf[i] = 0;
  1668. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1669. av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf);
  1670. /* buggy avid, it puts EOI only at every 10th frame */
  1671. if (!strcmp(cbuf, "AVID"))
  1672. {
  1673. s->buggy_avid = 1;
  1674. // if (s->first_picture)
  1675. // printf("mjpeg: workarounding buggy AVID\n");
  1676. }
  1677. else if(!strcmp(cbuf, "CS=ITU601")){
  1678. s->cs_itu601= 1;
  1679. }
  1680. av_free(cbuf);
  1681. }
  1682. }
  1683. return 0;
  1684. }
  1685. #if 0
  1686. static int valid_marker_list[] =
  1687. {
  1688. /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */
  1689. /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1690. /* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1691. /* 2 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1692. /* 3 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1693. /* 4 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1694. /* 5 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1695. /* 6 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1696. /* 7 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1697. /* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1698. /* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1699. /* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1700. /* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1701. /* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1702. /* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1703. /* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1704. /* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
  1705. }
  1706. #endif
  1707. /* return the 8 bit start code value and update the search
  1708. state. Return -1 if no start code found */
  1709. static int find_marker(uint8_t **pbuf_ptr, uint8_t *buf_end)
  1710. {
  1711. uint8_t *buf_ptr;
  1712. unsigned int v, v2;
  1713. int val;
  1714. #ifdef DEBUG
  1715. int skipped=0;
  1716. #endif
  1717. buf_ptr = *pbuf_ptr;
  1718. while (buf_ptr < buf_end) {
  1719. v = *buf_ptr++;
  1720. v2 = *buf_ptr;
  1721. if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
  1722. val = *buf_ptr++;
  1723. goto found;
  1724. }
  1725. #ifdef DEBUG
  1726. skipped++;
  1727. #endif
  1728. }
  1729. val = -1;
  1730. found:
  1731. #ifdef DEBUG
  1732. dprintf("find_marker skipped %d bytes\n", skipped);
  1733. #endif
  1734. *pbuf_ptr = buf_ptr;
  1735. return val;
  1736. }
  1737. static int mjpeg_decode_frame(AVCodecContext *avctx,
  1738. void *data, int *data_size,
  1739. uint8_t *buf, int buf_size)
  1740. {
  1741. MJpegDecodeContext *s = avctx->priv_data;
  1742. uint8_t *buf_end, *buf_ptr;
  1743. int start_code;
  1744. AVFrame *picture = data;
  1745. buf_ptr = buf;
  1746. buf_end = buf + buf_size;
  1747. while (buf_ptr < buf_end) {
  1748. /* find start next marker */
  1749. start_code = find_marker(&buf_ptr, buf_end);
  1750. {
  1751. /* EOF */
  1752. if (start_code < 0) {
  1753. goto the_end;
  1754. } else {
  1755. dprintf("marker=%x avail_size_in_buf=%d\n", start_code, buf_end - buf_ptr);
  1756. if ((buf_end - buf_ptr) > s->buffer_size)
  1757. {
  1758. av_free(s->buffer);
  1759. s->buffer_size = buf_end-buf_ptr;
  1760. s->buffer = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE);
  1761. dprintf("buffer too small, expanding to %d bytes\n",
  1762. s->buffer_size);
  1763. }
  1764. /* unescape buffer of SOS, use special treatment for JPEG-LS */
  1765. if (start_code == SOS && !s->ls)
  1766. {
  1767. uint8_t *src = buf_ptr;
  1768. uint8_t *dst = s->buffer;
  1769. while (src<buf_end)
  1770. {
  1771. uint8_t x = *(src++);
  1772. *(dst++) = x;
  1773. if (x == 0xff)
  1774. {
  1775. while(src<buf_end && x == 0xff)
  1776. x = *(src++);
  1777. if (x >= 0xd0 && x <= 0xd7)
  1778. *(dst++) = x;
  1779. else if (x)
  1780. break;
  1781. }
  1782. }
  1783. init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8);
  1784. dprintf("escaping removed %d bytes\n",
  1785. (buf_end - buf_ptr) - (dst - s->buffer));
  1786. }
  1787. else if(start_code == SOS && s->ls){
  1788. uint8_t *src = buf_ptr;
  1789. uint8_t *dst = s->buffer;
  1790. int bit_count = 0;
  1791. int t = 0, b = 0;
  1792. PutBitContext pb;
  1793. s->cur_scan++;
  1794. /* find marker */
  1795. while (src + t < buf_end){
  1796. uint8_t x = src[t++];
  1797. if (x == 0xff){
  1798. while((src + t < buf_end) && x == 0xff)
  1799. x = src[t++];
  1800. if (x & 0x80) {
  1801. t -= 2;
  1802. break;
  1803. }
  1804. }
  1805. }
  1806. bit_count = t * 8;
  1807. init_put_bits(&pb, dst, t);
  1808. /* unescape bitstream */
  1809. while(b < t){
  1810. uint8_t x = src[b++];
  1811. put_bits(&pb, 8, x);
  1812. if(x == 0xFF){
  1813. x = src[b++];
  1814. put_bits(&pb, 7, x);
  1815. bit_count--;
  1816. }
  1817. }
  1818. flush_put_bits(&pb);
  1819. init_get_bits(&s->gb, dst, bit_count);
  1820. }
  1821. else
  1822. init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8);
  1823. s->start_code = start_code;
  1824. if(s->avctx->debug & FF_DEBUG_STARTCODE){
  1825. av_log(s->avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
  1826. }
  1827. /* process markers */
  1828. if (start_code >= 0xd0 && start_code <= 0xd7) {
  1829. dprintf("restart marker: %d\n", start_code&0x0f);
  1830. /* APP fields */
  1831. } else if (start_code >= APP0 && start_code <= APP15) {
  1832. mjpeg_decode_app(s);
  1833. /* Comment */
  1834. } else if (start_code == COM){
  1835. mjpeg_decode_com(s);
  1836. }
  1837. switch(start_code) {
  1838. case SOI:
  1839. s->restart_interval = 0;
  1840. s->restart_count = 0;
  1841. /* nothing to do on SOI */
  1842. break;
  1843. case DQT:
  1844. mjpeg_decode_dqt(s);
  1845. break;
  1846. case DHT:
  1847. if(mjpeg_decode_dht(s) < 0){
  1848. av_log(s->avctx, AV_LOG_ERROR, "huffman table decode error\n");
  1849. return -1;
  1850. }
  1851. break;
  1852. case SOF0:
  1853. s->lossless=0;
  1854. s->progressive=0;
  1855. if (mjpeg_decode_sof(s) < 0)
  1856. return -1;
  1857. break;
  1858. case SOF2:
  1859. s->lossless=0;
  1860. s->progressive=1;
  1861. if (mjpeg_decode_sof(s) < 0)
  1862. return -1;
  1863. break;
  1864. case SOF3:
  1865. s->lossless=1;
  1866. s->progressive=0;
  1867. if (mjpeg_decode_sof(s) < 0)
  1868. return -1;
  1869. break;
  1870. case SOF48:
  1871. s->lossless=1;
  1872. s->ls=1;
  1873. s->progressive=0;
  1874. if (mjpeg_decode_sof(s) < 0)
  1875. return -1;
  1876. break;
  1877. case LSE:
  1878. if (decode_lse(s) < 0)
  1879. return -1;
  1880. break;
  1881. case EOI:
  1882. s->cur_scan = 0;
  1883. if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
  1884. break;
  1885. eoi_parser:
  1886. {
  1887. if (s->interlaced) {
  1888. s->bottom_field ^= 1;
  1889. /* if not bottom field, do not output image yet */
  1890. if (s->bottom_field)
  1891. goto not_the_end;
  1892. }
  1893. *picture = s->picture;
  1894. *data_size = sizeof(AVFrame);
  1895. if(!s->lossless){
  1896. picture->quality= FFMAX(FFMAX(s->qscale[0], s->qscale[1]), s->qscale[2]);
  1897. picture->qstride= 0;
  1898. picture->qscale_table= s->qscale_table;
  1899. memset(picture->qscale_table, picture->quality, (s->width+15)/16);
  1900. if(avctx->debug & FF_DEBUG_QP)
  1901. av_log(s->avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
  1902. picture->quality*= FF_QP2LAMBDA;
  1903. }
  1904. goto the_end;
  1905. }
  1906. break;
  1907. case SOS:
  1908. mjpeg_decode_sos(s);
  1909. /* buggy avid puts EOI every 10-20th frame */
  1910. /* if restart period is over process EOI */
  1911. if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
  1912. goto eoi_parser;
  1913. break;
  1914. case DRI:
  1915. mjpeg_decode_dri(s);
  1916. break;
  1917. case SOF1:
  1918. case SOF5:
  1919. case SOF6:
  1920. case SOF7:
  1921. case SOF9:
  1922. case SOF10:
  1923. case SOF11:
  1924. case SOF13:
  1925. case SOF14:
  1926. case SOF15:
  1927. case JPG:
  1928. av_log(s->avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\n", start_code);
  1929. break;
  1930. // default:
  1931. // printf("mjpeg: unsupported marker (%x)\n", start_code);
  1932. // break;
  1933. }
  1934. not_the_end:
  1935. /* eof process start code */
  1936. buf_ptr += (get_bits_count(&s->gb)+7)/8;
  1937. dprintf("marker parser used %d bytes (%d bits)\n",
  1938. (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb));
  1939. }
  1940. }
  1941. }
  1942. the_end:
  1943. dprintf("mjpeg decode frame unused %d bytes\n", buf_end - buf_ptr);
  1944. // return buf_end - buf_ptr;
  1945. return buf_ptr - buf;
  1946. }
  1947. static int mjpegb_decode_frame(AVCodecContext *avctx,
  1948. void *data, int *data_size,
  1949. uint8_t *buf, int buf_size)
  1950. {
  1951. MJpegDecodeContext *s = avctx->priv_data;
  1952. uint8_t *buf_end, *buf_ptr;
  1953. AVFrame *picture = data;
  1954. GetBitContext hgb; /* for the header */
  1955. uint32_t dqt_offs, dht_offs, sof_offs, sos_offs, second_field_offs;
  1956. uint32_t field_size, sod_offs;
  1957. buf_ptr = buf;
  1958. buf_end = buf + buf_size;
  1959. read_header:
  1960. /* reset on every SOI */
  1961. s->restart_interval = 0;
  1962. s->restart_count = 0;
  1963. s->mjpb_skiptosod = 0;
  1964. init_get_bits(&hgb, buf_ptr, /*buf_size*/(buf_end - buf_ptr)*8);
  1965. skip_bits(&hgb, 32); /* reserved zeros */
  1966. if (get_bits_long(&hgb, 32) != be2me_32(ff_get_fourcc("mjpg")))
  1967. {
  1968. dprintf("not mjpeg-b (bad fourcc)\n");
  1969. return 0;
  1970. }
  1971. field_size = get_bits_long(&hgb, 32); /* field size */
  1972. dprintf("field size: 0x%x\n", field_size);
  1973. skip_bits(&hgb, 32); /* padded field size */
  1974. second_field_offs = get_bits_long(&hgb, 32);
  1975. dprintf("second field offs: 0x%x\n", second_field_offs);
  1976. if (second_field_offs)
  1977. s->interlaced = 1;
  1978. dqt_offs = get_bits_long(&hgb, 32);
  1979. dprintf("dqt offs: 0x%x\n", dqt_offs);
  1980. if (dqt_offs)
  1981. {
  1982. init_get_bits(&s->gb, buf+dqt_offs, (buf_end - (buf+dqt_offs))*8);
  1983. s->start_code = DQT;
  1984. mjpeg_decode_dqt(s);
  1985. }
  1986. dht_offs = get_bits_long(&hgb, 32);
  1987. dprintf("dht offs: 0x%x\n", dht_offs);
  1988. if (dht_offs)
  1989. {
  1990. init_get_bits(&s->gb, buf+dht_offs, (buf_end - (buf+dht_offs))*8);
  1991. s->start_code = DHT;
  1992. mjpeg_decode_dht(s);
  1993. }
  1994. sof_offs = get_bits_long(&hgb, 32);
  1995. dprintf("sof offs: 0x%x\n", sof_offs);
  1996. if (sof_offs)
  1997. {
  1998. init_get_bits(&s->gb, buf+sof_offs, (buf_end - (buf+sof_offs))*8);
  1999. s->start_code = SOF0;
  2000. if (mjpeg_decode_sof(s) < 0)
  2001. return -1;
  2002. }
  2003. sos_offs = get_bits_long(&hgb, 32);
  2004. dprintf("sos offs: 0x%x\n", sos_offs);
  2005. sod_offs = get_bits_long(&hgb, 32);
  2006. dprintf("sod offs: 0x%x\n", sod_offs);
  2007. if (sos_offs)
  2008. {
  2009. // init_get_bits(&s->gb, buf+sos_offs, (buf_end - (buf+sos_offs))*8);
  2010. init_get_bits(&s->gb, buf+sos_offs, field_size*8);
  2011. s->mjpb_skiptosod = (sod_offs - sos_offs - show_bits(&s->gb, 16));
  2012. s->start_code = SOS;
  2013. mjpeg_decode_sos(s);
  2014. }
  2015. if (s->interlaced) {
  2016. s->bottom_field ^= 1;
  2017. /* if not bottom field, do not output image yet */
  2018. if (s->bottom_field && second_field_offs)
  2019. {
  2020. buf_ptr = buf + second_field_offs;
  2021. second_field_offs = 0;
  2022. goto read_header;
  2023. }
  2024. }
  2025. //XXX FIXME factorize, this looks very similar to the EOI code
  2026. *picture= s->picture;
  2027. *data_size = sizeof(AVFrame);
  2028. if(!s->lossless){
  2029. picture->quality= FFMAX(FFMAX(s->qscale[0], s->qscale[1]), s->qscale[2]);
  2030. picture->qstride= 0;
  2031. picture->qscale_table= s->qscale_table;
  2032. memset(picture->qscale_table, picture->quality, (s->width+15)/16);
  2033. if(avctx->debug & FF_DEBUG_QP)
  2034. av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
  2035. picture->quality*= FF_QP2LAMBDA;
  2036. }
  2037. return buf_ptr - buf;
  2038. }
  2039. #include "sp5x.h"
  2040. static int sp5x_decode_frame(AVCodecContext *avctx,
  2041. void *data, int *data_size,
  2042. uint8_t *buf, int buf_size)
  2043. {
  2044. #if 0
  2045. MJpegDecodeContext *s = avctx->priv_data;
  2046. #endif
  2047. const int qscale = 5;
  2048. uint8_t *buf_ptr, *buf_end, *recoded;
  2049. int i = 0, j = 0;
  2050. if (!avctx->width || !avctx->height)
  2051. return -1;
  2052. buf_ptr = buf;
  2053. buf_end = buf + buf_size;
  2054. #if 1
  2055. recoded = av_mallocz(buf_size + 1024);
  2056. if (!recoded)
  2057. return -1;
  2058. /* SOI */
  2059. recoded[j++] = 0xFF;
  2060. recoded[j++] = 0xD8;
  2061. memcpy(recoded+j, &sp5x_data_dqt[0], sizeof(sp5x_data_dqt));
  2062. memcpy(recoded+j+5, &sp5x_quant_table[qscale * 2], 64);
  2063. memcpy(recoded+j+70, &sp5x_quant_table[(qscale * 2) + 1], 64);
  2064. j += sizeof(sp5x_data_dqt);
  2065. memcpy(recoded+j, &sp5x_data_dht[0], sizeof(sp5x_data_dht));
  2066. j += sizeof(sp5x_data_dht);
  2067. memcpy(recoded+j, &sp5x_data_sof[0], sizeof(sp5x_data_sof));
  2068. recoded[j+5] = (avctx->coded_height >> 8) & 0xFF;
  2069. recoded[j+6] = avctx->coded_height & 0xFF;
  2070. recoded[j+7] = (avctx->coded_width >> 8) & 0xFF;
  2071. recoded[j+8] = avctx->coded_width & 0xFF;
  2072. j += sizeof(sp5x_data_sof);
  2073. memcpy(recoded+j, &sp5x_data_sos[0], sizeof(sp5x_data_sos));
  2074. j += sizeof(sp5x_data_sos);
  2075. for (i = 14; i < buf_size && j < buf_size+1024-2; i++)
  2076. {
  2077. recoded[j++] = buf[i];
  2078. if (buf[i] == 0xff)
  2079. recoded[j++] = 0;
  2080. }
  2081. /* EOI */
  2082. recoded[j++] = 0xFF;
  2083. recoded[j++] = 0xD9;
  2084. i = mjpeg_decode_frame(avctx, data, data_size, recoded, j);
  2085. av_free(recoded);
  2086. #else
  2087. /* SOF */
  2088. s->bits = 8;
  2089. s->width = avctx->coded_width;
  2090. s->height = avctx->coded_height;
  2091. s->nb_components = 3;
  2092. s->component_id[0] = 0;
  2093. s->h_count[0] = 2;
  2094. s->v_count[0] = 2;
  2095. s->quant_index[0] = 0;
  2096. s->component_id[1] = 1;
  2097. s->h_count[1] = 1;
  2098. s->v_count[1] = 1;
  2099. s->quant_index[1] = 1;
  2100. s->component_id[2] = 2;
  2101. s->h_count[2] = 1;
  2102. s->v_count[2] = 1;
  2103. s->quant_index[2] = 1;
  2104. s->h_max = 2;
  2105. s->v_max = 2;
  2106. s->qscale_table = av_mallocz((s->width+15)/16);
  2107. avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420;
  2108. s->interlaced = 0;
  2109. s->picture.reference = 0;
  2110. if (avctx->get_buffer(avctx, &s->picture) < 0)
  2111. {
  2112. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  2113. return -1;
  2114. }
  2115. s->picture.pict_type = I_TYPE;
  2116. s->picture.key_frame = 1;
  2117. for (i = 0; i < 3; i++)
  2118. s->linesize[i] = s->picture.linesize[i] << s->interlaced;
  2119. /* DQT */
  2120. for (i = 0; i < 64; i++)
  2121. {
  2122. j = s->scantable.permutated[i];
  2123. s->quant_matrixes[0][j] = sp5x_quant_table[(qscale * 2) + i];
  2124. }
  2125. s->qscale[0] = FFMAX(
  2126. s->quant_matrixes[0][s->scantable.permutated[1]],
  2127. s->quant_matrixes[0][s->scantable.permutated[8]]) >> 1;
  2128. for (i = 0; i < 64; i++)
  2129. {
  2130. j = s->scantable.permutated[i];
  2131. s->quant_matrixes[1][j] = sp5x_quant_table[(qscale * 2) + 1 + i];
  2132. }
  2133. s->qscale[1] = FFMAX(
  2134. s->quant_matrixes[1][s->scantable.permutated[1]],
  2135. s->quant_matrixes[1][s->scantable.permutated[8]]) >> 1;
  2136. /* DHT */
  2137. /* SOS */
  2138. s->comp_index[0] = 0;
  2139. s->nb_blocks[0] = s->h_count[0] * s->v_count[0];
  2140. s->h_scount[0] = s->h_count[0];
  2141. s->v_scount[0] = s->v_count[0];
  2142. s->dc_index[0] = 0;
  2143. s->ac_index[0] = 0;
  2144. s->comp_index[1] = 1;
  2145. s->nb_blocks[1] = s->h_count[1] * s->v_count[1];
  2146. s->h_scount[1] = s->h_count[1];
  2147. s->v_scount[1] = s->v_count[1];
  2148. s->dc_index[1] = 1;
  2149. s->ac_index[1] = 1;
  2150. s->comp_index[2] = 2;
  2151. s->nb_blocks[2] = s->h_count[2] * s->v_count[2];
  2152. s->h_scount[2] = s->h_count[2];
  2153. s->v_scount[2] = s->v_count[2];
  2154. s->dc_index[2] = 1;
  2155. s->ac_index[2] = 1;
  2156. for (i = 0; i < 3; i++)
  2157. s->last_dc[i] = 1024;
  2158. s->mb_width = (s->width * s->h_max * 8 -1) / (s->h_max * 8);
  2159. s->mb_height = (s->height * s->v_max * 8 -1) / (s->v_max * 8);
  2160. init_get_bits(&s->gb, buf+14, (buf_size-14)*8);
  2161. return mjpeg_decode_scan(s);
  2162. #endif
  2163. return i;
  2164. }
  2165. static int mjpeg_decode_end(AVCodecContext *avctx)
  2166. {
  2167. MJpegDecodeContext *s = avctx->priv_data;
  2168. int i, j;
  2169. av_free(s->buffer);
  2170. av_free(s->qscale_table);
  2171. for(i=0;i<2;i++) {
  2172. for(j=0;j<4;j++)
  2173. free_vlc(&s->vlcs[i][j]);
  2174. }
  2175. return 0;
  2176. }
  2177. AVCodec mjpeg_decoder = {
  2178. "mjpeg",
  2179. CODEC_TYPE_VIDEO,
  2180. CODEC_ID_MJPEG,
  2181. sizeof(MJpegDecodeContext),
  2182. mjpeg_decode_init,
  2183. NULL,
  2184. mjpeg_decode_end,
  2185. mjpeg_decode_frame,
  2186. CODEC_CAP_DR1,
  2187. NULL
  2188. };
  2189. AVCodec mjpegb_decoder = {
  2190. "mjpegb",
  2191. CODEC_TYPE_VIDEO,
  2192. CODEC_ID_MJPEGB,
  2193. sizeof(MJpegDecodeContext),
  2194. mjpeg_decode_init,
  2195. NULL,
  2196. mjpeg_decode_end,
  2197. mjpegb_decode_frame,
  2198. CODEC_CAP_DR1,
  2199. NULL
  2200. };
  2201. AVCodec sp5x_decoder = {
  2202. "sp5x",
  2203. CODEC_TYPE_VIDEO,
  2204. CODEC_ID_SP5X,
  2205. sizeof(MJpegDecodeContext),
  2206. mjpeg_decode_init,
  2207. NULL,
  2208. mjpeg_decode_end,
  2209. sp5x_decode_frame,
  2210. CODEC_CAP_DR1,
  2211. NULL
  2212. };
  2213. #ifdef CONFIG_ENCODERS
  2214. AVCodec ljpeg_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them
  2215. "ljpeg",
  2216. CODEC_TYPE_VIDEO,
  2217. CODEC_ID_LJPEG,
  2218. sizeof(MpegEncContext),
  2219. MPV_encode_init,
  2220. encode_picture_lossless,
  2221. MPV_encode_end,
  2222. };
  2223. #endif
  2224. AVCodecParser mjpeg_parser = {
  2225. { CODEC_ID_MJPEG },
  2226. sizeof(ParseContext),
  2227. NULL,
  2228. jpeg_parse,
  2229. ff_parse_close,
  2230. };