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.

500 lines
14KB

  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
  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 = put_bits_ptr(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->avctx->sample_aspect_ratio.num /* && !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, 0x0102); /* 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 = put_bits_ptr(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 = put_bits_ptr(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. // hack for AMV mjpeg format
  177. if(s->avctx->codec_id == CODEC_ID_AMV) return;
  178. jpeg_put_comments(s);
  179. jpeg_table_header(s);
  180. switch(s->avctx->codec_id){
  181. case CODEC_ID_MJPEG: put_marker(&s->pb, SOF0 ); break;
  182. case CODEC_ID_LJPEG: put_marker(&s->pb, SOF3 ); break;
  183. default: assert(0);
  184. }
  185. put_bits(&s->pb, 16, 17);
  186. if(lossless && (s->avctx->pix_fmt == PIX_FMT_BGR0
  187. || s->avctx->pix_fmt == PIX_FMT_BGRA
  188. || s->avctx->pix_fmt == PIX_FMT_BGR24))
  189. put_bits(&s->pb, 8, 9); /* 9 bits/component RCT */
  190. else
  191. put_bits(&s->pb, 8, 8); /* 8 bits/component */
  192. put_bits(&s->pb, 16, s->height);
  193. put_bits(&s->pb, 16, s->width);
  194. put_bits(&s->pb, 8, 3); /* 3 components */
  195. /* Y component */
  196. put_bits(&s->pb, 8, 1); /* component number */
  197. put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
  198. put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
  199. put_bits(&s->pb, 8, 0); /* select matrix */
  200. /* Cb component */
  201. put_bits(&s->pb, 8, 2); /* component number */
  202. put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
  203. put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
  204. #ifdef TWOMATRIXES
  205. put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
  206. #else
  207. put_bits(&s->pb, 8, 0); /* select matrix */
  208. #endif
  209. /* Cr component */
  210. put_bits(&s->pb, 8, 3); /* component number */
  211. put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
  212. put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
  213. #ifdef TWOMATRIXES
  214. put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
  215. #else
  216. put_bits(&s->pb, 8, 0); /* select matrix */
  217. #endif
  218. /* scan header */
  219. put_marker(&s->pb, SOS);
  220. put_bits(&s->pb, 16, 12); /* length */
  221. put_bits(&s->pb, 8, 3); /* 3 components */
  222. /* Y component */
  223. put_bits(&s->pb, 8, 1); /* index */
  224. put_bits(&s->pb, 4, 0); /* DC huffman table index */
  225. put_bits(&s->pb, 4, 0); /* AC huffman table index */
  226. /* Cb component */
  227. put_bits(&s->pb, 8, 2); /* 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. /* Cr component */
  231. put_bits(&s->pb, 8, 3); /* index */
  232. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  233. put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
  234. put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0); /* Ss (not used) */
  235. switch(s->avctx->codec_id){
  236. case CODEC_ID_MJPEG: put_bits(&s->pb, 8, 63); break; /* Se (not used) */
  237. case CODEC_ID_LJPEG: put_bits(&s->pb, 8, 0); break; /* not used */
  238. default: assert(0);
  239. }
  240. put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
  241. }
  242. static void escape_FF(MpegEncContext *s, int start)
  243. {
  244. int size= put_bits_count(&s->pb) - start*8;
  245. int i, ff_count;
  246. uint8_t *buf= s->pb.buf + start;
  247. int align= (-(size_t)(buf))&3;
  248. assert((size&7) == 0);
  249. size >>= 3;
  250. ff_count=0;
  251. for(i=0; i<size && i<align; i++){
  252. if(buf[i]==0xFF) ff_count++;
  253. }
  254. for(; i<size-15; i+=16){
  255. int acc, v;
  256. v= *(uint32_t*)(&buf[i]);
  257. acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  258. v= *(uint32_t*)(&buf[i+4]);
  259. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  260. v= *(uint32_t*)(&buf[i+8]);
  261. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  262. v= *(uint32_t*)(&buf[i+12]);
  263. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  264. acc>>=4;
  265. acc+= (acc>>16);
  266. acc+= (acc>>8);
  267. ff_count+= acc&0xFF;
  268. }
  269. for(; i<size; i++){
  270. if(buf[i]==0xFF) ff_count++;
  271. }
  272. if(ff_count==0) return;
  273. flush_put_bits(&s->pb);
  274. skip_put_bytes(&s->pb, ff_count);
  275. for(i=size-1; ff_count; i--){
  276. int v= buf[i];
  277. if(v==0xFF){
  278. //printf("%d %d\n", i, ff_count);
  279. buf[i+ff_count]= 0;
  280. ff_count--;
  281. }
  282. buf[i+ff_count]= v;
  283. }
  284. }
  285. void ff_mjpeg_encode_stuffing(PutBitContext * pbc)
  286. {
  287. int length;
  288. length= (-put_bits_count(pbc))&7;
  289. if(length) put_bits(pbc, length, (1<<length)-1);
  290. }
  291. void ff_mjpeg_encode_picture_trailer(MpegEncContext *s)
  292. {
  293. ff_mjpeg_encode_stuffing(&s->pb);
  294. flush_put_bits(&s->pb);
  295. assert((s->header_bits&7)==0);
  296. escape_FF(s, s->header_bits>>3);
  297. put_marker(&s->pb, EOI);
  298. }
  299. void ff_mjpeg_encode_dc(MpegEncContext *s, int val,
  300. uint8_t *huff_size, uint16_t *huff_code)
  301. {
  302. int mant, nbits;
  303. if (val == 0) {
  304. put_bits(&s->pb, huff_size[0], huff_code[0]);
  305. } else {
  306. mant = val;
  307. if (val < 0) {
  308. val = -val;
  309. mant--;
  310. }
  311. nbits= av_log2_16bit(val) + 1;
  312. put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
  313. put_sbits(&s->pb, nbits, mant);
  314. }
  315. }
  316. static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
  317. {
  318. int mant, nbits, code, i, j;
  319. int component, dc, run, last_index, val;
  320. MJpegContext *m = s->mjpeg_ctx;
  321. uint8_t *huff_size_ac;
  322. uint16_t *huff_code_ac;
  323. /* DC coef */
  324. component = (n <= 3 ? 0 : (n&1) + 1);
  325. dc = block[0]; /* overflow is impossible */
  326. val = dc - s->last_dc[component];
  327. if (n < 4) {
  328. ff_mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
  329. huff_size_ac = m->huff_size_ac_luminance;
  330. huff_code_ac = m->huff_code_ac_luminance;
  331. } else {
  332. ff_mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  333. huff_size_ac = m->huff_size_ac_chrominance;
  334. huff_code_ac = m->huff_code_ac_chrominance;
  335. }
  336. s->last_dc[component] = dc;
  337. /* AC coefs */
  338. run = 0;
  339. last_index = s->block_last_index[n];
  340. for(i=1;i<=last_index;i++) {
  341. j = s->intra_scantable.permutated[i];
  342. val = block[j];
  343. if (val == 0) {
  344. run++;
  345. } else {
  346. while (run >= 16) {
  347. put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
  348. run -= 16;
  349. }
  350. mant = val;
  351. if (val < 0) {
  352. val = -val;
  353. mant--;
  354. }
  355. nbits= av_log2(val) + 1;
  356. code = (run << 4) | nbits;
  357. put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
  358. put_sbits(&s->pb, nbits, mant);
  359. run = 0;
  360. }
  361. }
  362. /* output EOB only if not already 64 values */
  363. if (last_index < 63 || run != 0)
  364. put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
  365. }
  366. void ff_mjpeg_encode_mb(MpegEncContext *s, DCTELEM block[6][64])
  367. {
  368. int i;
  369. for(i=0;i<5;i++) {
  370. encode_block(s, block[i], i);
  371. }
  372. if (s->chroma_format == CHROMA_420) {
  373. encode_block(s, block[5], 5);
  374. } else {
  375. encode_block(s, block[6], 6);
  376. encode_block(s, block[5], 5);
  377. encode_block(s, block[7], 7);
  378. }
  379. s->i_tex_bits += get_bits_diff(s);
  380. }
  381. // maximum over s->mjpeg_vsample[i]
  382. #define V_MAX 2
  383. static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
  384. const AVFrame *pic_arg, int *got_packet)
  385. {
  386. MpegEncContext *s = avctx->priv_data;
  387. AVFrame pic = *pic_arg;
  388. int i;
  389. //CODEC_FLAG_EMU_EDGE have to be cleared
  390. if(s->avctx->flags & CODEC_FLAG_EMU_EDGE)
  391. return -1;
  392. //picture should be flipped upside-down
  393. for(i=0; i < 3; i++) {
  394. pic.data[i] += (pic.linesize[i] * (s->mjpeg_vsample[i] * (8 * s->mb_height -((s->height/V_MAX)&7)) - 1 ));
  395. pic.linesize[i] *= -1;
  396. }
  397. return ff_MPV_encode_picture(avctx, pkt, &pic, got_packet);
  398. }
  399. AVCodec ff_mjpeg_encoder = {
  400. .name = "mjpeg",
  401. .type = AVMEDIA_TYPE_VIDEO,
  402. .id = CODEC_ID_MJPEG,
  403. .priv_data_size = sizeof(MpegEncContext),
  404. .init = ff_MPV_encode_init,
  405. .encode2 = ff_MPV_encode_picture,
  406. .close = ff_MPV_encode_end,
  407. .pix_fmts = (const enum PixelFormat[]){
  408. PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_NONE
  409. },
  410. .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
  411. };
  412. AVCodec ff_amv_encoder = {
  413. .name = "amv",
  414. .type = AVMEDIA_TYPE_VIDEO,
  415. .id = CODEC_ID_AMV,
  416. .priv_data_size = sizeof(MpegEncContext),
  417. .init = ff_MPV_encode_init,
  418. .encode2 = amv_encode_picture,
  419. .close = ff_MPV_encode_end,
  420. .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_NONE},
  421. .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
  422. };