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.

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