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.

2463 lines
76KB

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