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.

2059 lines
62KB

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