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.

2414 lines
77KB

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