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.

546 lines
16KB

  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. avpriv_mjpeg_bits_dc_luminance,
  55. avpriv_mjpeg_val_dc);
  56. ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
  57. m->huff_code_dc_chrominance,
  58. avpriv_mjpeg_bits_dc_chrominance,
  59. avpriv_mjpeg_val_dc);
  60. ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
  61. m->huff_code_ac_luminance,
  62. avpriv_mjpeg_bits_ac_luminance,
  63. avpriv_mjpeg_val_ac_luminance);
  64. ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
  65. m->huff_code_ac_chrominance,
  66. avpriv_mjpeg_bits_ac_chrominance,
  67. avpriv_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->width-1)/(8*s->mjpeg_hsample[0]) + 1);
  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, avpriv_mjpeg_bits_dc_luminance,
  130. avpriv_mjpeg_val_dc);
  131. size += put_huffman_table(s, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
  132. avpriv_mjpeg_val_dc);
  133. size += put_huffman_table(s, 1, 0, avpriv_mjpeg_bits_ac_luminance,
  134. avpriv_mjpeg_val_ac_luminance);
  135. size += put_huffman_table(s, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
  136. avpriv_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. avpriv_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. avpriv_put_string(p, LIBAVCODEC_IDENT, 1);
  164. size = strlen(LIBAVCODEC_IDENT)+3;
  165. AV_WB16(ptr, size);
  166. }
  167. if( s->avctx->pix_fmt == AV_PIX_FMT_YUV420P
  168. ||s->avctx->pix_fmt == AV_PIX_FMT_YUV422P
  169. ||s->avctx->pix_fmt == AV_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. avpriv_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 == AV_PIX_FMT_BGR0
  195. || s->avctx->pix_fmt == AV_PIX_FMT_BGRA
  196. || s->avctx->pix_fmt == AV_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. buf[i+ff_count]= 0;
  291. ff_count--;
  292. }
  293. buf[i+ff_count]= v;
  294. }
  295. }
  296. void ff_mjpeg_encode_stuffing(MpegEncContext *s)
  297. {
  298. int length, i;
  299. PutBitContext *pbc = &s->pb;
  300. int mb_y = s->mb_y - !s->mb_x;
  301. length= (-put_bits_count(pbc))&7;
  302. if(length) put_bits(pbc, length, (1<<length)-1);
  303. flush_put_bits(&s->pb);
  304. escape_FF(s, s->esc_pos);
  305. if((s->avctx->active_thread_type & FF_THREAD_SLICE) && mb_y < s->mb_height)
  306. put_marker(pbc, RST0 + (mb_y&7));
  307. s->esc_pos = put_bits_count(pbc) >> 3;
  308. for(i=0; i<3; i++)
  309. s->last_dc[i] = 128 << s->intra_dc_precision;
  310. }
  311. void ff_mjpeg_encode_picture_trailer(MpegEncContext *s)
  312. {
  313. av_assert1((s->header_bits&7)==0);
  314. put_marker(&s->pb, EOI);
  315. }
  316. void ff_mjpeg_encode_dc(MpegEncContext *s, int val,
  317. uint8_t *huff_size, uint16_t *huff_code)
  318. {
  319. int mant, nbits;
  320. if (val == 0) {
  321. put_bits(&s->pb, huff_size[0], huff_code[0]);
  322. } else {
  323. mant = val;
  324. if (val < 0) {
  325. val = -val;
  326. mant--;
  327. }
  328. nbits= av_log2_16bit(val) + 1;
  329. put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
  330. put_sbits(&s->pb, nbits, mant);
  331. }
  332. }
  333. static void encode_block(MpegEncContext *s, int16_t *block, int n)
  334. {
  335. int mant, nbits, code, i, j;
  336. int component, dc, run, last_index, val;
  337. MJpegContext *m = s->mjpeg_ctx;
  338. uint8_t *huff_size_ac;
  339. uint16_t *huff_code_ac;
  340. /* DC coef */
  341. component = (n <= 3 ? 0 : (n&1) + 1);
  342. dc = block[0]; /* overflow is impossible */
  343. val = dc - s->last_dc[component];
  344. if (n < 4) {
  345. ff_mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
  346. huff_size_ac = m->huff_size_ac_luminance;
  347. huff_code_ac = m->huff_code_ac_luminance;
  348. } else {
  349. ff_mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  350. huff_size_ac = m->huff_size_ac_chrominance;
  351. huff_code_ac = m->huff_code_ac_chrominance;
  352. }
  353. s->last_dc[component] = dc;
  354. /* AC coefs */
  355. run = 0;
  356. last_index = s->block_last_index[n];
  357. for(i=1;i<=last_index;i++) {
  358. j = s->intra_scantable.permutated[i];
  359. val = block[j];
  360. if (val == 0) {
  361. run++;
  362. } else {
  363. while (run >= 16) {
  364. put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
  365. run -= 16;
  366. }
  367. mant = val;
  368. if (val < 0) {
  369. val = -val;
  370. mant--;
  371. }
  372. nbits= av_log2(val) + 1;
  373. code = (run << 4) | nbits;
  374. put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
  375. put_sbits(&s->pb, nbits, mant);
  376. run = 0;
  377. }
  378. }
  379. /* output EOB only if not already 64 values */
  380. if (last_index < 63 || run != 0)
  381. put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
  382. }
  383. void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[6][64])
  384. {
  385. int i;
  386. if (s->chroma_format == CHROMA_444) {
  387. encode_block(s, block[0], 0);
  388. encode_block(s, block[2], 2);
  389. encode_block(s, block[4], 4);
  390. encode_block(s, block[8], 8);
  391. encode_block(s, block[5], 5);
  392. encode_block(s, block[9], 9);
  393. if (16*s->mb_x+8 < s->width) {
  394. encode_block(s, block[1], 1);
  395. encode_block(s, block[3], 3);
  396. encode_block(s, block[6], 6);
  397. encode_block(s, block[10], 10);
  398. encode_block(s, block[7], 7);
  399. encode_block(s, block[11], 11);
  400. }
  401. } else {
  402. for(i=0;i<5;i++) {
  403. encode_block(s, block[i], i);
  404. }
  405. if (s->chroma_format == CHROMA_420) {
  406. encode_block(s, block[5], 5);
  407. } else {
  408. encode_block(s, block[6], 6);
  409. encode_block(s, block[5], 5);
  410. encode_block(s, block[7], 7);
  411. }
  412. }
  413. s->i_tex_bits += get_bits_diff(s);
  414. }
  415. // maximum over s->mjpeg_vsample[i]
  416. #define V_MAX 2
  417. static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
  418. const AVFrame *pic_arg, int *got_packet)
  419. {
  420. MpegEncContext *s = avctx->priv_data;
  421. AVFrame pic = *pic_arg;
  422. int i;
  423. //CODEC_FLAG_EMU_EDGE have to be cleared
  424. if(s->avctx->flags & CODEC_FLAG_EMU_EDGE)
  425. return -1;
  426. //picture should be flipped upside-down
  427. for(i=0; i < 3; i++) {
  428. pic.data[i] += (pic.linesize[i] * (s->mjpeg_vsample[i] * (8 * s->mb_height -((s->height/V_MAX)&7)) - 1 ));
  429. pic.linesize[i] *= -1;
  430. }
  431. return ff_MPV_encode_picture(avctx, pkt, &pic, got_packet);
  432. }
  433. #if CONFIG_MJPEG_ENCODER
  434. AVCodec ff_mjpeg_encoder = {
  435. .name = "mjpeg",
  436. .type = AVMEDIA_TYPE_VIDEO,
  437. .id = AV_CODEC_ID_MJPEG,
  438. .priv_data_size = sizeof(MpegEncContext),
  439. .init = ff_MPV_encode_init,
  440. .encode2 = ff_MPV_encode_picture,
  441. .close = ff_MPV_encode_end,
  442. .capabilities = CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS | CODEC_CAP_INTRA_ONLY,
  443. .pix_fmts = (const enum AVPixelFormat[]){
  444. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE
  445. },
  446. .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
  447. };
  448. #endif
  449. #if CONFIG_AMV_ENCODER
  450. AVCodec ff_amv_encoder = {
  451. .name = "amv",
  452. .type = AVMEDIA_TYPE_VIDEO,
  453. .id = AV_CODEC_ID_AMV,
  454. .priv_data_size = sizeof(MpegEncContext),
  455. .init = ff_MPV_encode_init,
  456. .encode2 = amv_encode_picture,
  457. .close = ff_MPV_encode_end,
  458. .pix_fmts = (const enum AVPixelFormat[]){
  459. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_NONE
  460. },
  461. .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
  462. };
  463. #endif