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.

2327 lines
69KB

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