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.

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