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.

2224 lines
66KB

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