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.

2322 lines
69KB

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