mjpegenc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * MJPEG encoder
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. * Copyright (c) 2003 Alex Beregszaszi
  5. * Copyright (c) 2003-2004 Michael Niedermayer
  6. *
  7. * Support for external huffman table, various fixes (AVID workaround),
  8. * aspecting, new decode_frame mechanism and apple mjpeg-b support
  9. * by Alex Beregszaszi
  10. *
  11. * This file is part of FFmpeg.
  12. *
  13. * FFmpeg is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2.1 of the License, or (at your option) any later version.
  17. *
  18. * FFmpeg is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with FFmpeg; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. /**
  28. * @file libavcodec/mjpegenc.c
  29. * MJPEG encoder.
  30. */
  31. //#define DEBUG
  32. #include <assert.h>
  33. #include "avcodec.h"
  34. #include "dsputil.h"
  35. #include "mpegvideo.h"
  36. #include "mjpeg.h"
  37. #include "mjpegenc.h"
  38. /* use two quantizer tables (one for luminance and one for chrominance) */
  39. /* not yet working */
  40. #undef TWOMATRIXES
  41. av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
  42. {
  43. MJpegContext *m;
  44. m = av_malloc(sizeof(MJpegContext));
  45. if (!m)
  46. return -1;
  47. s->min_qcoeff=-1023;
  48. s->max_qcoeff= 1023;
  49. /* build all the huffman tables */
  50. ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
  51. m->huff_code_dc_luminance,
  52. ff_mjpeg_bits_dc_luminance,
  53. ff_mjpeg_val_dc);
  54. ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
  55. m->huff_code_dc_chrominance,
  56. ff_mjpeg_bits_dc_chrominance,
  57. ff_mjpeg_val_dc);
  58. ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
  59. m->huff_code_ac_luminance,
  60. ff_mjpeg_bits_ac_luminance,
  61. ff_mjpeg_val_ac_luminance);
  62. ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
  63. m->huff_code_ac_chrominance,
  64. ff_mjpeg_bits_ac_chrominance,
  65. ff_mjpeg_val_ac_chrominance);
  66. s->mjpeg_ctx = m;
  67. return 0;
  68. }
  69. void ff_mjpeg_encode_close(MpegEncContext *s)
  70. {
  71. av_free(s->mjpeg_ctx);
  72. }
  73. /* table_class: 0 = DC coef, 1 = AC coefs */
  74. static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
  75. const uint8_t *bits_table, const uint8_t *value_table)
  76. {
  77. PutBitContext *p = &s->pb;
  78. int n, i;
  79. put_bits(p, 4, table_class);
  80. put_bits(p, 4, table_id);
  81. n = 0;
  82. for(i=1;i<=16;i++) {
  83. n += bits_table[i];
  84. put_bits(p, 8, bits_table[i]);
  85. }
  86. for(i=0;i<n;i++)
  87. put_bits(p, 8, value_table[i]);
  88. return n + 17;
  89. }
  90. static void jpeg_table_header(MpegEncContext *s)
  91. {
  92. PutBitContext *p = &s->pb;
  93. int i, j, size;
  94. uint8_t *ptr;
  95. /* quant matrixes */
  96. put_marker(p, DQT);
  97. #ifdef TWOMATRIXES
  98. put_bits(p, 16, 2 + 2 * (1 + 64));
  99. #else
  100. put_bits(p, 16, 2 + 1 * (1 + 64));
  101. #endif
  102. put_bits(p, 4, 0); /* 8 bit precision */
  103. put_bits(p, 4, 0); /* table 0 */
  104. for(i=0;i<64;i++) {
  105. j = s->intra_scantable.permutated[i];
  106. put_bits(p, 8, s->intra_matrix[j]);
  107. }
  108. #ifdef TWOMATRIXES
  109. put_bits(p, 4, 0); /* 8 bit precision */
  110. put_bits(p, 4, 1); /* table 1 */
  111. for(i=0;i<64;i++) {
  112. j = s->intra_scantable.permutated[i];
  113. put_bits(p, 8, s->chroma_intra_matrix[j]);
  114. }
  115. #endif
  116. /* huffman table */
  117. put_marker(p, DHT);
  118. flush_put_bits(p);
  119. ptr = pbBufPtr(p);
  120. put_bits(p, 16, 0); /* patched later */
  121. size = 2;
  122. size += put_huffman_table(s, 0, 0, ff_mjpeg_bits_dc_luminance,
  123. ff_mjpeg_val_dc);
  124. size += put_huffman_table(s, 0, 1, ff_mjpeg_bits_dc_chrominance,
  125. ff_mjpeg_val_dc);
  126. size += put_huffman_table(s, 1, 0, ff_mjpeg_bits_ac_luminance,
  127. ff_mjpeg_val_ac_luminance);
  128. size += put_huffman_table(s, 1, 1, ff_mjpeg_bits_ac_chrominance,
  129. ff_mjpeg_val_ac_chrominance);
  130. AV_WB16(ptr, size);
  131. }
  132. static void jpeg_put_comments(MpegEncContext *s)
  133. {
  134. PutBitContext *p = &s->pb;
  135. int size;
  136. uint8_t *ptr;
  137. if (s->aspect_ratio_info /* && !lossless */)
  138. {
  139. /* JFIF header */
  140. put_marker(p, APP0);
  141. put_bits(p, 16, 16);
  142. ff_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
  143. put_bits(p, 16, 0x0201); /* v 1.02 */
  144. put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
  145. put_bits(p, 16, s->avctx->sample_aspect_ratio.num);
  146. put_bits(p, 16, s->avctx->sample_aspect_ratio.den);
  147. put_bits(p, 8, 0); /* thumbnail width */
  148. put_bits(p, 8, 0); /* thumbnail height */
  149. }
  150. /* comment */
  151. if(!(s->flags & CODEC_FLAG_BITEXACT)){
  152. put_marker(p, COM);
  153. flush_put_bits(p);
  154. ptr = pbBufPtr(p);
  155. put_bits(p, 16, 0); /* patched later */
  156. ff_put_string(p, LIBAVCODEC_IDENT, 1);
  157. size = strlen(LIBAVCODEC_IDENT)+3;
  158. AV_WB16(ptr, size);
  159. }
  160. if( s->avctx->pix_fmt == PIX_FMT_YUV420P
  161. ||s->avctx->pix_fmt == PIX_FMT_YUV422P
  162. ||s->avctx->pix_fmt == PIX_FMT_YUV444P){
  163. put_marker(p, COM);
  164. flush_put_bits(p);
  165. ptr = pbBufPtr(p);
  166. put_bits(p, 16, 0); /* patched later */
  167. ff_put_string(p, "CS=ITU601", 1);
  168. size = strlen("CS=ITU601")+3;
  169. AV_WB16(ptr, size);
  170. }
  171. }
  172. void ff_mjpeg_encode_picture_header(MpegEncContext *s)
  173. {
  174. const int lossless= s->avctx->codec_id != CODEC_ID_MJPEG;
  175. put_marker(&s->pb, SOI);
  176. jpeg_put_comments(s);
  177. jpeg_table_header(s);
  178. switch(s->avctx->codec_id){
  179. case CODEC_ID_MJPEG: put_marker(&s->pb, SOF0 ); break;
  180. case CODEC_ID_LJPEG: put_marker(&s->pb, SOF3 ); break;
  181. default: assert(0);
  182. }
  183. put_bits(&s->pb, 16, 17);
  184. if(lossless && s->avctx->pix_fmt == PIX_FMT_RGB32)
  185. put_bits(&s->pb, 8, 9); /* 9 bits/component RCT */
  186. else
  187. put_bits(&s->pb, 8, 8); /* 8 bits/component */
  188. put_bits(&s->pb, 16, s->height);
  189. put_bits(&s->pb, 16, s->width);
  190. put_bits(&s->pb, 8, 3); /* 3 components */
  191. /* Y component */
  192. put_bits(&s->pb, 8, 1); /* component number */
  193. put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
  194. put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
  195. put_bits(&s->pb, 8, 0); /* select matrix */
  196. /* Cb component */
  197. put_bits(&s->pb, 8, 2); /* component number */
  198. put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
  199. put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
  200. #ifdef TWOMATRIXES
  201. put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
  202. #else
  203. put_bits(&s->pb, 8, 0); /* select matrix */
  204. #endif
  205. /* Cr component */
  206. put_bits(&s->pb, 8, 3); /* component number */
  207. put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
  208. put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
  209. #ifdef TWOMATRIXES
  210. put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
  211. #else
  212. put_bits(&s->pb, 8, 0); /* select matrix */
  213. #endif
  214. /* scan header */
  215. put_marker(&s->pb, SOS);
  216. put_bits(&s->pb, 16, 12); /* length */
  217. put_bits(&s->pb, 8, 3); /* 3 components */
  218. /* Y component */
  219. put_bits(&s->pb, 8, 1); /* index */
  220. put_bits(&s->pb, 4, 0); /* DC huffman table index */
  221. put_bits(&s->pb, 4, 0); /* AC huffman table index */
  222. /* Cb component */
  223. put_bits(&s->pb, 8, 2); /* index */
  224. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  225. put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
  226. /* Cr component */
  227. put_bits(&s->pb, 8, 3); /* index */
  228. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  229. put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
  230. put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0); /* Ss (not used) */
  231. switch(s->avctx->codec_id){
  232. case CODEC_ID_MJPEG: put_bits(&s->pb, 8, 63); break; /* Se (not used) */
  233. case CODEC_ID_LJPEG: put_bits(&s->pb, 8, 0); break; /* not used */
  234. default: assert(0);
  235. }
  236. put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
  237. }
  238. static void escape_FF(MpegEncContext *s, int start)
  239. {
  240. int size= put_bits_count(&s->pb) - start*8;
  241. int i, ff_count;
  242. uint8_t *buf= s->pb.buf + start;
  243. int align= (-(size_t)(buf))&3;
  244. assert((size&7) == 0);
  245. size >>= 3;
  246. ff_count=0;
  247. for(i=0; i<size && i<align; i++){
  248. if(buf[i]==0xFF) ff_count++;
  249. }
  250. for(; i<size-15; i+=16){
  251. int acc, v;
  252. v= *(uint32_t*)(&buf[i]);
  253. acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  254. v= *(uint32_t*)(&buf[i+4]);
  255. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  256. v= *(uint32_t*)(&buf[i+8]);
  257. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  258. v= *(uint32_t*)(&buf[i+12]);
  259. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  260. acc>>=4;
  261. acc+= (acc>>16);
  262. acc+= (acc>>8);
  263. ff_count+= acc&0xFF;
  264. }
  265. for(; i<size; i++){
  266. if(buf[i]==0xFF) ff_count++;
  267. }
  268. if(ff_count==0) return;
  269. /* skip put bits */
  270. for(i=0; i<ff_count-3; i+=4)
  271. put_bits(&s->pb, 32, 0);
  272. put_bits(&s->pb, (ff_count-i)*8, 0);
  273. flush_put_bits(&s->pb);
  274. for(i=size-1; ff_count; i--){
  275. int v= buf[i];
  276. if(v==0xFF){
  277. //printf("%d %d\n", i, ff_count);
  278. buf[i+ff_count]= 0;
  279. ff_count--;
  280. }
  281. buf[i+ff_count]= v;
  282. }
  283. }
  284. void ff_mjpeg_encode_stuffing(PutBitContext * pbc)
  285. {
  286. int length;
  287. length= (-put_bits_count(pbc))&7;
  288. if(length) put_bits(pbc, length, (1<<length)-1);
  289. }
  290. void ff_mjpeg_encode_picture_trailer(MpegEncContext *s)
  291. {
  292. ff_mjpeg_encode_stuffing(&s->pb);
  293. flush_put_bits(&s->pb);
  294. assert((s->header_bits&7)==0);
  295. escape_FF(s, s->header_bits>>3);
  296. put_marker(&s->pb, EOI);
  297. }
  298. void ff_mjpeg_encode_dc(MpegEncContext *s, int val,
  299. uint8_t *huff_size, uint16_t *huff_code)
  300. {
  301. int mant, nbits;
  302. if (val == 0) {
  303. put_bits(&s->pb, huff_size[0], huff_code[0]);
  304. } else {
  305. mant = val;
  306. if (val < 0) {
  307. val = -val;
  308. mant--;
  309. }
  310. nbits= av_log2_16bit(val) + 1;
  311. put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
  312. put_sbits(&s->pb, nbits, mant);
  313. }
  314. }
  315. static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
  316. {
  317. int mant, nbits, code, i, j;
  318. int component, dc, run, last_index, val;
  319. MJpegContext *m = s->mjpeg_ctx;
  320. uint8_t *huff_size_ac;
  321. uint16_t *huff_code_ac;
  322. /* DC coef */
  323. component = (n <= 3 ? 0 : (n&1) + 1);
  324. dc = block[0]; /* overflow is impossible */
  325. val = dc - s->last_dc[component];
  326. if (n < 4) {
  327. ff_mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
  328. huff_size_ac = m->huff_size_ac_luminance;
  329. huff_code_ac = m->huff_code_ac_luminance;
  330. } else {
  331. ff_mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  332. huff_size_ac = m->huff_size_ac_chrominance;
  333. huff_code_ac = m->huff_code_ac_chrominance;
  334. }
  335. s->last_dc[component] = dc;
  336. /* AC coefs */
  337. run = 0;
  338. last_index = s->block_last_index[n];
  339. for(i=1;i<=last_index;i++) {
  340. j = s->intra_scantable.permutated[i];
  341. val = block[j];
  342. if (val == 0) {
  343. run++;
  344. } else {
  345. while (run >= 16) {
  346. put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
  347. run -= 16;
  348. }
  349. mant = val;
  350. if (val < 0) {
  351. val = -val;
  352. mant--;
  353. }
  354. nbits= av_log2(val) + 1;
  355. code = (run << 4) | nbits;
  356. put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
  357. put_sbits(&s->pb, nbits, mant);
  358. run = 0;
  359. }
  360. }
  361. /* output EOB only if not already 64 values */
  362. if (last_index < 63 || run != 0)
  363. put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
  364. }
  365. void ff_mjpeg_encode_mb(MpegEncContext *s, DCTELEM block[6][64])
  366. {
  367. int i;
  368. for(i=0;i<5;i++) {
  369. encode_block(s, block[i], i);
  370. }
  371. if (s->chroma_format == CHROMA_420) {
  372. encode_block(s, block[5], 5);
  373. } else {
  374. encode_block(s, block[6], 6);
  375. encode_block(s, block[5], 5);
  376. encode_block(s, block[7], 7);
  377. }
  378. }
  379. AVCodec mjpeg_encoder = {
  380. "mjpeg",
  381. CODEC_TYPE_VIDEO,
  382. CODEC_ID_MJPEG,
  383. sizeof(MpegEncContext),
  384. MPV_encode_init,
  385. MPV_encode_picture,
  386. MPV_encode_end,
  387. .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_NONE},
  388. .long_name= NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
  389. };