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.

2445 lines
75KB

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