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.

2262 lines
66KB

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