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.

2453 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 - 4 + 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<6;i++) {
  576. encode_block(s, block[i], i);
  577. }
  578. }
  579. static int encode_picture_lossless(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  580. MpegEncContext * const s = avctx->priv_data;
  581. MJpegContext * const m = s->mjpeg_ctx;
  582. AVFrame *pict = data;
  583. const int width= s->width;
  584. const int height= s->height;
  585. AVFrame * const p= (AVFrame*)&s->current_picture;
  586. const int predictor= avctx->prediction_method+1;
  587. init_put_bits(&s->pb, buf, buf_size);
  588. *p = *pict;
  589. p->pict_type= FF_I_TYPE;
  590. p->key_frame= 1;
  591. mjpeg_picture_header(s);
  592. s->header_bits= put_bits_count(&s->pb);
  593. if(avctx->pix_fmt == PIX_FMT_RGBA32){
  594. int x, y, i;
  595. const int linesize= p->linesize[0];
  596. uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
  597. int left[3], top[3], topleft[3];
  598. for(i=0; i<3; i++){
  599. buffer[0][i]= 1 << (9 - 1);
  600. }
  601. for(y = 0; y < height; y++) {
  602. const int modified_predictor= y ? predictor : 1;
  603. uint8_t *ptr = p->data[0] + (linesize * y);
  604. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
  605. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  606. return -1;
  607. }
  608. for(i=0; i<3; i++){
  609. top[i]= left[i]= topleft[i]= buffer[0][i];
  610. }
  611. for(x = 0; x < width; x++) {
  612. buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
  613. buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
  614. buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
  615. for(i=0;i<3;i++) {
  616. int pred, diff;
  617. PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
  618. topleft[i]= top[i];
  619. top[i]= buffer[x+1][i];
  620. left[i]= buffer[x][i];
  621. diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
  622. if(i==0)
  623. mjpeg_encode_dc(s, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
  624. else
  625. mjpeg_encode_dc(s, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  626. }
  627. }
  628. }
  629. }else{
  630. int mb_x, mb_y, i;
  631. const int mb_width = (width + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0];
  632. const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0];
  633. for(mb_y = 0; mb_y < mb_height; mb_y++) {
  634. 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]){
  635. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  636. return -1;
  637. }
  638. for(mb_x = 0; mb_x < mb_width; mb_x++) {
  639. if(mb_x==0 || mb_y==0){
  640. for(i=0;i<3;i++) {
  641. uint8_t *ptr;
  642. int x, y, h, v, linesize;
  643. h = s->mjpeg_hsample[i];
  644. v = s->mjpeg_vsample[i];
  645. linesize= p->linesize[i];
  646. for(y=0; y<v; y++){
  647. for(x=0; x<h; x++){
  648. int pred;
  649. ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  650. if(y==0 && mb_y==0){
  651. if(x==0 && mb_x==0){
  652. pred= 128;
  653. }else{
  654. pred= ptr[-1];
  655. }
  656. }else{
  657. if(x==0 && mb_x==0){
  658. pred= ptr[-linesize];
  659. }else{
  660. PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
  661. }
  662. }
  663. if(i==0)
  664. mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
  665. else
  666. mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  667. }
  668. }
  669. }
  670. }else{
  671. for(i=0;i<3;i++) {
  672. uint8_t *ptr;
  673. int x, y, h, v, linesize;
  674. h = s->mjpeg_hsample[i];
  675. v = s->mjpeg_vsample[i];
  676. linesize= p->linesize[i];
  677. for(y=0; y<v; y++){
  678. for(x=0; x<h; x++){
  679. int pred;
  680. ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  681. //printf("%d %d %d %d %8X\n", mb_x, mb_y, x, y, ptr);
  682. PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
  683. if(i==0)
  684. mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
  685. else
  686. mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  687. }
  688. }
  689. }
  690. }
  691. }
  692. }
  693. }
  694. emms_c();
  695. mjpeg_picture_trailer(s);
  696. s->picture_number++;
  697. flush_put_bits(&s->pb);
  698. return pbBufPtr(&s->pb) - s->pb.buf;
  699. // return (put_bits_count(&f->pb)+7)/8;
  700. }
  701. #endif //CONFIG_ENCODERS
  702. /******************************************/
  703. /* decoding */
  704. #define MAX_COMPONENTS 4
  705. typedef struct MJpegDecodeContext {
  706. AVCodecContext *avctx;
  707. GetBitContext gb;
  708. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  709. int start_code; /* current start code */
  710. int buffer_size;
  711. uint8_t *buffer;
  712. int16_t quant_matrixes[4][64];
  713. VLC vlcs[2][4];
  714. int qscale[4]; ///< quantizer scale calculated from quant_matrixes
  715. int org_height; /* size given at codec init */
  716. int first_picture; /* true if decoding first picture */
  717. int interlaced; /* true if interlaced */
  718. int bottom_field; /* true if bottom field */
  719. int lossless;
  720. int ls;
  721. int rgb;
  722. int rct; /* standard rct */
  723. int pegasus_rct; /* pegasus reversible colorspace transform */
  724. int bits; /* bits per component */
  725. int maxval;
  726. int near; ///< near lossless bound (si 0 for lossless)
  727. int t1,t2,t3;
  728. int reset; ///< context halfing intervall ?rename
  729. int width, height;
  730. int mb_width, mb_height;
  731. int nb_components;
  732. int component_id[MAX_COMPONENTS];
  733. int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */
  734. int v_count[MAX_COMPONENTS];
  735. int comp_index[MAX_COMPONENTS];
  736. int dc_index[MAX_COMPONENTS];
  737. int ac_index[MAX_COMPONENTS];
  738. int nb_blocks[MAX_COMPONENTS];
  739. int h_scount[MAX_COMPONENTS];
  740. int v_scount[MAX_COMPONENTS];
  741. int h_max, v_max; /* maximum h and v counts */
  742. int quant_index[4]; /* quant table index for each component */
  743. int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */
  744. AVFrame picture; /* picture structure */
  745. int linesize[MAX_COMPONENTS]; ///< linesize << interlaced
  746. int8_t *qscale_table;
  747. DECLARE_ALIGNED_8(DCTELEM, block[64]);
  748. ScanTable scantable;
  749. void (*idct_put)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  750. int restart_interval;
  751. int restart_count;
  752. int buggy_avid;
  753. int cs_itu601;
  754. int interlace_polarity;
  755. int mjpb_skiptosod;
  756. int cur_scan; /* current scan, used by JPEG-LS */
  757. } MJpegDecodeContext;
  758. #include "jpeg_ls.c" //FIXME make jpeg-ls more independant
  759. static int mjpeg_decode_dht(MJpegDecodeContext *s);
  760. static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table,
  761. int nb_codes, int use_static, int is_ac)
  762. {
  763. uint8_t huff_size[256+16];
  764. uint16_t huff_code[256+16];
  765. assert(nb_codes <= 256);
  766. memset(huff_size, 0, sizeof(huff_size));
  767. build_huffman_codes(huff_size, huff_code, bits_table, val_table);
  768. if(is_ac){
  769. memmove(huff_size+16, huff_size, sizeof(uint8_t)*nb_codes);
  770. memmove(huff_code+16, huff_code, sizeof(uint16_t)*nb_codes);
  771. memset(huff_size, 0, sizeof(uint8_t)*16);
  772. memset(huff_code, 0, sizeof(uint16_t)*16);
  773. nb_codes += 16;
  774. }
  775. return init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2, use_static);
  776. }
  777. static int mjpeg_decode_init(AVCodecContext *avctx)
  778. {
  779. MJpegDecodeContext *s = avctx->priv_data;
  780. MpegEncContext s2;
  781. memset(s, 0, sizeof(MJpegDecodeContext));
  782. s->avctx = avctx;
  783. /* ugly way to get the idct & scantable FIXME */
  784. memset(&s2, 0, sizeof(MpegEncContext));
  785. s2.avctx= avctx;
  786. // s2->out_format = FMT_MJPEG;
  787. dsputil_init(&s2.dsp, avctx);
  788. DCT_common_init(&s2);
  789. s->scantable= s2.intra_scantable;
  790. s->idct_put= s2.dsp.idct_put;
  791. s->mpeg_enc_ctx_allocated = 0;
  792. s->buffer_size = 0;
  793. s->buffer = NULL;
  794. s->start_code = -1;
  795. s->first_picture = 1;
  796. s->org_height = avctx->coded_height;
  797. build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12, 0, 0);
  798. build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12, 0, 0);
  799. build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251, 0, 1);
  800. build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251, 0, 1);
  801. if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
  802. {
  803. av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n");
  804. init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8);
  805. mjpeg_decode_dht(s);
  806. /* should check for error - but dunno */
  807. }
  808. return 0;
  809. }
  810. /**
  811. * finds the end of the current frame in the bitstream.
  812. * @return the position of the first byte of the next frame, or -1
  813. */
  814. static int find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
  815. int vop_found, i;
  816. uint16_t state;
  817. vop_found= pc->frame_start_found;
  818. state= pc->state;
  819. i=0;
  820. if(!vop_found){
  821. for(i=0; i<buf_size; i++){
  822. state= (state<<8) | buf[i];
  823. if(state == 0xFFD8){
  824. i++;
  825. vop_found=1;
  826. break;
  827. }
  828. }
  829. }
  830. if(vop_found){
  831. /* EOF considered as end of frame */
  832. if (buf_size == 0)
  833. return 0;
  834. for(; i<buf_size; i++){
  835. state= (state<<8) | buf[i];
  836. if(state == 0xFFD8){
  837. pc->frame_start_found=0;
  838. pc->state=0;
  839. return i-1;
  840. }
  841. }
  842. }
  843. pc->frame_start_found= vop_found;
  844. pc->state= state;
  845. return END_NOT_FOUND;
  846. }
  847. static int jpeg_parse(AVCodecParserContext *s,
  848. AVCodecContext *avctx,
  849. uint8_t **poutbuf, int *poutbuf_size,
  850. const uint8_t *buf, int buf_size)
  851. {
  852. ParseContext *pc = s->priv_data;
  853. int next;
  854. next= find_frame_end(pc, buf, buf_size);
  855. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  856. *poutbuf = NULL;
  857. *poutbuf_size = 0;
  858. return buf_size;
  859. }
  860. *poutbuf = (uint8_t *)buf;
  861. *poutbuf_size = buf_size;
  862. return next;
  863. }
  864. /* quantize tables */
  865. static int mjpeg_decode_dqt(MJpegDecodeContext *s)
  866. {
  867. int len, index, i, j;
  868. len = get_bits(&s->gb, 16) - 2;
  869. while (len >= 65) {
  870. /* only 8 bit precision handled */
  871. if (get_bits(&s->gb, 4) != 0)
  872. {
  873. dprintf("dqt: 16bit precision\n");
  874. return -1;
  875. }
  876. index = get_bits(&s->gb, 4);
  877. if (index >= 4)
  878. return -1;
  879. dprintf("index=%d\n", index);
  880. /* read quant table */
  881. for(i=0;i<64;i++) {
  882. j = s->scantable.permutated[i];
  883. s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
  884. }
  885. //XXX FIXME finetune, and perhaps add dc too
  886. s->qscale[index]= FFMAX(
  887. s->quant_matrixes[index][s->scantable.permutated[1]],
  888. s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1;
  889. dprintf("qscale[%d]: %d\n", index, s->qscale[index]);
  890. len -= 65;
  891. }
  892. return 0;
  893. }
  894. /* decode huffman tables and build VLC decoders */
  895. static int mjpeg_decode_dht(MJpegDecodeContext *s)
  896. {
  897. int len, index, i, class, n, v, code_max;
  898. uint8_t bits_table[17];
  899. uint8_t val_table[256];
  900. len = get_bits(&s->gb, 16) - 2;
  901. while (len > 0) {
  902. if (len < 17)
  903. return -1;
  904. class = get_bits(&s->gb, 4);
  905. if (class >= 2)
  906. return -1;
  907. index = get_bits(&s->gb, 4);
  908. if (index >= 4)
  909. return -1;
  910. n = 0;
  911. for(i=1;i<=16;i++) {
  912. bits_table[i] = get_bits(&s->gb, 8);
  913. n += bits_table[i];
  914. }
  915. len -= 17;
  916. if (len < n || n > 256)
  917. return -1;
  918. code_max = 0;
  919. for(i=0;i<n;i++) {
  920. v = get_bits(&s->gb, 8);
  921. if (v > code_max)
  922. code_max = v;
  923. val_table[i] = v;
  924. }
  925. len -= n;
  926. /* build VLC and flush previous vlc if present */
  927. free_vlc(&s->vlcs[class][index]);
  928. dprintf("class=%d index=%d nb_codes=%d\n",
  929. class, index, code_max + 1);
  930. if(build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1, 0, class > 0) < 0){
  931. return -1;
  932. }
  933. }
  934. return 0;
  935. }
  936. static int mjpeg_decode_sof(MJpegDecodeContext *s)
  937. {
  938. int len, nb_components, i, width, height;
  939. /* XXX: verify len field validity */
  940. len = get_bits(&s->gb, 16);
  941. s->bits= get_bits(&s->gb, 8);
  942. if(s->pegasus_rct) s->bits=9;
  943. if(s->bits==9 && !s->pegasus_rct) s->rct=1; //FIXME ugly
  944. if (s->bits != 8 && !s->lossless){
  945. av_log(s->avctx, AV_LOG_ERROR, "only 8 bits/component accepted\n");
  946. return -1;
  947. }
  948. if (s->bits > 8 && s->ls){
  949. av_log(s->avctx, AV_LOG_ERROR, "only <= 8 bits/component accepted for JPEG-LS\n");
  950. return -1;
  951. }
  952. height = get_bits(&s->gb, 16);
  953. width = get_bits(&s->gb, 16);
  954. dprintf("sof0: picture: %dx%d\n", width, height);
  955. if(avcodec_check_dimensions(s->avctx, width, height))
  956. return -1;
  957. nb_components = get_bits(&s->gb, 8);
  958. if (nb_components <= 0 ||
  959. nb_components > MAX_COMPONENTS)
  960. return -1;
  961. s->nb_components = nb_components;
  962. s->h_max = 1;
  963. s->v_max = 1;
  964. for(i=0;i<nb_components;i++) {
  965. /* component id */
  966. s->component_id[i] = get_bits(&s->gb, 8) - 1;
  967. s->h_count[i] = get_bits(&s->gb, 4);
  968. s->v_count[i] = get_bits(&s->gb, 4);
  969. /* compute hmax and vmax (only used in interleaved case) */
  970. if (s->h_count[i] > s->h_max)
  971. s->h_max = s->h_count[i];
  972. if (s->v_count[i] > s->v_max)
  973. s->v_max = s->v_count[i];
  974. s->quant_index[i] = get_bits(&s->gb, 8);
  975. if (s->quant_index[i] >= 4)
  976. return -1;
  977. dprintf("component %d %d:%d id: %d quant:%d\n", i, s->h_count[i],
  978. s->v_count[i], s->component_id[i], s->quant_index[i]);
  979. }
  980. if(s->ls && (s->h_max > 1 || s->v_max > 1)) {
  981. av_log(s->avctx, AV_LOG_ERROR, "Subsampling in JPEG-LS is not supported.\n");
  982. return -1;
  983. }
  984. if(s->v_max==1 && s->h_max==1 && s->lossless==1) s->rgb=1;
  985. /* if different size, realloc/alloc picture */
  986. /* XXX: also check h_count and v_count */
  987. if (width != s->width || height != s->height) {
  988. av_freep(&s->qscale_table);
  989. s->width = width;
  990. s->height = height;
  991. /* test interlaced mode */
  992. if (s->first_picture &&
  993. s->org_height != 0 &&
  994. s->height < ((s->org_height * 3) / 4)) {
  995. s->interlaced = 1;
  996. // s->bottom_field = (s->interlace_polarity) ? 1 : 0;
  997. s->bottom_field = 0;
  998. height *= 2;
  999. }
  1000. avcodec_set_dimensions(s->avctx, width, height);
  1001. s->qscale_table= av_mallocz((s->width+15)/16);
  1002. s->first_picture = 0;
  1003. }
  1004. if(s->interlaced && s->bottom_field)
  1005. return 0;
  1006. /* XXX: not complete test ! */
  1007. switch((s->h_count[0] << 4) | s->v_count[0]) {
  1008. case 0x11:
  1009. if(s->rgb){
  1010. s->avctx->pix_fmt = PIX_FMT_RGBA32;
  1011. }else if(s->nb_components==3)
  1012. s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV444P : PIX_FMT_YUVJ444P;
  1013. else
  1014. s->avctx->pix_fmt = PIX_FMT_GRAY8;
  1015. break;
  1016. case 0x21:
  1017. s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV422P : PIX_FMT_YUVJ422P;
  1018. break;
  1019. default:
  1020. case 0x22:
  1021. s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420P;
  1022. break;
  1023. }
  1024. if(s->ls){
  1025. if(s->nb_components > 1)
  1026. s->avctx->pix_fmt = PIX_FMT_RGB24;
  1027. else
  1028. s->avctx->pix_fmt = PIX_FMT_GRAY8;
  1029. }
  1030. if(s->picture.data[0])
  1031. s->avctx->release_buffer(s->avctx, &s->picture);
  1032. s->picture.reference= 0;
  1033. if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
  1034. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1035. return -1;
  1036. }
  1037. s->picture.pict_type= I_TYPE;
  1038. s->picture.key_frame= 1;
  1039. for(i=0; i<3; i++){
  1040. s->linesize[i]= s->picture.linesize[i] << s->interlaced;
  1041. }
  1042. // printf("%d %d %d %d %d %d\n", s->width, s->height, s->linesize[0], s->linesize[1], s->interlaced, s->avctx->height);
  1043. if (len != (8+(3*nb_components)))
  1044. {
  1045. dprintf("decode_sof0: error, len(%d) mismatch\n", len);
  1046. }
  1047. return 0;
  1048. }
  1049. static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
  1050. {
  1051. int code;
  1052. code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
  1053. if (code < 0)
  1054. {
  1055. dprintf("mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index,
  1056. &s->vlcs[0][dc_index]);
  1057. return 0xffff;
  1058. }
  1059. if(code)
  1060. return get_xbits(&s->gb, code);
  1061. else
  1062. return 0;
  1063. }
  1064. /* decode block and dequantize */
  1065. static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
  1066. int component, int dc_index, int ac_index, int16_t *quant_matrix)
  1067. {
  1068. int code, i, j, level, val;
  1069. VLC *ac_vlc;
  1070. /* DC coef */
  1071. val = mjpeg_decode_dc(s, dc_index);
  1072. if (val == 0xffff) {
  1073. dprintf("error dc\n");
  1074. return -1;
  1075. }
  1076. val = val * quant_matrix[0] + s->last_dc[component];
  1077. s->last_dc[component] = val;
  1078. block[0] = val;
  1079. /* AC coefs */
  1080. ac_vlc = &s->vlcs[1][ac_index];
  1081. i = 0;
  1082. {OPEN_READER(re, &s->gb)
  1083. for(;;) {
  1084. UPDATE_CACHE(re, &s->gb);
  1085. GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2)
  1086. /* EOB */
  1087. if (code == 0x10)
  1088. break;
  1089. if (code == 0x100) {
  1090. i += 16;
  1091. } else {
  1092. i += ((unsigned)code) >> 4;
  1093. code &= 0xf;
  1094. UPDATE_CACHE(re, &s->gb)
  1095. if ((int32_t)GET_CACHE(re,&s->gb)<0) { //MSB=1
  1096. level = NEG_USR32( GET_CACHE(re,&s->gb),code);
  1097. } else {
  1098. level = - NEG_USR32(~GET_CACHE(re,&s->gb),code);
  1099. }
  1100. LAST_SKIP_BITS(re, &s->gb, code)
  1101. if (i >= 63) {
  1102. if(i == 63){
  1103. j = s->scantable.permutated[63];
  1104. block[j] = level * quant_matrix[j];
  1105. break;
  1106. }
  1107. dprintf("error count: %d\n", i);
  1108. return -1;
  1109. }
  1110. j = s->scantable.permutated[i];
  1111. block[j] = level * quant_matrix[j];
  1112. }
  1113. }
  1114. CLOSE_READER(re, &s->gb)}
  1115. return 0;
  1116. }
  1117. static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor, int point_transform){
  1118. int i, mb_x, mb_y;
  1119. uint16_t buffer[32768][4];
  1120. int left[3], top[3], topleft[3];
  1121. const int linesize= s->linesize[0];
  1122. const int mask= (1<<s->bits)-1;
  1123. if((unsigned)s->mb_width > 32768) //dynamic alloc
  1124. return -1;
  1125. for(i=0; i<3; i++){
  1126. buffer[0][i]= 1 << (s->bits + point_transform - 1);
  1127. }
  1128. for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1129. const int modified_predictor= mb_y ? predictor : 1;
  1130. uint8_t *ptr = s->picture.data[0] + (linesize * mb_y);
  1131. if (s->interlaced && s->bottom_field)
  1132. ptr += linesize >> 1;
  1133. for(i=0; i<3; i++){
  1134. top[i]= left[i]= topleft[i]= buffer[0][i];
  1135. }
  1136. for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1137. if (s->restart_interval && !s->restart_count)
  1138. s->restart_count = s->restart_interval;
  1139. for(i=0;i<3;i++) {
  1140. int pred;
  1141. topleft[i]= top[i];
  1142. top[i]= buffer[mb_x][i];
  1143. PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
  1144. left[i]=
  1145. buffer[mb_x][i]= mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform));
  1146. }
  1147. if (s->restart_interval && !--s->restart_count) {
  1148. align_get_bits(&s->gb);
  1149. skip_bits(&s->gb, 16); /* skip RSTn */
  1150. }
  1151. }
  1152. if(s->rct){
  1153. for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1154. ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200)>>2);
  1155. ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1];
  1156. ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1];
  1157. }
  1158. }else if(s->pegasus_rct){
  1159. for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1160. ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2])>>2);
  1161. ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1];
  1162. ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1];
  1163. }
  1164. }else{
  1165. for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1166. ptr[4*mb_x+0] = buffer[mb_x][0];
  1167. ptr[4*mb_x+1] = buffer[mb_x][1];
  1168. ptr[4*mb_x+2] = buffer[mb_x][2];
  1169. }
  1170. }
  1171. }
  1172. return 0;
  1173. }
  1174. static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, int point_transform){
  1175. int i, mb_x, mb_y;
  1176. const int nb_components=3;
  1177. for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1178. for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1179. if (s->restart_interval && !s->restart_count)
  1180. s->restart_count = s->restart_interval;
  1181. if(mb_x==0 || mb_y==0 || s->interlaced){
  1182. for(i=0;i<nb_components;i++) {
  1183. uint8_t *ptr;
  1184. int n, h, v, x, y, c, j, linesize;
  1185. n = s->nb_blocks[i];
  1186. c = s->comp_index[i];
  1187. h = s->h_scount[i];
  1188. v = s->v_scount[i];
  1189. x = 0;
  1190. y = 0;
  1191. linesize= s->linesize[c];
  1192. for(j=0; j<n; j++) {
  1193. int pred;
  1194. ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  1195. if(y==0 && mb_y==0){
  1196. if(x==0 && mb_x==0){
  1197. pred= 128 << point_transform;
  1198. }else{
  1199. pred= ptr[-1];
  1200. }
  1201. }else{
  1202. if(x==0 && mb_x==0){
  1203. pred= ptr[-linesize];
  1204. }else{
  1205. PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
  1206. }
  1207. }
  1208. if (s->interlaced && s->bottom_field)
  1209. ptr += linesize >> 1;
  1210. *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
  1211. if (++x == h) {
  1212. x = 0;
  1213. y++;
  1214. }
  1215. }
  1216. }
  1217. }else{
  1218. for(i=0;i<nb_components;i++) {
  1219. uint8_t *ptr;
  1220. int n, h, v, x, y, c, j, linesize;
  1221. n = s->nb_blocks[i];
  1222. c = s->comp_index[i];
  1223. h = s->h_scount[i];
  1224. v = s->v_scount[i];
  1225. x = 0;
  1226. y = 0;
  1227. linesize= s->linesize[c];
  1228. for(j=0; j<n; j++) {
  1229. int pred;
  1230. ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  1231. PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
  1232. *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
  1233. if (++x == h) {
  1234. x = 0;
  1235. y++;
  1236. }
  1237. }
  1238. }
  1239. }
  1240. if (s->restart_interval && !--s->restart_count) {
  1241. align_get_bits(&s->gb);
  1242. skip_bits(&s->gb, 16); /* skip RSTn */
  1243. }
  1244. }
  1245. }
  1246. return 0;
  1247. }
  1248. static int mjpeg_decode_scan(MJpegDecodeContext *s){
  1249. int i, mb_x, mb_y;
  1250. const int nb_components=3;
  1251. for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1252. for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1253. if (s->restart_interval && !s->restart_count)
  1254. s->restart_count = s->restart_interval;
  1255. for(i=0;i<nb_components;i++) {
  1256. uint8_t *ptr;
  1257. int n, h, v, x, y, c, j;
  1258. n = s->nb_blocks[i];
  1259. c = s->comp_index[i];
  1260. h = s->h_scount[i];
  1261. v = s->v_scount[i];
  1262. x = 0;
  1263. y = 0;
  1264. for(j=0;j<n;j++) {
  1265. memset(s->block, 0, sizeof(s->block));
  1266. if (decode_block(s, s->block, i,
  1267. s->dc_index[i], s->ac_index[i],
  1268. s->quant_matrixes[ s->quant_index[c] ]) < 0) {
  1269. dprintf("error y=%d x=%d\n", mb_y, mb_x);
  1270. return -1;
  1271. }
  1272. // dprintf("mb: %d %d processed\n", mb_y, mb_x);
  1273. ptr = s->picture.data[c] +
  1274. (((s->linesize[c] * (v * mb_y + y) * 8) +
  1275. (h * mb_x + x) * 8) >> s->avctx->lowres);
  1276. if (s->interlaced && s->bottom_field)
  1277. ptr += s->linesize[c] >> 1;
  1278. //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);
  1279. s->idct_put(ptr, s->linesize[c], s->block);
  1280. if (++x == h) {
  1281. x = 0;
  1282. y++;
  1283. }
  1284. }
  1285. }
  1286. /* (< 1350) buggy workaround for Spectralfan.mov, should be fixed */
  1287. if (s->restart_interval && (s->restart_interval < 1350) &&
  1288. !--s->restart_count) {
  1289. align_get_bits(&s->gb);
  1290. skip_bits(&s->gb, 16); /* skip RSTn */
  1291. for (i=0; i<nb_components; i++) /* reset dc */
  1292. s->last_dc[i] = 1024;
  1293. }
  1294. }
  1295. }
  1296. return 0;
  1297. }
  1298. static int mjpeg_decode_sos(MJpegDecodeContext *s)
  1299. {
  1300. int len, nb_components, i, h, v, predictor, point_transform;
  1301. int vmax, hmax, index, id;
  1302. const int block_size= s->lossless ? 1 : 8;
  1303. int ilv;
  1304. /* XXX: verify len field validity */
  1305. len = get_bits(&s->gb, 16);
  1306. nb_components = get_bits(&s->gb, 8);
  1307. if (len != 6+2*nb_components)
  1308. {
  1309. dprintf("decode_sos: invalid len (%d)\n", len);
  1310. return -1;
  1311. }
  1312. /* XXX: only interleaved scan accepted */
  1313. if ((nb_components != s->nb_components) && !s->ls)
  1314. {
  1315. dprintf("decode_sos: components(%d) mismatch\n", nb_components);
  1316. return -1;
  1317. }
  1318. vmax = 0;
  1319. hmax = 0;
  1320. for(i=0;i<nb_components;i++) {
  1321. id = get_bits(&s->gb, 8) - 1;
  1322. dprintf("component: %d\n", id);
  1323. /* find component index */
  1324. for(index=0;index<s->nb_components;index++)
  1325. if (id == s->component_id[index])
  1326. break;
  1327. if (index == s->nb_components)
  1328. {
  1329. dprintf("decode_sos: index(%d) out of components\n", index);
  1330. return -1;
  1331. }
  1332. s->comp_index[i] = index;
  1333. s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
  1334. s->h_scount[i] = s->h_count[index];
  1335. s->v_scount[i] = s->v_count[index];
  1336. s->dc_index[i] = get_bits(&s->gb, 4);
  1337. s->ac_index[i] = get_bits(&s->gb, 4);
  1338. if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
  1339. s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
  1340. goto out_of_range;
  1341. #if 0 //buggy
  1342. switch(s->start_code)
  1343. {
  1344. case SOF0:
  1345. if (dc_index[i] > 1 || ac_index[i] > 1)
  1346. goto out_of_range;
  1347. break;
  1348. case SOF1:
  1349. case SOF2:
  1350. if (dc_index[i] > 3 || ac_index[i] > 3)
  1351. goto out_of_range;
  1352. break;
  1353. case SOF3:
  1354. if (dc_index[i] > 3 || ac_index[i] != 0)
  1355. goto out_of_range;
  1356. break;
  1357. }
  1358. #endif
  1359. }
  1360. predictor= get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
  1361. ilv= get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */
  1362. skip_bits(&s->gb, 4); /* Ah */
  1363. point_transform= get_bits(&s->gb, 4); /* Al */
  1364. for(i=0;i<nb_components;i++)
  1365. s->last_dc[i] = 1024;
  1366. if (nb_components > 1) {
  1367. /* interleaved stream */
  1368. s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
  1369. s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
  1370. } else if(!s->ls) { /* skip this for JPEG-LS */
  1371. h = s->h_max / s->h_scount[s->comp_index[0]];
  1372. v = s->v_max / s->v_scount[s->comp_index[0]];
  1373. s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
  1374. s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
  1375. s->nb_blocks[0] = 1;
  1376. s->h_scount[0] = 1;
  1377. s->v_scount[0] = 1;
  1378. }
  1379. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1380. 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" : "",
  1381. predictor, point_transform, ilv, s->bits,
  1382. s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""));
  1383. /* mjpeg-b can have padding bytes between sos and image data, skip them */
  1384. for (i = s->mjpb_skiptosod; i > 0; i--)
  1385. skip_bits(&s->gb, 8);
  1386. if(s->lossless){
  1387. if(s->ls){
  1388. // for(){
  1389. // reset_ls_coding_parameters(s, 0);
  1390. ls_decode_picture(s, predictor, point_transform, ilv);
  1391. }else{
  1392. if(s->rgb){
  1393. if(ljpeg_decode_rgb_scan(s, predictor, point_transform) < 0)
  1394. return -1;
  1395. }else{
  1396. if(ljpeg_decode_yuv_scan(s, predictor, point_transform) < 0)
  1397. return -1;
  1398. }
  1399. }
  1400. }else{
  1401. if(mjpeg_decode_scan(s) < 0)
  1402. return -1;
  1403. }
  1404. emms_c();
  1405. return 0;
  1406. out_of_range:
  1407. dprintf("decode_sos: ac/dc index out of range\n");
  1408. return -1;
  1409. }
  1410. static int mjpeg_decode_dri(MJpegDecodeContext *s)
  1411. {
  1412. if (get_bits(&s->gb, 16) != 4)
  1413. return -1;
  1414. s->restart_interval = get_bits(&s->gb, 16);
  1415. s->restart_count = 0;
  1416. dprintf("restart interval: %d\n", s->restart_interval);
  1417. return 0;
  1418. }
  1419. static int mjpeg_decode_app(MJpegDecodeContext *s)
  1420. {
  1421. int len, id;
  1422. len = get_bits(&s->gb, 16);
  1423. if (len < 5)
  1424. return -1;
  1425. if(8*len + get_bits_count(&s->gb) > s->gb.size_in_bits)
  1426. return -1;
  1427. id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
  1428. id = be2me_32(id);
  1429. len -= 6;
  1430. if(s->avctx->debug & FF_DEBUG_STARTCODE){
  1431. av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id);
  1432. }
  1433. /* buggy AVID, it puts EOI only at every 10th frame */
  1434. /* also this fourcc is used by non-avid files too, it holds some
  1435. informations, but it's always present in AVID creates files */
  1436. if (id == ff_get_fourcc("AVI1"))
  1437. {
  1438. /* structure:
  1439. 4bytes AVI1
  1440. 1bytes polarity
  1441. 1bytes always zero
  1442. 4bytes field_size
  1443. 4bytes field_size_less_padding
  1444. */
  1445. s->buggy_avid = 1;
  1446. // if (s->first_picture)
  1447. // printf("mjpeg: workarounding buggy AVID\n");
  1448. s->interlace_polarity = get_bits(&s->gb, 8);
  1449. #if 0
  1450. skip_bits(&s->gb, 8);
  1451. skip_bits(&s->gb, 32);
  1452. skip_bits(&s->gb, 32);
  1453. len -= 10;
  1454. #endif
  1455. // if (s->interlace_polarity)
  1456. // printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity);
  1457. goto out;
  1458. }
  1459. // len -= 2;
  1460. if (id == ff_get_fourcc("JFIF"))
  1461. {
  1462. int t_w, t_h, v1, v2;
  1463. skip_bits(&s->gb, 8); /* the trailing zero-byte */
  1464. v1= get_bits(&s->gb, 8);
  1465. v2= get_bits(&s->gb, 8);
  1466. skip_bits(&s->gb, 8);
  1467. s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 16);
  1468. s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 16);
  1469. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1470. av_log(s->avctx, AV_LOG_INFO, "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
  1471. v1, v2,
  1472. s->avctx->sample_aspect_ratio.num,
  1473. s->avctx->sample_aspect_ratio.den
  1474. );
  1475. t_w = get_bits(&s->gb, 8);
  1476. t_h = get_bits(&s->gb, 8);
  1477. if (t_w && t_h)
  1478. {
  1479. /* skip thumbnail */
  1480. if (len-10-(t_w*t_h*3) > 0)
  1481. len -= t_w*t_h*3;
  1482. }
  1483. len -= 10;
  1484. goto out;
  1485. }
  1486. if (id == ff_get_fourcc("Adob") && (get_bits(&s->gb, 8) == 'e'))
  1487. {
  1488. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1489. av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n");
  1490. skip_bits(&s->gb, 16); /* version */
  1491. skip_bits(&s->gb, 16); /* flags0 */
  1492. skip_bits(&s->gb, 16); /* flags1 */
  1493. skip_bits(&s->gb, 8); /* transform */
  1494. len -= 7;
  1495. goto out;
  1496. }
  1497. if (id == ff_get_fourcc("LJIF")){
  1498. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1499. av_log(s->avctx, AV_LOG_INFO, "Pegasus lossless jpeg header found\n");
  1500. skip_bits(&s->gb, 16); /* version ? */
  1501. skip_bits(&s->gb, 16); /* unknwon always 0? */
  1502. skip_bits(&s->gb, 16); /* unknwon always 0? */
  1503. skip_bits(&s->gb, 16); /* unknwon always 0? */
  1504. switch( get_bits(&s->gb, 8)){
  1505. case 1:
  1506. s->rgb= 1;
  1507. s->pegasus_rct=0;
  1508. break;
  1509. case 2:
  1510. s->rgb= 1;
  1511. s->pegasus_rct=1;
  1512. break;
  1513. default:
  1514. av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n");
  1515. }
  1516. len -= 9;
  1517. goto out;
  1518. }
  1519. /* Apple MJPEG-A */
  1520. if ((s->start_code == APP1) && (len > (0x28 - 8)))
  1521. {
  1522. id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
  1523. id = be2me_32(id);
  1524. len -= 4;
  1525. if (id == ff_get_fourcc("mjpg")) /* Apple MJPEG-A */
  1526. {
  1527. #if 0
  1528. skip_bits(&s->gb, 32); /* field size */
  1529. skip_bits(&s->gb, 32); /* pad field size */
  1530. skip_bits(&s->gb, 32); /* next off */
  1531. skip_bits(&s->gb, 32); /* quant off */
  1532. skip_bits(&s->gb, 32); /* huff off */
  1533. skip_bits(&s->gb, 32); /* image off */
  1534. skip_bits(&s->gb, 32); /* scan off */
  1535. skip_bits(&s->gb, 32); /* data off */
  1536. #endif
  1537. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1538. av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
  1539. }
  1540. }
  1541. out:
  1542. /* slow but needed for extreme adobe jpegs */
  1543. if (len < 0)
  1544. av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error, decode_app parser read over the end\n");
  1545. while(--len > 0)
  1546. skip_bits(&s->gb, 8);
  1547. return 0;
  1548. }
  1549. static int mjpeg_decode_com(MJpegDecodeContext *s)
  1550. {
  1551. int len = get_bits(&s->gb, 16);
  1552. if (len >= 2 && 8*len - 16 + get_bits_count(&s->gb) <= s->gb.size_in_bits) {
  1553. uint8_t *cbuf = av_malloc(len - 1);
  1554. if (cbuf) {
  1555. int i;
  1556. for (i = 0; i < len - 2; i++)
  1557. cbuf[i] = get_bits(&s->gb, 8);
  1558. if (i > 0 && cbuf[i-1] == '\n')
  1559. cbuf[i-1] = 0;
  1560. else
  1561. cbuf[i] = 0;
  1562. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1563. av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf);
  1564. /* buggy avid, it puts EOI only at every 10th frame */
  1565. if (!strcmp(cbuf, "AVID"))
  1566. {
  1567. s->buggy_avid = 1;
  1568. // if (s->first_picture)
  1569. // printf("mjpeg: workarounding buggy AVID\n");
  1570. }
  1571. else if(!strcmp(cbuf, "CS=ITU601")){
  1572. s->cs_itu601= 1;
  1573. }
  1574. av_free(cbuf);
  1575. }
  1576. }
  1577. return 0;
  1578. }
  1579. #if 0
  1580. static int valid_marker_list[] =
  1581. {
  1582. /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */
  1583. /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1584. /* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1585. /* 2 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1586. /* 3 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1587. /* 4 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1588. /* 5 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1589. /* 6 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1590. /* 7 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1591. /* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1592. /* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1593. /* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1594. /* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1595. /* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1596. /* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1597. /* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1598. /* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
  1599. }
  1600. #endif
  1601. /* return the 8 bit start code value and update the search
  1602. state. Return -1 if no start code found */
  1603. static int find_marker(uint8_t **pbuf_ptr, uint8_t *buf_end)
  1604. {
  1605. uint8_t *buf_ptr;
  1606. unsigned int v, v2;
  1607. int val;
  1608. #ifdef DEBUG
  1609. int skipped=0;
  1610. #endif
  1611. buf_ptr = *pbuf_ptr;
  1612. while (buf_ptr < buf_end) {
  1613. v = *buf_ptr++;
  1614. v2 = *buf_ptr;
  1615. if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
  1616. val = *buf_ptr++;
  1617. goto found;
  1618. }
  1619. #ifdef DEBUG
  1620. skipped++;
  1621. #endif
  1622. }
  1623. val = -1;
  1624. found:
  1625. #ifdef DEBUG
  1626. dprintf("find_marker skipped %d bytes\n", skipped);
  1627. #endif
  1628. *pbuf_ptr = buf_ptr;
  1629. return val;
  1630. }
  1631. static int mjpeg_decode_frame(AVCodecContext *avctx,
  1632. void *data, int *data_size,
  1633. uint8_t *buf, int buf_size)
  1634. {
  1635. MJpegDecodeContext *s = avctx->priv_data;
  1636. uint8_t *buf_end, *buf_ptr;
  1637. int start_code;
  1638. AVFrame *picture = data;
  1639. buf_ptr = buf;
  1640. buf_end = buf + buf_size;
  1641. while (buf_ptr < buf_end) {
  1642. /* find start next marker */
  1643. start_code = find_marker(&buf_ptr, buf_end);
  1644. {
  1645. /* EOF */
  1646. if (start_code < 0) {
  1647. goto the_end;
  1648. } else {
  1649. dprintf("marker=%x avail_size_in_buf=%d\n", start_code, buf_end - buf_ptr);
  1650. if ((buf_end - buf_ptr) > s->buffer_size)
  1651. {
  1652. av_free(s->buffer);
  1653. s->buffer_size = buf_end-buf_ptr;
  1654. s->buffer = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE);
  1655. dprintf("buffer too small, expanding to %d bytes\n",
  1656. s->buffer_size);
  1657. }
  1658. /* unescape buffer of SOS, use special treatment for JPEG-LS */
  1659. if (start_code == SOS && !s->ls)
  1660. {
  1661. uint8_t *src = buf_ptr;
  1662. uint8_t *dst = s->buffer;
  1663. while (src<buf_end)
  1664. {
  1665. uint8_t x = *(src++);
  1666. *(dst++) = x;
  1667. if (x == 0xff)
  1668. {
  1669. while(src<buf_end && x == 0xff)
  1670. x = *(src++);
  1671. if (x >= 0xd0 && x <= 0xd7)
  1672. *(dst++) = x;
  1673. else if (x)
  1674. break;
  1675. }
  1676. }
  1677. init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8);
  1678. dprintf("escaping removed %d bytes\n",
  1679. (buf_end - buf_ptr) - (dst - s->buffer));
  1680. }
  1681. else if(start_code == SOS && s->ls){
  1682. uint8_t *src = buf_ptr;
  1683. uint8_t *dst = s->buffer;
  1684. int bit_count = 0;
  1685. int t = 0, b = 0;
  1686. PutBitContext pb;
  1687. s->cur_scan++;
  1688. /* find marker */
  1689. while (src + t < buf_end){
  1690. uint8_t x = src[t++];
  1691. if (x == 0xff){
  1692. while((src + t < buf_end) && x == 0xff)
  1693. x = src[t++];
  1694. if (x & 0x80) {
  1695. t -= 2;
  1696. break;
  1697. }
  1698. }
  1699. }
  1700. bit_count = t * 8;
  1701. init_put_bits(&pb, dst, t);
  1702. /* unescape bitstream */
  1703. while(b < t){
  1704. uint8_t x = src[b++];
  1705. put_bits(&pb, 8, x);
  1706. if(x == 0xFF){
  1707. x = src[b++];
  1708. put_bits(&pb, 7, x);
  1709. bit_count--;
  1710. }
  1711. }
  1712. flush_put_bits(&pb);
  1713. init_get_bits(&s->gb, dst, bit_count);
  1714. }
  1715. else
  1716. init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8);
  1717. s->start_code = start_code;
  1718. if(s->avctx->debug & FF_DEBUG_STARTCODE){
  1719. av_log(s->avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
  1720. }
  1721. /* process markers */
  1722. if (start_code >= 0xd0 && start_code <= 0xd7) {
  1723. dprintf("restart marker: %d\n", start_code&0x0f);
  1724. /* APP fields */
  1725. } else if (start_code >= APP0 && start_code <= APP15) {
  1726. mjpeg_decode_app(s);
  1727. /* Comment */
  1728. } else if (start_code == COM){
  1729. mjpeg_decode_com(s);
  1730. }
  1731. switch(start_code) {
  1732. case SOI:
  1733. s->restart_interval = 0;
  1734. s->restart_count = 0;
  1735. /* nothing to do on SOI */
  1736. break;
  1737. case DQT:
  1738. mjpeg_decode_dqt(s);
  1739. break;
  1740. case DHT:
  1741. if(mjpeg_decode_dht(s) < 0){
  1742. av_log(s->avctx, AV_LOG_ERROR, "huffman table decode error\n");
  1743. return -1;
  1744. }
  1745. break;
  1746. case SOF0:
  1747. s->lossless=0;
  1748. if (mjpeg_decode_sof(s) < 0)
  1749. return -1;
  1750. break;
  1751. case SOF3:
  1752. s->lossless=1;
  1753. if (mjpeg_decode_sof(s) < 0)
  1754. return -1;
  1755. break;
  1756. case SOF48:
  1757. s->lossless=1;
  1758. s->ls=1;
  1759. if (mjpeg_decode_sof(s) < 0)
  1760. return -1;
  1761. break;
  1762. case LSE:
  1763. if (decode_lse(s) < 0)
  1764. return -1;
  1765. break;
  1766. case EOI:
  1767. if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
  1768. break;
  1769. eoi_parser:
  1770. {
  1771. if (s->interlaced) {
  1772. s->bottom_field ^= 1;
  1773. /* if not bottom field, do not output image yet */
  1774. if (s->bottom_field)
  1775. goto not_the_end;
  1776. }
  1777. *picture = s->picture;
  1778. *data_size = sizeof(AVFrame);
  1779. if(!s->lossless){
  1780. picture->quality= FFMAX(FFMAX(s->qscale[0], s->qscale[1]), s->qscale[2]);
  1781. picture->qstride= 0;
  1782. picture->qscale_table= s->qscale_table;
  1783. memset(picture->qscale_table, picture->quality, (s->width+15)/16);
  1784. if(avctx->debug & FF_DEBUG_QP)
  1785. av_log(s->avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
  1786. picture->quality*= FF_QP2LAMBDA;
  1787. }
  1788. goto the_end;
  1789. }
  1790. break;
  1791. case SOS:
  1792. mjpeg_decode_sos(s);
  1793. /* buggy avid puts EOI every 10-20th frame */
  1794. /* if restart period is over process EOI */
  1795. if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
  1796. goto eoi_parser;
  1797. break;
  1798. case DRI:
  1799. mjpeg_decode_dri(s);
  1800. break;
  1801. case SOF1:
  1802. case SOF2:
  1803. case SOF5:
  1804. case SOF6:
  1805. case SOF7:
  1806. case SOF9:
  1807. case SOF10:
  1808. case SOF11:
  1809. case SOF13:
  1810. case SOF14:
  1811. case SOF15:
  1812. case JPG:
  1813. av_log(s->avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\n", start_code);
  1814. break;
  1815. // default:
  1816. // printf("mjpeg: unsupported marker (%x)\n", start_code);
  1817. // break;
  1818. }
  1819. not_the_end:
  1820. /* eof process start code */
  1821. buf_ptr += (get_bits_count(&s->gb)+7)/8;
  1822. dprintf("marker parser used %d bytes (%d bits)\n",
  1823. (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb));
  1824. }
  1825. }
  1826. }
  1827. the_end:
  1828. dprintf("mjpeg decode frame unused %d bytes\n", buf_end - buf_ptr);
  1829. // return buf_end - buf_ptr;
  1830. return buf_ptr - buf;
  1831. }
  1832. static int mjpegb_decode_frame(AVCodecContext *avctx,
  1833. void *data, int *data_size,
  1834. uint8_t *buf, int buf_size)
  1835. {
  1836. MJpegDecodeContext *s = avctx->priv_data;
  1837. uint8_t *buf_end, *buf_ptr;
  1838. AVFrame *picture = data;
  1839. GetBitContext hgb; /* for the header */
  1840. uint32_t dqt_offs, dht_offs, sof_offs, sos_offs, second_field_offs;
  1841. uint32_t field_size, sod_offs;
  1842. buf_ptr = buf;
  1843. buf_end = buf + buf_size;
  1844. read_header:
  1845. /* reset on every SOI */
  1846. s->restart_interval = 0;
  1847. s->restart_count = 0;
  1848. s->mjpb_skiptosod = 0;
  1849. init_get_bits(&hgb, buf_ptr, /*buf_size*/(buf_end - buf_ptr)*8);
  1850. skip_bits(&hgb, 32); /* reserved zeros */
  1851. if (get_bits_long(&hgb, 32) != be2me_32(ff_get_fourcc("mjpg")))
  1852. {
  1853. dprintf("not mjpeg-b (bad fourcc)\n");
  1854. return 0;
  1855. }
  1856. field_size = get_bits_long(&hgb, 32); /* field size */
  1857. dprintf("field size: 0x%x\n", field_size);
  1858. skip_bits(&hgb, 32); /* padded field size */
  1859. second_field_offs = get_bits_long(&hgb, 32);
  1860. dprintf("second field offs: 0x%x\n", second_field_offs);
  1861. if (second_field_offs)
  1862. s->interlaced = 1;
  1863. dqt_offs = get_bits_long(&hgb, 32);
  1864. dprintf("dqt offs: 0x%x\n", dqt_offs);
  1865. if (dqt_offs)
  1866. {
  1867. init_get_bits(&s->gb, buf+dqt_offs, (buf_end - (buf+dqt_offs))*8);
  1868. s->start_code = DQT;
  1869. mjpeg_decode_dqt(s);
  1870. }
  1871. dht_offs = get_bits_long(&hgb, 32);
  1872. dprintf("dht offs: 0x%x\n", dht_offs);
  1873. if (dht_offs)
  1874. {
  1875. init_get_bits(&s->gb, buf+dht_offs, (buf_end - (buf+dht_offs))*8);
  1876. s->start_code = DHT;
  1877. mjpeg_decode_dht(s);
  1878. }
  1879. sof_offs = get_bits_long(&hgb, 32);
  1880. dprintf("sof offs: 0x%x\n", sof_offs);
  1881. if (sof_offs)
  1882. {
  1883. init_get_bits(&s->gb, buf+sof_offs, (buf_end - (buf+sof_offs))*8);
  1884. s->start_code = SOF0;
  1885. if (mjpeg_decode_sof(s) < 0)
  1886. return -1;
  1887. }
  1888. sos_offs = get_bits_long(&hgb, 32);
  1889. dprintf("sos offs: 0x%x\n", sos_offs);
  1890. sod_offs = get_bits_long(&hgb, 32);
  1891. dprintf("sod offs: 0x%x\n", sod_offs);
  1892. if (sos_offs)
  1893. {
  1894. // init_get_bits(&s->gb, buf+sos_offs, (buf_end - (buf+sos_offs))*8);
  1895. init_get_bits(&s->gb, buf+sos_offs, field_size*8);
  1896. s->mjpb_skiptosod = (sod_offs - sos_offs - show_bits(&s->gb, 16));
  1897. s->start_code = SOS;
  1898. mjpeg_decode_sos(s);
  1899. }
  1900. if (s->interlaced) {
  1901. s->bottom_field ^= 1;
  1902. /* if not bottom field, do not output image yet */
  1903. if (s->bottom_field && second_field_offs)
  1904. {
  1905. buf_ptr = buf + second_field_offs;
  1906. second_field_offs = 0;
  1907. goto read_header;
  1908. }
  1909. }
  1910. //XXX FIXME factorize, this looks very similar to the EOI code
  1911. *picture= s->picture;
  1912. *data_size = sizeof(AVFrame);
  1913. if(!s->lossless){
  1914. picture->quality= FFMAX(FFMAX(s->qscale[0], s->qscale[1]), s->qscale[2]);
  1915. picture->qstride= 0;
  1916. picture->qscale_table= s->qscale_table;
  1917. memset(picture->qscale_table, picture->quality, (s->width+15)/16);
  1918. if(avctx->debug & FF_DEBUG_QP)
  1919. av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
  1920. picture->quality*= FF_QP2LAMBDA;
  1921. }
  1922. return buf_ptr - buf;
  1923. }
  1924. #include "sp5x.h"
  1925. static int sp5x_decode_frame(AVCodecContext *avctx,
  1926. void *data, int *data_size,
  1927. uint8_t *buf, int buf_size)
  1928. {
  1929. #if 0
  1930. MJpegDecodeContext *s = avctx->priv_data;
  1931. #endif
  1932. const int qscale = 5;
  1933. uint8_t *buf_ptr, *buf_end, *recoded;
  1934. int i = 0, j = 0;
  1935. if (!avctx->width || !avctx->height)
  1936. return -1;
  1937. buf_ptr = buf;
  1938. buf_end = buf + buf_size;
  1939. #if 1
  1940. recoded = av_mallocz(buf_size + 1024);
  1941. if (!recoded)
  1942. return -1;
  1943. /* SOI */
  1944. recoded[j++] = 0xFF;
  1945. recoded[j++] = 0xD8;
  1946. memcpy(recoded+j, &sp5x_data_dqt[0], sizeof(sp5x_data_dqt));
  1947. memcpy(recoded+j+5, &sp5x_quant_table[qscale * 2], 64);
  1948. memcpy(recoded+j+70, &sp5x_quant_table[(qscale * 2) + 1], 64);
  1949. j += sizeof(sp5x_data_dqt);
  1950. memcpy(recoded+j, &sp5x_data_dht[0], sizeof(sp5x_data_dht));
  1951. j += sizeof(sp5x_data_dht);
  1952. memcpy(recoded+j, &sp5x_data_sof[0], sizeof(sp5x_data_sof));
  1953. recoded[j+5] = (avctx->coded_height >> 8) & 0xFF;
  1954. recoded[j+6] = avctx->coded_height & 0xFF;
  1955. recoded[j+7] = (avctx->coded_width >> 8) & 0xFF;
  1956. recoded[j+8] = avctx->coded_width & 0xFF;
  1957. j += sizeof(sp5x_data_sof);
  1958. memcpy(recoded+j, &sp5x_data_sos[0], sizeof(sp5x_data_sos));
  1959. j += sizeof(sp5x_data_sos);
  1960. for (i = 14; i < buf_size && j < buf_size+1024-2; i++)
  1961. {
  1962. recoded[j++] = buf[i];
  1963. if (buf[i] == 0xff)
  1964. recoded[j++] = 0;
  1965. }
  1966. /* EOI */
  1967. recoded[j++] = 0xFF;
  1968. recoded[j++] = 0xD9;
  1969. i = mjpeg_decode_frame(avctx, data, data_size, recoded, j);
  1970. av_free(recoded);
  1971. #else
  1972. /* SOF */
  1973. s->bits = 8;
  1974. s->width = avctx->coded_width;
  1975. s->height = avctx->coded_height;
  1976. s->nb_components = 3;
  1977. s->component_id[0] = 0;
  1978. s->h_count[0] = 2;
  1979. s->v_count[0] = 2;
  1980. s->quant_index[0] = 0;
  1981. s->component_id[1] = 1;
  1982. s->h_count[1] = 1;
  1983. s->v_count[1] = 1;
  1984. s->quant_index[1] = 1;
  1985. s->component_id[2] = 2;
  1986. s->h_count[2] = 1;
  1987. s->v_count[2] = 1;
  1988. s->quant_index[2] = 1;
  1989. s->h_max = 2;
  1990. s->v_max = 2;
  1991. s->qscale_table = av_mallocz((s->width+15)/16);
  1992. avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420;
  1993. s->interlaced = 0;
  1994. s->picture.reference = 0;
  1995. if (avctx->get_buffer(avctx, &s->picture) < 0)
  1996. {
  1997. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1998. return -1;
  1999. }
  2000. s->picture.pict_type = I_TYPE;
  2001. s->picture.key_frame = 1;
  2002. for (i = 0; i < 3; i++)
  2003. s->linesize[i] = s->picture.linesize[i] << s->interlaced;
  2004. /* DQT */
  2005. for (i = 0; i < 64; i++)
  2006. {
  2007. j = s->scantable.permutated[i];
  2008. s->quant_matrixes[0][j] = sp5x_quant_table[(qscale * 2) + i];
  2009. }
  2010. s->qscale[0] = FFMAX(
  2011. s->quant_matrixes[0][s->scantable.permutated[1]],
  2012. s->quant_matrixes[0][s->scantable.permutated[8]]) >> 1;
  2013. for (i = 0; i < 64; i++)
  2014. {
  2015. j = s->scantable.permutated[i];
  2016. s->quant_matrixes[1][j] = sp5x_quant_table[(qscale * 2) + 1 + i];
  2017. }
  2018. s->qscale[1] = FFMAX(
  2019. s->quant_matrixes[1][s->scantable.permutated[1]],
  2020. s->quant_matrixes[1][s->scantable.permutated[8]]) >> 1;
  2021. /* DHT */
  2022. /* SOS */
  2023. s->comp_index[0] = 0;
  2024. s->nb_blocks[0] = s->h_count[0] * s->v_count[0];
  2025. s->h_scount[0] = s->h_count[0];
  2026. s->v_scount[0] = s->v_count[0];
  2027. s->dc_index[0] = 0;
  2028. s->ac_index[0] = 0;
  2029. s->comp_index[1] = 1;
  2030. s->nb_blocks[1] = s->h_count[1] * s->v_count[1];
  2031. s->h_scount[1] = s->h_count[1];
  2032. s->v_scount[1] = s->v_count[1];
  2033. s->dc_index[1] = 1;
  2034. s->ac_index[1] = 1;
  2035. s->comp_index[2] = 2;
  2036. s->nb_blocks[2] = s->h_count[2] * s->v_count[2];
  2037. s->h_scount[2] = s->h_count[2];
  2038. s->v_scount[2] = s->v_count[2];
  2039. s->dc_index[2] = 1;
  2040. s->ac_index[2] = 1;
  2041. for (i = 0; i < 3; i++)
  2042. s->last_dc[i] = 1024;
  2043. s->mb_width = (s->width * s->h_max * 8 -1) / (s->h_max * 8);
  2044. s->mb_height = (s->height * s->v_max * 8 -1) / (s->v_max * 8);
  2045. init_get_bits(&s->gb, buf+14, (buf_size-14)*8);
  2046. return mjpeg_decode_scan(s);
  2047. #endif
  2048. return i;
  2049. }
  2050. static int mjpeg_decode_end(AVCodecContext *avctx)
  2051. {
  2052. MJpegDecodeContext *s = avctx->priv_data;
  2053. int i, j;
  2054. av_free(s->buffer);
  2055. av_free(s->qscale_table);
  2056. for(i=0;i<2;i++) {
  2057. for(j=0;j<4;j++)
  2058. free_vlc(&s->vlcs[i][j]);
  2059. }
  2060. return 0;
  2061. }
  2062. AVCodec mjpeg_decoder = {
  2063. "mjpeg",
  2064. CODEC_TYPE_VIDEO,
  2065. CODEC_ID_MJPEG,
  2066. sizeof(MJpegDecodeContext),
  2067. mjpeg_decode_init,
  2068. NULL,
  2069. mjpeg_decode_end,
  2070. mjpeg_decode_frame,
  2071. CODEC_CAP_DR1,
  2072. NULL
  2073. };
  2074. AVCodec mjpegb_decoder = {
  2075. "mjpegb",
  2076. CODEC_TYPE_VIDEO,
  2077. CODEC_ID_MJPEGB,
  2078. sizeof(MJpegDecodeContext),
  2079. mjpeg_decode_init,
  2080. NULL,
  2081. mjpeg_decode_end,
  2082. mjpegb_decode_frame,
  2083. CODEC_CAP_DR1,
  2084. NULL
  2085. };
  2086. AVCodec sp5x_decoder = {
  2087. "sp5x",
  2088. CODEC_TYPE_VIDEO,
  2089. CODEC_ID_SP5X,
  2090. sizeof(MJpegDecodeContext),
  2091. mjpeg_decode_init,
  2092. NULL,
  2093. mjpeg_decode_end,
  2094. sp5x_decode_frame,
  2095. CODEC_CAP_DR1,
  2096. NULL
  2097. };
  2098. #ifdef CONFIG_ENCODERS
  2099. AVCodec ljpeg_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them
  2100. "ljpeg",
  2101. CODEC_TYPE_VIDEO,
  2102. CODEC_ID_LJPEG,
  2103. sizeof(MpegEncContext),
  2104. MPV_encode_init,
  2105. encode_picture_lossless,
  2106. MPV_encode_end,
  2107. };
  2108. #endif
  2109. AVCodecParser mjpeg_parser = {
  2110. { CODEC_ID_MJPEG },
  2111. sizeof(ParseContext),
  2112. NULL,
  2113. jpeg_parse,
  2114. ff_parse_close,
  2115. };