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.

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