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.

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