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.

411 lines
13KB

  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 "libavutil/pixdesc.h"
  32. #include "avcodec.h"
  33. #include "jpegtables.h"
  34. #include "mjpegenc_common.h"
  35. #include "mpegvideo.h"
  36. #include "mjpeg.h"
  37. #include "mjpegenc.h"
  38. static int alloc_huffman(MpegEncContext *s)
  39. {
  40. MJpegContext *m = s->mjpeg_ctx;
  41. size_t num_mbs, num_blocks, num_codes;
  42. int blocks_per_mb;
  43. // We need to init this here as the mjpeg init is called before the common init,
  44. s->mb_width = (s->width + 15) / 16;
  45. s->mb_height = (s->height + 15) / 16;
  46. switch (s->chroma_format) {
  47. case CHROMA_420: blocks_per_mb = 6; break;
  48. case CHROMA_422: blocks_per_mb = 8; break;
  49. case CHROMA_444: blocks_per_mb = 12; break;
  50. default: av_assert0(0);
  51. };
  52. // Make sure we have enough space to hold this frame.
  53. num_mbs = s->mb_width * s->mb_height;
  54. num_blocks = num_mbs * blocks_per_mb;
  55. num_codes = num_blocks * 64;
  56. m->huff_buffer = av_malloc_array(num_codes, sizeof(MJpegHuffmanCode));
  57. if (!m->huff_buffer)
  58. return AVERROR(ENOMEM);
  59. return 0;
  60. }
  61. av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
  62. {
  63. MJpegContext *m;
  64. av_assert0(s->slice_context_count == 1);
  65. if (s->width > 65500 || s->height > 65500) {
  66. av_log(s, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
  67. return AVERROR(EINVAL);
  68. }
  69. m = av_malloc(sizeof(MJpegContext));
  70. if (!m)
  71. return AVERROR(ENOMEM);
  72. s->min_qcoeff=-1023;
  73. s->max_qcoeff= 1023;
  74. // Build default Huffman tables.
  75. // These may be overwritten later with more optimal Huffman tables, but
  76. // they are needed at least right now for some processes like trellis.
  77. ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
  78. m->huff_code_dc_luminance,
  79. avpriv_mjpeg_bits_dc_luminance,
  80. avpriv_mjpeg_val_dc);
  81. ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
  82. m->huff_code_dc_chrominance,
  83. avpriv_mjpeg_bits_dc_chrominance,
  84. avpriv_mjpeg_val_dc);
  85. ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
  86. m->huff_code_ac_luminance,
  87. avpriv_mjpeg_bits_ac_luminance,
  88. avpriv_mjpeg_val_ac_luminance);
  89. ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
  90. m->huff_code_ac_chrominance,
  91. avpriv_mjpeg_bits_ac_chrominance,
  92. avpriv_mjpeg_val_ac_chrominance);
  93. ff_init_uni_ac_vlc(m->huff_size_ac_luminance, m->uni_ac_vlc_len);
  94. ff_init_uni_ac_vlc(m->huff_size_ac_chrominance, m->uni_chroma_ac_vlc_len);
  95. s->intra_ac_vlc_length =
  96. s->intra_ac_vlc_last_length = m->uni_ac_vlc_len;
  97. s->intra_chroma_ac_vlc_length =
  98. s->intra_chroma_ac_vlc_last_length = m->uni_chroma_ac_vlc_len;
  99. // Buffers start out empty.
  100. m->huff_ncode = 0;
  101. s->mjpeg_ctx = m;
  102. return alloc_huffman(s);
  103. }
  104. av_cold void ff_mjpeg_encode_close(MpegEncContext *s)
  105. {
  106. av_freep(&s->mjpeg_ctx->huff_buffer);
  107. av_freep(&s->mjpeg_ctx);
  108. }
  109. /**
  110. * Encodes and outputs the entire frame in the JPEG format.
  111. *
  112. * @param s The MpegEncContext.
  113. */
  114. void ff_mjpeg_encode_picture_frame(MpegEncContext *s)
  115. {
  116. int i, nbits, code, table_id;
  117. MJpegContext *m = s->mjpeg_ctx;
  118. uint8_t *huff_size[4] = {m->huff_size_dc_luminance,
  119. m->huff_size_dc_chrominance,
  120. m->huff_size_ac_luminance,
  121. m->huff_size_ac_chrominance};
  122. uint16_t *huff_code[4] = {m->huff_code_dc_luminance,
  123. m->huff_code_dc_chrominance,
  124. m->huff_code_ac_luminance,
  125. m->huff_code_ac_chrominance};
  126. size_t total_bits = 0;
  127. size_t bytes_needed;
  128. s->header_bits = get_bits_diff(s);
  129. // Estimate the total size first
  130. for (i = 0; i < m->huff_ncode; i++) {
  131. table_id = m->huff_buffer[i].table_id;
  132. code = m->huff_buffer[i].code;
  133. nbits = code & 0xf;
  134. total_bits += huff_size[table_id][code] + nbits;
  135. }
  136. bytes_needed = (total_bits + 7) / 8;
  137. ff_mpv_reallocate_putbitbuffer(s, bytes_needed, bytes_needed);
  138. for (i = 0; i < m->huff_ncode; i++) {
  139. table_id = m->huff_buffer[i].table_id;
  140. code = m->huff_buffer[i].code;
  141. nbits = code & 0xf;
  142. put_bits(&s->pb, huff_size[table_id][code], huff_code[table_id][code]);
  143. if (nbits != 0) {
  144. put_sbits(&s->pb, nbits, m->huff_buffer[i].mant);
  145. }
  146. }
  147. m->huff_ncode = 0;
  148. s->i_tex_bits = get_bits_diff(s);
  149. }
  150. /**
  151. * Add code and table_id to the JPEG buffer.
  152. *
  153. * @param s The MJpegContext which contains the JPEG buffer.
  154. * @param table_id Which Huffman table the code belongs to.
  155. * @param code The encoded exponent of the coefficients and the run-bits.
  156. */
  157. static inline void ff_mjpeg_encode_code(MJpegContext *s, uint8_t table_id, int code)
  158. {
  159. MJpegHuffmanCode *c = &s->huff_buffer[s->huff_ncode++];
  160. c->table_id = table_id;
  161. c->code = code;
  162. }
  163. /**
  164. * Add the coefficient's data to the JPEG buffer.
  165. *
  166. * @param s The MJpegContext which contains the JPEG buffer.
  167. * @param table_id Which Huffman table the code belongs to.
  168. * @param val The coefficient.
  169. * @param run The run-bits.
  170. */
  171. static void ff_mjpeg_encode_coef(MJpegContext *s, uint8_t table_id, int val, int run)
  172. {
  173. int mant, code;
  174. if (val == 0) {
  175. av_assert0(run == 0);
  176. ff_mjpeg_encode_code(s, table_id, 0);
  177. } else {
  178. mant = val;
  179. if (val < 0) {
  180. val = -val;
  181. mant--;
  182. }
  183. code = (run << 4) | (av_log2_16bit(val) + 1);
  184. s->huff_buffer[s->huff_ncode].mant = mant;
  185. ff_mjpeg_encode_code(s, table_id, code);
  186. }
  187. }
  188. /**
  189. * Add the block's data into the JPEG buffer.
  190. *
  191. * @param s The MJpegEncContext that contains the JPEG buffer.
  192. * @param block The block.
  193. * @param n The block's index or number.
  194. */
  195. static void encode_block(MpegEncContext *s, int16_t *block, int n)
  196. {
  197. int i, j, table_id;
  198. int component, dc, last_index, val, run;
  199. MJpegContext *m = s->mjpeg_ctx;
  200. /* DC coef */
  201. component = (n <= 3 ? 0 : (n&1) + 1);
  202. table_id = (n <= 3 ? 0 : 1);
  203. dc = block[0]; /* overflow is impossible */
  204. val = dc - s->last_dc[component];
  205. ff_mjpeg_encode_coef(m, table_id, val, 0);
  206. s->last_dc[component] = dc;
  207. /* AC coefs */
  208. run = 0;
  209. last_index = s->block_last_index[n];
  210. table_id |= 2;
  211. for(i=1;i<=last_index;i++) {
  212. j = s->intra_scantable.permutated[i];
  213. val = block[j];
  214. if (val == 0) {
  215. run++;
  216. } else {
  217. while (run >= 16) {
  218. ff_mjpeg_encode_code(m, table_id, 0xf0);
  219. run -= 16;
  220. }
  221. ff_mjpeg_encode_coef(m, table_id, val, run);
  222. run = 0;
  223. }
  224. }
  225. /* output EOB only if not already 64 values */
  226. if (last_index < 63 || run != 0)
  227. ff_mjpeg_encode_code(m, table_id, 0);
  228. }
  229. int ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64])
  230. {
  231. int i, is_chroma_420;
  232. if (s->chroma_format == CHROMA_444) {
  233. encode_block(s, block[0], 0);
  234. encode_block(s, block[2], 2);
  235. encode_block(s, block[4], 4);
  236. encode_block(s, block[8], 8);
  237. encode_block(s, block[5], 5);
  238. encode_block(s, block[9], 9);
  239. if (16*s->mb_x+8 < s->width) {
  240. encode_block(s, block[1], 1);
  241. encode_block(s, block[3], 3);
  242. encode_block(s, block[6], 6);
  243. encode_block(s, block[10], 10);
  244. encode_block(s, block[7], 7);
  245. encode_block(s, block[11], 11);
  246. }
  247. } else {
  248. is_chroma_420 = (s->chroma_format == CHROMA_420);
  249. for(i=0;i<5;i++) {
  250. encode_block(s, block[i], i);
  251. }
  252. if (is_chroma_420) {
  253. encode_block(s, block[5], 5);
  254. } else {
  255. encode_block(s, block[6], 6);
  256. encode_block(s, block[5], 5);
  257. encode_block(s, block[7], 7);
  258. }
  259. }
  260. return 0;
  261. }
  262. // maximum over s->mjpeg_vsample[i]
  263. #define V_MAX 2
  264. static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
  265. const AVFrame *pic_arg, int *got_packet)
  266. {
  267. MpegEncContext *s = avctx->priv_data;
  268. AVFrame *pic;
  269. int i, ret;
  270. int chroma_h_shift, chroma_v_shift;
  271. av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
  272. #if FF_API_EMU_EDGE
  273. //CODEC_FLAG_EMU_EDGE have to be cleared
  274. if(s->avctx->flags & CODEC_FLAG_EMU_EDGE)
  275. return AVERROR(EINVAL);
  276. #endif
  277. if ((avctx->height & 15) && avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
  278. av_log(avctx, AV_LOG_ERROR,
  279. "Heights which are not a multiple of 16 might fail with some decoders, "
  280. "use vstrict=-1 / -strict -1 to use %d anyway.\n", avctx->height);
  281. av_log(avctx, AV_LOG_WARNING, "If you have a device that plays AMV videos, please test if videos "
  282. "with such heights work with it and report your findings to ffmpeg-devel@ffmpeg.org\n");
  283. return AVERROR_EXPERIMENTAL;
  284. }
  285. pic = av_frame_clone(pic_arg);
  286. if (!pic)
  287. return AVERROR(ENOMEM);
  288. //picture should be flipped upside-down
  289. for(i=0; i < 3; i++) {
  290. int vsample = i ? 2 >> chroma_v_shift : 2;
  291. pic->data[i] += pic->linesize[i] * (vsample * s->height / V_MAX - 1);
  292. pic->linesize[i] *= -1;
  293. }
  294. ret = ff_mpv_encode_picture(avctx, pkt, pic, got_packet);
  295. av_frame_free(&pic);
  296. return ret;
  297. }
  298. #define OFFSET(x) offsetof(MpegEncContext, x)
  299. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  300. static const AVOption options[] = {
  301. FF_MPV_COMMON_OPTS
  302. { "pred", "Prediction method", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 3, VE, "pred" },
  303. { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
  304. { "plane", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, VE, "pred" },
  305. { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 3 }, INT_MIN, INT_MAX, VE, "pred" },
  306. { "huffman", "Huffman table strategy", OFFSET(huffman), AV_OPT_TYPE_INT, { .i64 = HUFFMAN_TABLE_DEFAULT }, 0, NB_HUFFMAN_TABLE_OPTION - 1, VE, "huffman" },
  307. { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = HUFFMAN_TABLE_DEFAULT }, INT_MIN, INT_MAX, VE, "huffman" },
  308. { "optimal", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = HUFFMAN_TABLE_OPTIMAL }, INT_MIN, INT_MAX, VE, "huffman" },
  309. { NULL},
  310. };
  311. #if CONFIG_MJPEG_ENCODER
  312. static const AVClass mjpeg_class = {
  313. .class_name = "mjpeg encoder",
  314. .item_name = av_default_item_name,
  315. .option = options,
  316. .version = LIBAVUTIL_VERSION_INT,
  317. };
  318. AVCodec ff_mjpeg_encoder = {
  319. .name = "mjpeg",
  320. .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
  321. .type = AVMEDIA_TYPE_VIDEO,
  322. .id = AV_CODEC_ID_MJPEG,
  323. .priv_data_size = sizeof(MpegEncContext),
  324. .init = ff_mpv_encode_init,
  325. .encode2 = ff_mpv_encode_picture,
  326. .close = ff_mpv_encode_end,
  327. .capabilities = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_INTRA_ONLY,
  328. .pix_fmts = (const enum AVPixelFormat[]){
  329. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE
  330. },
  331. .priv_class = &mjpeg_class,
  332. };
  333. #endif
  334. #if CONFIG_AMV_ENCODER
  335. static const AVClass amv_class = {
  336. .class_name = "amv encoder",
  337. .item_name = av_default_item_name,
  338. .option = options,
  339. .version = LIBAVUTIL_VERSION_INT,
  340. };
  341. AVCodec ff_amv_encoder = {
  342. .name = "amv",
  343. .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
  344. .type = AVMEDIA_TYPE_VIDEO,
  345. .id = AV_CODEC_ID_AMV,
  346. .priv_data_size = sizeof(MpegEncContext),
  347. .init = ff_mpv_encode_init,
  348. .encode2 = amv_encode_picture,
  349. .close = ff_mpv_encode_end,
  350. .pix_fmts = (const enum AVPixelFormat[]){
  351. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_NONE
  352. },
  353. .priv_class = &amv_class,
  354. };
  355. #endif