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.

1103 lines
33KB

  1. /*
  2. * MJPEG encoder and decoder
  3. * Copyright (c) 2000, 2001 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. *
  19. * Support for external huffman table and various fixed by
  20. * Alex Beregszaszi <alex@naxine.org>
  21. */
  22. //#define DEBUG
  23. #include "avcodec.h"
  24. #include "dsputil.h"
  25. #include "mpegvideo.h"
  26. typedef struct MJpegContext {
  27. UINT8 huff_size_dc_luminance[12];
  28. UINT16 huff_code_dc_luminance[12];
  29. UINT8 huff_size_dc_chrominance[12];
  30. UINT16 huff_code_dc_chrominance[12];
  31. UINT8 huff_size_ac_luminance[256];
  32. UINT16 huff_code_ac_luminance[256];
  33. UINT8 huff_size_ac_chrominance[256];
  34. UINT16 huff_code_ac_chrominance[256];
  35. } MJpegContext;
  36. /* JPEG marker codes */
  37. typedef enum {
  38. /* start of frame */
  39. SOF0 = 0xc0, /* baseline */
  40. SOF1 = 0xc1, /* extended sequential, huffman */
  41. SOF2 = 0xc2, /* progressive, huffman */
  42. SOF3 = 0xc3, /* lossless, huffman */
  43. SOF5 = 0xc5, /* differential sequential, huffman */
  44. SOF6 = 0xc6, /* differential progressive, huffman */
  45. SOF7 = 0xc7, /* differential lossless, huffman */
  46. JPG = 0xc8, /* reserved for JPEG extension */
  47. SOF9 = 0xc9, /* extended sequential, arithmetic */
  48. SOF10 = 0xca, /* progressive, arithmetic */
  49. SOF11 = 0xcb, /* lossless, arithmetic */
  50. SOF13 = 0xcd, /* differential sequential, arithmetic */
  51. SOF14 = 0xce, /* differential progressive, arithmetic */
  52. SOF15 = 0xcf, /* differential lossless, arithmetic */
  53. DHT = 0xc4, /* define huffman tables */
  54. DAC = 0xcc, /* define arithmetic-coding conditioning */
  55. /* restart with modulo 8 count "m" */
  56. RST0 = 0xd0,
  57. RST1 = 0xd1,
  58. RST2 = 0xd2,
  59. RST3 = 0xd3,
  60. RST4 = 0xd4,
  61. RST5 = 0xd5,
  62. RST6 = 0xd6,
  63. RST7 = 0xd7,
  64. SOI = 0xd8, /* start of image */
  65. EOI = 0xd9, /* end of image */
  66. SOS = 0xda, /* start of scan */
  67. DQT = 0xdb, /* define quantization tables */
  68. DNL = 0xdc, /* define number of lines */
  69. DRI = 0xdd, /* define restart interval */
  70. DHP = 0xde, /* define hierarchical progression */
  71. EXP = 0xdf, /* expand reference components */
  72. APP0 = 0xe0,
  73. APP1 = 0xe1,
  74. APP2 = 0xe2,
  75. APP3 = 0xe3,
  76. APP4 = 0xe4,
  77. APP5 = 0xe5,
  78. APP6 = 0xe6,
  79. APP7 = 0xe7,
  80. APP8 = 0xe8,
  81. APP9 = 0xe9,
  82. APP10 = 0xea,
  83. APP11 = 0xeb,
  84. APP12 = 0xec,
  85. APP13 = 0xed,
  86. APP14 = 0xee,
  87. APP15 = 0xef,
  88. JPG0 = 0xf0,
  89. JPG1 = 0xf1,
  90. JPG2 = 0xf2,
  91. JPG3 = 0xf3,
  92. JPG4 = 0xf4,
  93. JPG5 = 0xf5,
  94. JPG6 = 0xf6,
  95. JPG7 = 0xf7,
  96. JPG8 = 0xf8,
  97. JPG9 = 0xf9,
  98. JPG10 = 0xfa,
  99. JPG11 = 0xfb,
  100. JPG12 = 0xfc,
  101. JPG13 = 0xfd,
  102. COM = 0xfe, /* comment */
  103. TEM = 0x01, /* temporary private use for arithmetic coding */
  104. /* 0x02 -> 0xbf reserved */
  105. } JPEG_MARKER;
  106. #if 0
  107. /* These are the sample quantization tables given in JPEG spec section K.1.
  108. * The spec says that the values given produce "good" quality, and
  109. * when divided by 2, "very good" quality.
  110. */
  111. static const unsigned char std_luminance_quant_tbl[64] = {
  112. 16, 11, 10, 16, 24, 40, 51, 61,
  113. 12, 12, 14, 19, 26, 58, 60, 55,
  114. 14, 13, 16, 24, 40, 57, 69, 56,
  115. 14, 17, 22, 29, 51, 87, 80, 62,
  116. 18, 22, 37, 56, 68, 109, 103, 77,
  117. 24, 35, 55, 64, 81, 104, 113, 92,
  118. 49, 64, 78, 87, 103, 121, 120, 101,
  119. 72, 92, 95, 98, 112, 100, 103, 99
  120. };
  121. static const unsigned char std_chrominance_quant_tbl[64] = {
  122. 17, 18, 24, 47, 99, 99, 99, 99,
  123. 18, 21, 26, 66, 99, 99, 99, 99,
  124. 24, 26, 56, 99, 99, 99, 99, 99,
  125. 47, 66, 99, 99, 99, 99, 99, 99,
  126. 99, 99, 99, 99, 99, 99, 99, 99,
  127. 99, 99, 99, 99, 99, 99, 99, 99,
  128. 99, 99, 99, 99, 99, 99, 99, 99,
  129. 99, 99, 99, 99, 99, 99, 99, 99
  130. };
  131. #endif
  132. /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
  133. /* IMPORTANT: these are only valid for 8-bit data precision! */
  134. static const UINT8 bits_dc_luminance[17] =
  135. { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
  136. static const UINT8 val_dc_luminance[] =
  137. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  138. static const UINT8 bits_dc_chrominance[17] =
  139. { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
  140. static const UINT8 val_dc_chrominance[] =
  141. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  142. static const UINT8 bits_ac_luminance[17] =
  143. { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
  144. static const UINT8 val_ac_luminance[] =
  145. { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  146. 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  147. 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  148. 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  149. 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  150. 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  151. 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  152. 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  153. 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  154. 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  155. 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  156. 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  157. 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  158. 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  159. 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  160. 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  161. 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  162. 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  163. 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  164. 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  165. 0xf9, 0xfa
  166. };
  167. static const UINT8 bits_ac_chrominance[17] =
  168. { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
  169. static const UINT8 val_ac_chrominance[] =
  170. { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
  171. 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  172. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  173. 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
  174. 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
  175. 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
  176. 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
  177. 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  178. 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  179. 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  180. 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  181. 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  182. 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
  183. 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  184. 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  185. 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
  186. 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
  187. 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
  188. 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  189. 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  190. 0xf9, 0xfa
  191. };
  192. /* isn't this function nicer than the one in the libjpeg ? */
  193. static void build_huffman_codes(UINT8 *huff_size, UINT16 *huff_code,
  194. const UINT8 *bits_table, const UINT8 *val_table)
  195. {
  196. int i, j, k,nb, code, sym;
  197. code = 0;
  198. k = 0;
  199. for(i=1;i<=16;i++) {
  200. nb = bits_table[i];
  201. for(j=0;j<nb;j++) {
  202. sym = val_table[k++];
  203. huff_size[sym] = i;
  204. huff_code[sym] = code;
  205. code++;
  206. }
  207. code <<= 1;
  208. }
  209. }
  210. int mjpeg_init(MpegEncContext *s)
  211. {
  212. MJpegContext *m;
  213. m = malloc(sizeof(MJpegContext));
  214. if (!m)
  215. return -1;
  216. s->min_qcoeff=-1023;
  217. s->max_qcoeff= 1023;
  218. /* build all the huffman tables */
  219. build_huffman_codes(m->huff_size_dc_luminance,
  220. m->huff_code_dc_luminance,
  221. bits_dc_luminance,
  222. val_dc_luminance);
  223. build_huffman_codes(m->huff_size_dc_chrominance,
  224. m->huff_code_dc_chrominance,
  225. bits_dc_chrominance,
  226. val_dc_chrominance);
  227. build_huffman_codes(m->huff_size_ac_luminance,
  228. m->huff_code_ac_luminance,
  229. bits_ac_luminance,
  230. val_ac_luminance);
  231. build_huffman_codes(m->huff_size_ac_chrominance,
  232. m->huff_code_ac_chrominance,
  233. bits_ac_chrominance,
  234. val_ac_chrominance);
  235. s->mjpeg_ctx = m;
  236. return 0;
  237. }
  238. void mjpeg_close(MpegEncContext *s)
  239. {
  240. free(s->mjpeg_ctx);
  241. }
  242. static inline void put_marker(PutBitContext *p, int code)
  243. {
  244. put_bits(p, 8, 0xff);
  245. put_bits(p, 8, code);
  246. }
  247. /* table_class: 0 = DC coef, 1 = AC coefs */
  248. static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
  249. const UINT8 *bits_table, const UINT8 *value_table)
  250. {
  251. PutBitContext *p = &s->pb;
  252. int n, i;
  253. put_bits(p, 4, table_class);
  254. put_bits(p, 4, table_id);
  255. n = 0;
  256. for(i=1;i<=16;i++) {
  257. n += bits_table[i];
  258. put_bits(p, 8, bits_table[i]);
  259. }
  260. for(i=0;i<n;i++)
  261. put_bits(p, 8, value_table[i]);
  262. return n + 17;
  263. }
  264. static void jpeg_table_header(MpegEncContext *s)
  265. {
  266. PutBitContext *p = &s->pb;
  267. int i, j, size;
  268. UINT8 *ptr;
  269. /* quant matrixes */
  270. put_marker(p, DQT);
  271. put_bits(p, 16, 2 + 1 * (1 + 64));
  272. put_bits(p, 4, 0); /* 8 bit precision */
  273. put_bits(p, 4, 0); /* table 0 */
  274. for(i=0;i<64;i++) {
  275. j = zigzag_direct[i];
  276. put_bits(p, 8, s->intra_matrix[j]);
  277. }
  278. #if 0
  279. put_bits(p, 4, 0); /* 8 bit precision */
  280. put_bits(p, 4, 1); /* table 1 */
  281. for(i=0;i<64;i++) {
  282. j = zigzag_direct[i];
  283. put_bits(p, 8, s->chroma_intra_matrix[j]);
  284. }
  285. #endif
  286. /* huffman table */
  287. put_marker(p, DHT);
  288. flush_put_bits(p);
  289. ptr = pbBufPtr(p);
  290. put_bits(p, 16, 0); /* patched later */
  291. size = 2;
  292. size += put_huffman_table(s, 0, 0, bits_dc_luminance, val_dc_luminance);
  293. size += put_huffman_table(s, 0, 1, bits_dc_chrominance, val_dc_chrominance);
  294. size += put_huffman_table(s, 1, 0, bits_ac_luminance, val_ac_luminance);
  295. size += put_huffman_table(s, 1, 1, bits_ac_chrominance, val_ac_chrominance);
  296. ptr[0] = size >> 8;
  297. ptr[1] = size;
  298. }
  299. void mjpeg_picture_header(MpegEncContext *s)
  300. {
  301. put_marker(&s->pb, SOI);
  302. if (s->mjpeg_write_tables) jpeg_table_header(s);
  303. put_marker(&s->pb, SOF0);
  304. put_bits(&s->pb, 16, 17);
  305. put_bits(&s->pb, 8, 8); /* 8 bits/component */
  306. put_bits(&s->pb, 16, s->height);
  307. put_bits(&s->pb, 16, s->width);
  308. put_bits(&s->pb, 8, 3); /* 3 components */
  309. /* Y component */
  310. put_bits(&s->pb, 8, 1); /* component number */
  311. put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
  312. put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
  313. put_bits(&s->pb, 8, 0); /* select matrix */
  314. /* Cb component */
  315. put_bits(&s->pb, 8, 2); /* component number */
  316. put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
  317. put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
  318. put_bits(&s->pb, 8, 0); /* select matrix */
  319. /* Cr component */
  320. put_bits(&s->pb, 8, 3); /* component number */
  321. put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
  322. put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
  323. put_bits(&s->pb, 8, 0); /* select matrix */
  324. /* scan header */
  325. put_marker(&s->pb, SOS);
  326. put_bits(&s->pb, 16, 12); /* length */
  327. put_bits(&s->pb, 8, 3); /* 3 components */
  328. /* Y component */
  329. put_bits(&s->pb, 8, 1); /* index */
  330. put_bits(&s->pb, 4, 0); /* DC huffman table index */
  331. put_bits(&s->pb, 4, 0); /* AC huffman table index */
  332. /* Cb component */
  333. put_bits(&s->pb, 8, 2); /* index */
  334. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  335. put_bits(&s->pb, 4, 1); /* AC huffman table index */
  336. /* Cr component */
  337. put_bits(&s->pb, 8, 3); /* index */
  338. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  339. put_bits(&s->pb, 4, 1); /* AC huffman table index */
  340. put_bits(&s->pb, 8, 0); /* Ss (not used) */
  341. put_bits(&s->pb, 8, 63); /* Se (not used) */
  342. put_bits(&s->pb, 8, 0); /* (not used) */
  343. }
  344. void mjpeg_picture_trailer(MpegEncContext *s)
  345. {
  346. jflush_put_bits(&s->pb);
  347. put_marker(&s->pb, EOI);
  348. }
  349. static inline void encode_dc(MpegEncContext *s, int val,
  350. UINT8 *huff_size, UINT16 *huff_code)
  351. {
  352. int mant, nbits;
  353. if (val == 0) {
  354. jput_bits(&s->pb, huff_size[0], huff_code[0]);
  355. } else {
  356. mant = val;
  357. if (val < 0) {
  358. val = -val;
  359. mant--;
  360. }
  361. /* compute the log (XXX: optimize) */
  362. nbits = 0;
  363. while (val != 0) {
  364. val = val >> 1;
  365. nbits++;
  366. }
  367. jput_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
  368. jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
  369. }
  370. }
  371. static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
  372. {
  373. int mant, nbits, code, i, j;
  374. int component, dc, run, last_index, val;
  375. MJpegContext *m = s->mjpeg_ctx;
  376. UINT8 *huff_size_ac;
  377. UINT16 *huff_code_ac;
  378. /* DC coef */
  379. component = (n <= 3 ? 0 : n - 4 + 1);
  380. dc = block[0]; /* overflow is impossible */
  381. val = dc - s->last_dc[component];
  382. if (n < 4) {
  383. encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
  384. huff_size_ac = m->huff_size_ac_luminance;
  385. huff_code_ac = m->huff_code_ac_luminance;
  386. } else {
  387. encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  388. huff_size_ac = m->huff_size_ac_chrominance;
  389. huff_code_ac = m->huff_code_ac_chrominance;
  390. }
  391. s->last_dc[component] = dc;
  392. /* AC coefs */
  393. run = 0;
  394. last_index = s->block_last_index[n];
  395. for(i=1;i<=last_index;i++) {
  396. j = zigzag_direct[i];
  397. val = block[j];
  398. if (val == 0) {
  399. run++;
  400. } else {
  401. while (run >= 16) {
  402. jput_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
  403. run -= 16;
  404. }
  405. mant = val;
  406. if (val < 0) {
  407. val = -val;
  408. mant--;
  409. }
  410. /* compute the log (XXX: optimize) */
  411. nbits = 0;
  412. while (val != 0) {
  413. val = val >> 1;
  414. nbits++;
  415. }
  416. code = (run << 4) | nbits;
  417. jput_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
  418. jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
  419. run = 0;
  420. }
  421. }
  422. /* output EOB only if not already 64 values */
  423. if (last_index < 63 || run != 0)
  424. jput_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
  425. }
  426. void mjpeg_encode_mb(MpegEncContext *s,
  427. DCTELEM block[6][64])
  428. {
  429. int i;
  430. for(i=0;i<6;i++) {
  431. encode_block(s, block[i], i);
  432. }
  433. }
  434. /******************************************/
  435. /* decoding */
  436. /* compressed picture size */
  437. #define PICTURE_BUFFER_SIZE 100000
  438. #define MAX_COMPONENTS 4
  439. typedef struct MJpegDecodeContext {
  440. GetBitContext gb;
  441. UINT32 header_state;
  442. int start_code; /* current start code */
  443. UINT8 *buf_ptr;
  444. int buffer_size;
  445. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  446. INT16 quant_matrixes[4][64];
  447. VLC vlcs[2][4];
  448. int org_width, org_height; /* size given at codec init */
  449. int first_picture; /* true if decoding first picture */
  450. int interlaced; /* true if interlaced */
  451. int bottom_field; /* true if bottom field */
  452. int width, height;
  453. int nb_components;
  454. int component_id[MAX_COMPONENTS];
  455. int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */
  456. int v_count[MAX_COMPONENTS];
  457. int h_max, v_max; /* maximum h and v counts */
  458. int quant_index[4]; /* quant table index for each component */
  459. int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */
  460. UINT8 *current_picture[MAX_COMPONENTS]; /* picture structure */
  461. int linesize[MAX_COMPONENTS];
  462. DCTELEM block[64] __align8;
  463. UINT8 buffer[PICTURE_BUFFER_SIZE];
  464. } MJpegDecodeContext;
  465. static void build_vlc(VLC *vlc, const UINT8 *bits_table, const UINT8 *val_table,
  466. int nb_codes)
  467. {
  468. UINT8 huff_size[256];
  469. UINT16 huff_code[256];
  470. memset(huff_size, 0, sizeof(huff_size));
  471. build_huffman_codes(huff_size, huff_code, bits_table, val_table);
  472. init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2);
  473. }
  474. static int mjpeg_decode_init(AVCodecContext *avctx)
  475. {
  476. MJpegDecodeContext *s = avctx->priv_data;
  477. s->header_state = 0;
  478. s->mpeg_enc_ctx_allocated = 0;
  479. s->buffer_size = PICTURE_BUFFER_SIZE - 1; /* minus 1 to take into
  480. account FF 00 case */
  481. s->start_code = -1;
  482. s->buf_ptr = s->buffer;
  483. s->first_picture = 1;
  484. s->org_width = avctx->width;
  485. s->org_height = avctx->height;
  486. build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12);
  487. build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12);
  488. build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251);
  489. build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251);
  490. if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
  491. {
  492. printf("mjpeg: using external huffman table\n");
  493. mjpeg_decode_dht(s, avctx->extradata, avctx->extradata_size);
  494. /* should check for error - but dunno */
  495. }
  496. return 0;
  497. }
  498. /* quantize tables */
  499. static int mjpeg_decode_dqt(MJpegDecodeContext *s,
  500. UINT8 *buf, int buf_size)
  501. {
  502. int len, index, i, j;
  503. init_get_bits(&s->gb, buf, buf_size);
  504. len = get_bits(&s->gb, 16);
  505. len -= 2;
  506. while (len >= 65) {
  507. /* only 8 bit precision handled */
  508. if (get_bits(&s->gb, 4) != 0)
  509. {
  510. dprintf("dqt: 16bit precision\n");
  511. return -1;
  512. }
  513. index = get_bits(&s->gb, 4);
  514. if (index >= 4)
  515. return -1;
  516. dprintf("index=%d\n", index);
  517. /* read quant table */
  518. for(i=0;i<64;i++) {
  519. j = zigzag_direct[i];
  520. s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
  521. }
  522. len -= 65;
  523. }
  524. return 0;
  525. }
  526. /* decode huffman tables and build VLC decoders */
  527. static int mjpeg_decode_dht(MJpegDecodeContext *s,
  528. UINT8 *buf, int buf_size)
  529. {
  530. int len, index, i, class, n, v, code_max;
  531. UINT8 bits_table[17];
  532. UINT8 val_table[256];
  533. init_get_bits(&s->gb, buf, buf_size);
  534. len = get_bits(&s->gb, 16);
  535. len -= 2;
  536. while (len > 0) {
  537. if (len < 17)
  538. return -1;
  539. class = get_bits(&s->gb, 4);
  540. if (class >= 2)
  541. return -1;
  542. index = get_bits(&s->gb, 4);
  543. if (index >= 4)
  544. return -1;
  545. n = 0;
  546. for(i=1;i<=16;i++) {
  547. bits_table[i] = get_bits(&s->gb, 8);
  548. n += bits_table[i];
  549. }
  550. len -= 17;
  551. if (len < n || n > 256)
  552. return -1;
  553. code_max = 0;
  554. for(i=0;i<n;i++) {
  555. v = get_bits(&s->gb, 8);
  556. if (v > code_max)
  557. code_max = v;
  558. val_table[i] = v;
  559. }
  560. len -= n;
  561. /* build VLC and flush previous vlc if present */
  562. free_vlc(&s->vlcs[class][index]);
  563. dprintf("class=%d index=%d nb_codes=%d\n",
  564. class, index, code_max + 1);
  565. build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1);
  566. }
  567. return 0;
  568. }
  569. static int mjpeg_decode_sof0(MJpegDecodeContext *s,
  570. UINT8 *buf, int buf_size)
  571. {
  572. int len, nb_components, i, width, height;
  573. init_get_bits(&s->gb, buf, buf_size);
  574. /* XXX: verify len field validity */
  575. len = get_bits(&s->gb, 16);
  576. /* only 8 bits/component accepted */
  577. if (get_bits(&s->gb, 8) != 8)
  578. return -1;
  579. height = get_bits(&s->gb, 16);
  580. width = get_bits(&s->gb, 16);
  581. nb_components = get_bits(&s->gb, 8);
  582. if (nb_components <= 0 ||
  583. nb_components > MAX_COMPONENTS)
  584. return -1;
  585. s->nb_components = nb_components;
  586. s->h_max = 1;
  587. s->v_max = 1;
  588. for(i=0;i<nb_components;i++) {
  589. /* component id */
  590. s->component_id[i] = get_bits(&s->gb, 8) - 1;
  591. s->h_count[i] = get_bits(&s->gb, 4);
  592. s->v_count[i] = get_bits(&s->gb, 4);
  593. /* compute hmax and vmax (only used in interleaved case) */
  594. if (s->h_count[i] > s->h_max)
  595. s->h_max = s->h_count[i];
  596. if (s->v_count[i] > s->v_max)
  597. s->v_max = s->v_count[i];
  598. s->quant_index[i] = get_bits(&s->gb, 8);
  599. if (s->quant_index[i] >= 4)
  600. return -1;
  601. dprintf("component %d %d:%d\n", i, s->h_count[i], s->v_count[i]);
  602. }
  603. /* if different size, realloc/alloc picture */
  604. /* XXX: also check h_count and v_count */
  605. if (width != s->width || height != s->height) {
  606. for(i=0;i<MAX_COMPONENTS;i++) {
  607. free(s->current_picture[i]);
  608. s->current_picture[i] = NULL;
  609. }
  610. s->width = width;
  611. s->height = height;
  612. /* test interlaced mode */
  613. if (s->first_picture &&
  614. s->org_height != 0 &&
  615. s->height < ((s->org_height * 3) / 4)) {
  616. s->interlaced = 1;
  617. s->bottom_field = 0;
  618. }
  619. for(i=0;i<nb_components;i++) {
  620. int w, h;
  621. w = (s->width + 8 * s->h_max - 1) / (8 * s->h_max);
  622. h = (s->height + 8 * s->v_max - 1) / (8 * s->v_max);
  623. w = w * 8 * s->h_count[i];
  624. h = h * 8 * s->v_count[i];
  625. if (s->interlaced)
  626. w *= 2;
  627. s->linesize[i] = w;
  628. /* memory test is done in mjpeg_decode_sos() */
  629. s->current_picture[i] = av_mallocz(w * h);
  630. }
  631. s->first_picture = 0;
  632. }
  633. if (len != 8+(3*nb_components))
  634. dprintf("decode_sof0: error, len(%d) mismatch\n", len);
  635. return 0;
  636. }
  637. static inline int decode_dc(MJpegDecodeContext *s, int dc_index)
  638. {
  639. int code, diff;
  640. code = get_vlc(&s->gb, &s->vlcs[0][dc_index]);
  641. if (code < 0)
  642. {
  643. dprintf("decode_dc: bad vlc: %d:%d\n", 0, dc_index);
  644. return 0xffff;
  645. }
  646. if (code == 0) {
  647. diff = 0;
  648. // dprintf("decode_dc: bad code\n");
  649. } else {
  650. diff = get_bits(&s->gb, code);
  651. if ((diff & (1 << (code - 1))) == 0)
  652. diff = (-1 << code) | (diff + 1);
  653. }
  654. return diff;
  655. }
  656. /* decode block and dequantize */
  657. static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
  658. int component, int dc_index, int ac_index, int quant_index)
  659. {
  660. int nbits, code, i, j, level;
  661. int run, val;
  662. VLC *ac_vlc;
  663. INT16 *quant_matrix;
  664. /* DC coef */
  665. val = decode_dc(s, dc_index);
  666. if (val == 0xffff) {
  667. dprintf("error dc\n");
  668. return -1;
  669. }
  670. quant_matrix = s->quant_matrixes[quant_index];
  671. val = val * quant_matrix[0] + s->last_dc[component];
  672. s->last_dc[component] = val;
  673. block[0] = val;
  674. /* AC coefs */
  675. ac_vlc = &s->vlcs[1][ac_index];
  676. i = 1;
  677. for(;;) {
  678. code = get_vlc(&s->gb, ac_vlc);
  679. if (code < 0) {
  680. dprintf("error ac\n");
  681. return -1;
  682. }
  683. /* EOB */
  684. if (code == 0)
  685. break;
  686. if (code == 0xf0) {
  687. i += 16;
  688. } else {
  689. run = code >> 4;
  690. nbits = code & 0xf;
  691. level = get_bits(&s->gb, nbits);
  692. if ((level & (1 << (nbits - 1))) == 0)
  693. level = (-1 << nbits) | (level + 1);
  694. i += run;
  695. if (i >= 64) {
  696. dprintf("error count: %d\n", i);
  697. return -1;
  698. }
  699. j = zigzag_direct[i];
  700. block[j] = level * quant_matrix[j];
  701. i++;
  702. if (i >= 64)
  703. break;
  704. }
  705. }
  706. return 0;
  707. }
  708. static int mjpeg_decode_sos(MJpegDecodeContext *s,
  709. UINT8 *buf, int buf_size)
  710. {
  711. int len, nb_components, i, j, n, h, v, ret;
  712. int mb_width, mb_height, mb_x, mb_y, vmax, hmax, index, id;
  713. int comp_index[4];
  714. int dc_index[4];
  715. int ac_index[4];
  716. int nb_blocks[4];
  717. int h_count[4];
  718. int v_count[4];
  719. init_get_bits(&s->gb, buf, buf_size);
  720. /* XXX: verify len field validity */
  721. len = get_bits(&s->gb, 16);
  722. nb_components = get_bits(&s->gb, 8);
  723. /* XXX: only interleaved scan accepted */
  724. if (nb_components != 3)
  725. {
  726. dprintf("decode_sos: components(%d) mismatch\n", nb_components);
  727. return -1;
  728. }
  729. vmax = 0;
  730. hmax = 0;
  731. for(i=0;i<nb_components;i++) {
  732. id = get_bits(&s->gb, 8) - 1;
  733. /* find component index */
  734. for(index=0;index<s->nb_components;index++)
  735. if (id == s->component_id[index])
  736. break;
  737. if (index == s->nb_components)
  738. {
  739. dprintf("decode_sos: index out of components\n");
  740. return -1;
  741. }
  742. comp_index[i] = index;
  743. nb_blocks[i] = s->h_count[index] * s->v_count[index];
  744. h_count[i] = s->h_count[index];
  745. v_count[i] = s->v_count[index];
  746. dc_index[i] = get_bits(&s->gb, 4);
  747. ac_index[i] = get_bits(&s->gb, 4);
  748. if (dc_index[i] < 0 || ac_index[i] < 0 ||
  749. dc_index[i] >= 4 || ac_index[i] >= 4)
  750. goto out_of_range;
  751. switch(s->start_code)
  752. {
  753. case SOF0:
  754. if (dc_index[i] > 1 || ac_index[i] > 1)
  755. goto out_of_range;
  756. break;
  757. case SOF1:
  758. case SOF2:
  759. if (dc_index[i] > 3 || ac_index[i] > 3)
  760. goto out_of_range;
  761. break;
  762. case SOF3:
  763. if (dc_index[i] > 3 || ac_index[i] != 0)
  764. goto out_of_range;
  765. break;
  766. }
  767. }
  768. get_bits(&s->gb, 8); /* Ss */
  769. get_bits(&s->gb, 8); /* Se */
  770. get_bits(&s->gb, 8); /* Ah and Al (each are 4 bits) */
  771. for(i=0;i<nb_components;i++)
  772. s->last_dc[i] = 1024;
  773. if (nb_components > 1) {
  774. /* interleaved stream */
  775. mb_width = (s->width + s->h_max * 8 - 1) / (s->h_max * 8);
  776. mb_height = (s->height + s->v_max * 8 - 1) / (s->v_max * 8);
  777. } else {
  778. h = s->h_max / s->h_count[comp_index[0]];
  779. v = s->v_max / s->v_count[comp_index[0]];
  780. mb_width = (s->width + h * 8 - 1) / (h * 8);
  781. mb_height = (s->height + v * 8 - 1) / (v * 8);
  782. nb_blocks[0] = 1;
  783. h_count[0] = 1;
  784. v_count[0] = 1;
  785. }
  786. for(mb_y = 0; mb_y < mb_height; mb_y++) {
  787. for(mb_x = 0; mb_x < mb_width; mb_x++) {
  788. for(i=0;i<nb_components;i++) {
  789. UINT8 *ptr;
  790. int x, y, c;
  791. n = nb_blocks[i];
  792. c = comp_index[i];
  793. h = h_count[i];
  794. v = v_count[i];
  795. x = 0;
  796. y = 0;
  797. for(j=0;j<n;j++) {
  798. memset(s->block, 0, sizeof(s->block));
  799. if (decode_block(s, s->block, i,
  800. dc_index[i], ac_index[i],
  801. s->quant_index[c]) < 0) {
  802. dprintf("error %d %d\n", mb_y, mb_x);
  803. ret = -1;
  804. goto the_end;
  805. }
  806. // dprintf("mb: %d %d processed\n", mb_y, mb_x);
  807. ff_idct (s->block);
  808. ptr = s->current_picture[c] +
  809. (s->linesize[c] * (v * mb_y + y) * 8) +
  810. (h * mb_x + x) * 8;
  811. if (s->interlaced && s->bottom_field)
  812. ptr += s->linesize[c] >> 1;
  813. put_pixels_clamped(s->block, ptr, s->linesize[c]);
  814. if (++x == h) {
  815. x = 0;
  816. y++;
  817. }
  818. }
  819. }
  820. }
  821. }
  822. ret = 0;
  823. the_end:
  824. emms_c();
  825. return ret;
  826. out_of_range:
  827. dprintf("decode_sos: ac/dc index out of range\n");
  828. return -1;
  829. }
  830. /* return the 8 bit start code value and update the search
  831. state. Return -1 if no start code found */
  832. static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end,
  833. UINT32 *header_state)
  834. {
  835. UINT8 *buf_ptr;
  836. unsigned int state, v;
  837. int val;
  838. state = *header_state;
  839. buf_ptr = *pbuf_ptr;
  840. if (state) {
  841. /* get marker */
  842. found:
  843. if (buf_ptr < buf_end) {
  844. val = *buf_ptr++;
  845. state = 0;
  846. } else {
  847. val = -1;
  848. }
  849. } else {
  850. while (buf_ptr < buf_end) {
  851. v = *buf_ptr++;
  852. if (v == 0xff) {
  853. state = 1;
  854. goto found;
  855. }
  856. }
  857. val = -1;
  858. }
  859. *pbuf_ptr = buf_ptr;
  860. *header_state = state;
  861. return val;
  862. }
  863. static int mjpeg_decode_frame(AVCodecContext *avctx,
  864. void *data, int *data_size,
  865. UINT8 *buf, int buf_size)
  866. {
  867. MJpegDecodeContext *s = avctx->priv_data;
  868. UINT8 *buf_end, *buf_ptr, *buf_start;
  869. int len, code, input_size, i;
  870. AVPicture *picture = data;
  871. unsigned int start_code;
  872. *data_size = 0;
  873. /* no supplementary picture */
  874. if (buf_size == 0)
  875. return 0;
  876. buf_ptr = buf;
  877. buf_end = buf + buf_size;
  878. while (buf_ptr < buf_end) {
  879. buf_start = buf_ptr;
  880. /* find start next marker */
  881. code = find_marker(&buf_ptr, buf_end, &s->header_state);
  882. /* copy to buffer */
  883. len = buf_ptr - buf_start;
  884. if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
  885. /* data too big : flush */
  886. s->buf_ptr = s->buffer;
  887. if (code > 0)
  888. s->start_code = code;
  889. } else {
  890. memcpy(s->buf_ptr, buf_start, len);
  891. s->buf_ptr += len;
  892. /* if we got FF 00, we copy FF to the stream to unescape FF 00 */
  893. /* valid marker code is between 00 and ff - alex */
  894. if (code == 0 || code == 0xff) {
  895. s->buf_ptr--;
  896. } else if (code > 0) {
  897. /* prepare data for next start code */
  898. input_size = s->buf_ptr - s->buffer;
  899. start_code = s->start_code;
  900. s->buf_ptr = s->buffer;
  901. s->start_code = code;
  902. dprintf("marker=%x\n", start_code);
  903. switch(start_code) {
  904. case SOI:
  905. /* nothing to do on SOI */
  906. break;
  907. case DQT:
  908. mjpeg_decode_dqt(s, s->buffer, input_size);
  909. break;
  910. case DHT:
  911. mjpeg_decode_dht(s, s->buffer, input_size);
  912. break;
  913. case SOF0:
  914. mjpeg_decode_sof0(s, s->buffer, input_size);
  915. break;
  916. case SOS:
  917. mjpeg_decode_sos(s, s->buffer, input_size);
  918. if (s->start_code == EOI) {
  919. int l;
  920. if (s->interlaced) {
  921. s->bottom_field ^= 1;
  922. /* if not bottom field, do not output image yet */
  923. if (s->bottom_field)
  924. goto the_end;
  925. }
  926. for(i=0;i<3;i++) {
  927. picture->data[i] = s->current_picture[i];
  928. l = s->linesize[i];
  929. if (s->interlaced)
  930. l >>= 1;
  931. picture->linesize[i] = l;
  932. }
  933. *data_size = sizeof(AVPicture);
  934. avctx->height = s->height;
  935. if (s->interlaced)
  936. avctx->height *= 2;
  937. avctx->width = s->width;
  938. /* XXX: not complete test ! */
  939. switch((s->h_count[0] << 4) | s->v_count[0]) {
  940. case 0x11:
  941. avctx->pix_fmt = PIX_FMT_YUV444P;
  942. break;
  943. case 0x21:
  944. avctx->pix_fmt = PIX_FMT_YUV422P;
  945. break;
  946. default:
  947. case 0x22:
  948. avctx->pix_fmt = PIX_FMT_YUV420P;
  949. break;
  950. }
  951. /* dummy quality */
  952. /* XXX: infer it with matrix */
  953. avctx->quality = 3;
  954. goto the_end;
  955. }
  956. break;
  957. }
  958. }
  959. }
  960. }
  961. the_end:
  962. return buf_ptr - buf;
  963. }
  964. static int mjpeg_decode_end(AVCodecContext *avctx)
  965. {
  966. MJpegDecodeContext *s = avctx->priv_data;
  967. int i, j;
  968. for(i=0;i<MAX_COMPONENTS;i++)
  969. free(s->current_picture[i]);
  970. for(i=0;i<2;i++) {
  971. for(j=0;j<4;j++)
  972. free_vlc(&s->vlcs[i][j]);
  973. }
  974. return 0;
  975. }
  976. AVCodec mjpeg_decoder = {
  977. "mjpeg",
  978. CODEC_TYPE_VIDEO,
  979. CODEC_ID_MJPEG,
  980. sizeof(MJpegDecodeContext),
  981. mjpeg_decode_init,
  982. NULL,
  983. mjpeg_decode_end,
  984. mjpeg_decode_frame,
  985. };