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.

505 lines
15KB

  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. if (s->width > 65500 || s->height > 65500) {
  45. av_log(s, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
  46. return -1;
  47. }
  48. m = av_malloc(sizeof(MJpegContext));
  49. if (!m)
  50. return -1;
  51. s->min_qcoeff=-1023;
  52. s->max_qcoeff= 1023;
  53. /* build all the huffman tables */
  54. ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
  55. m->huff_code_dc_luminance,
  56. ff_mjpeg_bits_dc_luminance,
  57. ff_mjpeg_val_dc);
  58. ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
  59. m->huff_code_dc_chrominance,
  60. ff_mjpeg_bits_dc_chrominance,
  61. ff_mjpeg_val_dc);
  62. ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
  63. m->huff_code_ac_luminance,
  64. ff_mjpeg_bits_ac_luminance,
  65. ff_mjpeg_val_ac_luminance);
  66. ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
  67. m->huff_code_ac_chrominance,
  68. ff_mjpeg_bits_ac_chrominance,
  69. ff_mjpeg_val_ac_chrominance);
  70. s->mjpeg_ctx = m;
  71. return 0;
  72. }
  73. void ff_mjpeg_encode_close(MpegEncContext *s)
  74. {
  75. av_free(s->mjpeg_ctx);
  76. }
  77. /* table_class: 0 = DC coef, 1 = AC coefs */
  78. static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
  79. const uint8_t *bits_table, const uint8_t *value_table)
  80. {
  81. PutBitContext *p = &s->pb;
  82. int n, i;
  83. put_bits(p, 4, table_class);
  84. put_bits(p, 4, table_id);
  85. n = 0;
  86. for(i=1;i<=16;i++) {
  87. n += bits_table[i];
  88. put_bits(p, 8, bits_table[i]);
  89. }
  90. for(i=0;i<n;i++)
  91. put_bits(p, 8, value_table[i]);
  92. return n + 17;
  93. }
  94. static void jpeg_table_header(MpegEncContext *s)
  95. {
  96. PutBitContext *p = &s->pb;
  97. int i, j, size;
  98. uint8_t *ptr;
  99. /* quant matrixes */
  100. put_marker(p, DQT);
  101. #ifdef TWOMATRIXES
  102. put_bits(p, 16, 2 + 2 * (1 + 64));
  103. #else
  104. put_bits(p, 16, 2 + 1 * (1 + 64));
  105. #endif
  106. put_bits(p, 4, 0); /* 8 bit precision */
  107. put_bits(p, 4, 0); /* table 0 */
  108. for(i=0;i<64;i++) {
  109. j = s->intra_scantable.permutated[i];
  110. put_bits(p, 8, s->intra_matrix[j]);
  111. }
  112. #ifdef TWOMATRIXES
  113. put_bits(p, 4, 0); /* 8 bit precision */
  114. put_bits(p, 4, 1); /* table 1 */
  115. for(i=0;i<64;i++) {
  116. j = s->intra_scantable.permutated[i];
  117. put_bits(p, 8, s->chroma_intra_matrix[j]);
  118. }
  119. #endif
  120. /* huffman table */
  121. put_marker(p, DHT);
  122. flush_put_bits(p);
  123. ptr = put_bits_ptr(p);
  124. put_bits(p, 16, 0); /* patched later */
  125. size = 2;
  126. size += put_huffman_table(s, 0, 0, ff_mjpeg_bits_dc_luminance,
  127. ff_mjpeg_val_dc);
  128. size += put_huffman_table(s, 0, 1, ff_mjpeg_bits_dc_chrominance,
  129. ff_mjpeg_val_dc);
  130. size += put_huffman_table(s, 1, 0, ff_mjpeg_bits_ac_luminance,
  131. ff_mjpeg_val_ac_luminance);
  132. size += put_huffman_table(s, 1, 1, ff_mjpeg_bits_ac_chrominance,
  133. ff_mjpeg_val_ac_chrominance);
  134. AV_WB16(ptr, size);
  135. }
  136. static void jpeg_put_comments(MpegEncContext *s)
  137. {
  138. PutBitContext *p = &s->pb;
  139. int size;
  140. uint8_t *ptr;
  141. if (s->avctx->sample_aspect_ratio.num /* && !lossless */)
  142. {
  143. /* JFIF header */
  144. put_marker(p, APP0);
  145. put_bits(p, 16, 16);
  146. ff_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
  147. put_bits(p, 16, 0x0102); /* v 1.02 */
  148. put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
  149. put_bits(p, 16, s->avctx->sample_aspect_ratio.num);
  150. put_bits(p, 16, s->avctx->sample_aspect_ratio.den);
  151. put_bits(p, 8, 0); /* thumbnail width */
  152. put_bits(p, 8, 0); /* thumbnail height */
  153. }
  154. /* comment */
  155. if(!(s->flags & CODEC_FLAG_BITEXACT)){
  156. put_marker(p, COM);
  157. flush_put_bits(p);
  158. ptr = put_bits_ptr(p);
  159. put_bits(p, 16, 0); /* patched later */
  160. ff_put_string(p, LIBAVCODEC_IDENT, 1);
  161. size = strlen(LIBAVCODEC_IDENT)+3;
  162. AV_WB16(ptr, size);
  163. }
  164. if( s->avctx->pix_fmt == PIX_FMT_YUV420P
  165. ||s->avctx->pix_fmt == PIX_FMT_YUV422P
  166. ||s->avctx->pix_fmt == PIX_FMT_YUV444P){
  167. put_marker(p, COM);
  168. flush_put_bits(p);
  169. ptr = put_bits_ptr(p);
  170. put_bits(p, 16, 0); /* patched later */
  171. ff_put_string(p, "CS=ITU601", 1);
  172. size = strlen("CS=ITU601")+3;
  173. AV_WB16(ptr, size);
  174. }
  175. }
  176. void ff_mjpeg_encode_picture_header(MpegEncContext *s)
  177. {
  178. const int lossless= s->avctx->codec_id != CODEC_ID_MJPEG;
  179. put_marker(&s->pb, SOI);
  180. // hack for AMV mjpeg format
  181. if(s->avctx->codec_id == CODEC_ID_AMV) return;
  182. jpeg_put_comments(s);
  183. jpeg_table_header(s);
  184. switch(s->avctx->codec_id){
  185. case CODEC_ID_MJPEG: put_marker(&s->pb, SOF0 ); break;
  186. case CODEC_ID_LJPEG: put_marker(&s->pb, SOF3 ); break;
  187. default: assert(0);
  188. }
  189. put_bits(&s->pb, 16, 17);
  190. if(lossless && (s->avctx->pix_fmt == PIX_FMT_BGR0
  191. || s->avctx->pix_fmt == PIX_FMT_BGRA
  192. || s->avctx->pix_fmt == PIX_FMT_BGR24))
  193. put_bits(&s->pb, 8, 9); /* 9 bits/component RCT */
  194. else
  195. put_bits(&s->pb, 8, 8); /* 8 bits/component */
  196. put_bits(&s->pb, 16, s->height);
  197. put_bits(&s->pb, 16, s->width);
  198. put_bits(&s->pb, 8, 3); /* 3 components */
  199. /* Y component */
  200. put_bits(&s->pb, 8, 1); /* component number */
  201. put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
  202. put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
  203. put_bits(&s->pb, 8, 0); /* select matrix */
  204. /* Cb component */
  205. put_bits(&s->pb, 8, 2); /* component number */
  206. put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
  207. put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
  208. #ifdef TWOMATRIXES
  209. put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
  210. #else
  211. put_bits(&s->pb, 8, 0); /* select matrix */
  212. #endif
  213. /* Cr component */
  214. put_bits(&s->pb, 8, 3); /* component number */
  215. put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
  216. put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
  217. #ifdef TWOMATRIXES
  218. put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
  219. #else
  220. put_bits(&s->pb, 8, 0); /* select matrix */
  221. #endif
  222. /* scan header */
  223. put_marker(&s->pb, SOS);
  224. put_bits(&s->pb, 16, 12); /* length */
  225. put_bits(&s->pb, 8, 3); /* 3 components */
  226. /* Y component */
  227. put_bits(&s->pb, 8, 1); /* index */
  228. put_bits(&s->pb, 4, 0); /* DC huffman table index */
  229. put_bits(&s->pb, 4, 0); /* AC huffman table index */
  230. /* Cb component */
  231. put_bits(&s->pb, 8, 2); /* 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. /* Cr component */
  235. put_bits(&s->pb, 8, 3); /* index */
  236. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  237. put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
  238. put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0); /* Ss (not used) */
  239. switch(s->avctx->codec_id){
  240. case CODEC_ID_MJPEG: put_bits(&s->pb, 8, 63); break; /* Se (not used) */
  241. case CODEC_ID_LJPEG: put_bits(&s->pb, 8, 0); break; /* not used */
  242. default: assert(0);
  243. }
  244. put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
  245. }
  246. static void escape_FF(MpegEncContext *s, int start)
  247. {
  248. int size= put_bits_count(&s->pb) - start*8;
  249. int i, ff_count;
  250. uint8_t *buf= s->pb.buf + start;
  251. int align= (-(size_t)(buf))&3;
  252. assert((size&7) == 0);
  253. size >>= 3;
  254. ff_count=0;
  255. for(i=0; i<size && i<align; i++){
  256. if(buf[i]==0xFF) ff_count++;
  257. }
  258. for(; i<size-15; i+=16){
  259. int acc, v;
  260. v= *(uint32_t*)(&buf[i]);
  261. acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  262. v= *(uint32_t*)(&buf[i+4]);
  263. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  264. v= *(uint32_t*)(&buf[i+8]);
  265. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  266. v= *(uint32_t*)(&buf[i+12]);
  267. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  268. acc>>=4;
  269. acc+= (acc>>16);
  270. acc+= (acc>>8);
  271. ff_count+= acc&0xFF;
  272. }
  273. for(; i<size; i++){
  274. if(buf[i]==0xFF) ff_count++;
  275. }
  276. if(ff_count==0) return;
  277. flush_put_bits(&s->pb);
  278. skip_put_bytes(&s->pb, ff_count);
  279. for(i=size-1; ff_count; i--){
  280. int v= buf[i];
  281. if(v==0xFF){
  282. //printf("%d %d\n", i, ff_count);
  283. buf[i+ff_count]= 0;
  284. ff_count--;
  285. }
  286. buf[i+ff_count]= v;
  287. }
  288. }
  289. void ff_mjpeg_encode_stuffing(PutBitContext * pbc)
  290. {
  291. int length;
  292. length= (-put_bits_count(pbc))&7;
  293. if(length) put_bits(pbc, length, (1<<length)-1);
  294. }
  295. void ff_mjpeg_encode_picture_trailer(MpegEncContext *s)
  296. {
  297. ff_mjpeg_encode_stuffing(&s->pb);
  298. flush_put_bits(&s->pb);
  299. assert((s->header_bits&7)==0);
  300. escape_FF(s, s->header_bits>>3);
  301. put_marker(&s->pb, EOI);
  302. }
  303. void ff_mjpeg_encode_dc(MpegEncContext *s, int val,
  304. uint8_t *huff_size, uint16_t *huff_code)
  305. {
  306. int mant, nbits;
  307. if (val == 0) {
  308. put_bits(&s->pb, huff_size[0], huff_code[0]);
  309. } else {
  310. mant = val;
  311. if (val < 0) {
  312. val = -val;
  313. mant--;
  314. }
  315. nbits= av_log2_16bit(val) + 1;
  316. put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
  317. put_sbits(&s->pb, nbits, mant);
  318. }
  319. }
  320. static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
  321. {
  322. int mant, nbits, code, i, j;
  323. int component, dc, run, last_index, val;
  324. MJpegContext *m = s->mjpeg_ctx;
  325. uint8_t *huff_size_ac;
  326. uint16_t *huff_code_ac;
  327. /* DC coef */
  328. component = (n <= 3 ? 0 : (n&1) + 1);
  329. dc = block[0]; /* overflow is impossible */
  330. val = dc - s->last_dc[component];
  331. if (n < 4) {
  332. ff_mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
  333. huff_size_ac = m->huff_size_ac_luminance;
  334. huff_code_ac = m->huff_code_ac_luminance;
  335. } else {
  336. ff_mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  337. huff_size_ac = m->huff_size_ac_chrominance;
  338. huff_code_ac = m->huff_code_ac_chrominance;
  339. }
  340. s->last_dc[component] = dc;
  341. /* AC coefs */
  342. run = 0;
  343. last_index = s->block_last_index[n];
  344. for(i=1;i<=last_index;i++) {
  345. j = s->intra_scantable.permutated[i];
  346. val = block[j];
  347. if (val == 0) {
  348. run++;
  349. } else {
  350. while (run >= 16) {
  351. put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
  352. run -= 16;
  353. }
  354. mant = val;
  355. if (val < 0) {
  356. val = -val;
  357. mant--;
  358. }
  359. nbits= av_log2(val) + 1;
  360. code = (run << 4) | nbits;
  361. put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
  362. put_sbits(&s->pb, nbits, mant);
  363. run = 0;
  364. }
  365. }
  366. /* output EOB only if not already 64 values */
  367. if (last_index < 63 || run != 0)
  368. put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
  369. }
  370. void ff_mjpeg_encode_mb(MpegEncContext *s, DCTELEM block[6][64])
  371. {
  372. int i;
  373. for(i=0;i<5;i++) {
  374. encode_block(s, block[i], i);
  375. }
  376. if (s->chroma_format == CHROMA_420) {
  377. encode_block(s, block[5], 5);
  378. } else {
  379. encode_block(s, block[6], 6);
  380. encode_block(s, block[5], 5);
  381. encode_block(s, block[7], 7);
  382. }
  383. s->i_tex_bits += get_bits_diff(s);
  384. }
  385. // maximum over s->mjpeg_vsample[i]
  386. #define V_MAX 2
  387. static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
  388. const AVFrame *pic_arg, int *got_packet)
  389. {
  390. MpegEncContext *s = avctx->priv_data;
  391. AVFrame pic = *pic_arg;
  392. int i;
  393. //CODEC_FLAG_EMU_EDGE have to be cleared
  394. if(s->avctx->flags & CODEC_FLAG_EMU_EDGE)
  395. return -1;
  396. //picture should be flipped upside-down
  397. for(i=0; i < 3; i++) {
  398. pic.data[i] += (pic.linesize[i] * (s->mjpeg_vsample[i] * (8 * s->mb_height -((s->height/V_MAX)&7)) - 1 ));
  399. pic.linesize[i] *= -1;
  400. }
  401. return ff_MPV_encode_picture(avctx, pkt, &pic, got_packet);
  402. }
  403. AVCodec ff_mjpeg_encoder = {
  404. .name = "mjpeg",
  405. .type = AVMEDIA_TYPE_VIDEO,
  406. .id = CODEC_ID_MJPEG,
  407. .priv_data_size = sizeof(MpegEncContext),
  408. .init = ff_MPV_encode_init,
  409. .encode2 = ff_MPV_encode_picture,
  410. .close = ff_MPV_encode_end,
  411. .pix_fmts = (const enum PixelFormat[]){
  412. PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_NONE
  413. },
  414. .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
  415. };
  416. AVCodec ff_amv_encoder = {
  417. .name = "amv",
  418. .type = AVMEDIA_TYPE_VIDEO,
  419. .id = CODEC_ID_AMV,
  420. .priv_data_size = sizeof(MpegEncContext),
  421. .init = ff_MPV_encode_init,
  422. .encode2 = amv_encode_picture,
  423. .close = ff_MPV_encode_end,
  424. .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_NONE},
  425. .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
  426. };