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.

302 lines
9.7KB

  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 uint8_t uni_ac_vlc_len[64 * 64 * 2];
  39. static uint8_t uni_chroma_ac_vlc_len[64 * 64 * 2];
  40. static av_cold void init_uni_ac_vlc(const uint8_t huff_size_ac[256], uint8_t *uni_ac_vlc_len)
  41. {
  42. int i;
  43. for (i = 0; i < 128; i++) {
  44. int level = i - 64;
  45. int run;
  46. if (!level)
  47. continue;
  48. for (run = 0; run < 64; run++) {
  49. int len, code, nbits;
  50. int alevel = FFABS(level);
  51. len = (run >> 4) * huff_size_ac[0xf0];
  52. nbits= av_log2_16bit(alevel) + 1;
  53. code = ((15&run) << 4) | nbits;
  54. len += huff_size_ac[code] + nbits;
  55. uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len;
  56. // We ignore EOB as its just a constant which does not change generally
  57. }
  58. }
  59. }
  60. av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
  61. {
  62. MJpegContext *m;
  63. if (s->width > 65500 || s->height > 65500) {
  64. av_log(s, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
  65. return AVERROR(EINVAL);
  66. }
  67. m = av_malloc(sizeof(MJpegContext));
  68. if (!m)
  69. return AVERROR(ENOMEM);
  70. s->min_qcoeff=-1023;
  71. s->max_qcoeff= 1023;
  72. /* build all the huffman tables */
  73. ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
  74. m->huff_code_dc_luminance,
  75. avpriv_mjpeg_bits_dc_luminance,
  76. avpriv_mjpeg_val_dc);
  77. ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
  78. m->huff_code_dc_chrominance,
  79. avpriv_mjpeg_bits_dc_chrominance,
  80. avpriv_mjpeg_val_dc);
  81. ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
  82. m->huff_code_ac_luminance,
  83. avpriv_mjpeg_bits_ac_luminance,
  84. avpriv_mjpeg_val_ac_luminance);
  85. ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
  86. m->huff_code_ac_chrominance,
  87. avpriv_mjpeg_bits_ac_chrominance,
  88. avpriv_mjpeg_val_ac_chrominance);
  89. init_uni_ac_vlc(m->huff_size_ac_luminance, uni_ac_vlc_len);
  90. init_uni_ac_vlc(m->huff_size_ac_chrominance, uni_chroma_ac_vlc_len);
  91. s->intra_ac_vlc_length =
  92. s->intra_ac_vlc_last_length = uni_ac_vlc_len;
  93. s->intra_chroma_ac_vlc_length =
  94. s->intra_chroma_ac_vlc_last_length = uni_chroma_ac_vlc_len;
  95. s->mjpeg_ctx = m;
  96. return 0;
  97. }
  98. av_cold void ff_mjpeg_encode_close(MpegEncContext *s)
  99. {
  100. av_freep(&s->mjpeg_ctx);
  101. }
  102. static void encode_block(MpegEncContext *s, int16_t *block, int n)
  103. {
  104. int mant, nbits, code, i, j;
  105. int component, dc, run, last_index, val;
  106. MJpegContext *m = s->mjpeg_ctx;
  107. uint8_t *huff_size_ac;
  108. uint16_t *huff_code_ac;
  109. /* DC coef */
  110. component = (n <= 3 ? 0 : (n&1) + 1);
  111. dc = block[0]; /* overflow is impossible */
  112. val = dc - s->last_dc[component];
  113. if (n < 4) {
  114. ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
  115. huff_size_ac = m->huff_size_ac_luminance;
  116. huff_code_ac = m->huff_code_ac_luminance;
  117. } else {
  118. ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  119. huff_size_ac = m->huff_size_ac_chrominance;
  120. huff_code_ac = m->huff_code_ac_chrominance;
  121. }
  122. s->last_dc[component] = dc;
  123. /* AC coefs */
  124. run = 0;
  125. last_index = s->block_last_index[n];
  126. for(i=1;i<=last_index;i++) {
  127. j = s->intra_scantable.permutated[i];
  128. val = block[j];
  129. if (val == 0) {
  130. run++;
  131. } else {
  132. while (run >= 16) {
  133. put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
  134. run -= 16;
  135. }
  136. mant = val;
  137. if (val < 0) {
  138. val = -val;
  139. mant--;
  140. }
  141. nbits= av_log2_16bit(val) + 1;
  142. code = (run << 4) | nbits;
  143. put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
  144. put_sbits(&s->pb, nbits, mant);
  145. run = 0;
  146. }
  147. }
  148. /* output EOB only if not already 64 values */
  149. if (last_index < 63 || run != 0)
  150. put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
  151. }
  152. void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64])
  153. {
  154. int i;
  155. if (s->chroma_format == CHROMA_444) {
  156. encode_block(s, block[0], 0);
  157. encode_block(s, block[2], 2);
  158. encode_block(s, block[4], 4);
  159. encode_block(s, block[8], 8);
  160. encode_block(s, block[5], 5);
  161. encode_block(s, block[9], 9);
  162. if (16*s->mb_x+8 < s->width) {
  163. encode_block(s, block[1], 1);
  164. encode_block(s, block[3], 3);
  165. encode_block(s, block[6], 6);
  166. encode_block(s, block[10], 10);
  167. encode_block(s, block[7], 7);
  168. encode_block(s, block[11], 11);
  169. }
  170. } else {
  171. for(i=0;i<5;i++) {
  172. encode_block(s, block[i], i);
  173. }
  174. if (s->chroma_format == CHROMA_420) {
  175. encode_block(s, block[5], 5);
  176. } else {
  177. encode_block(s, block[6], 6);
  178. encode_block(s, block[5], 5);
  179. encode_block(s, block[7], 7);
  180. }
  181. }
  182. s->i_tex_bits += get_bits_diff(s);
  183. }
  184. // maximum over s->mjpeg_vsample[i]
  185. #define V_MAX 2
  186. static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
  187. const AVFrame *pic_arg, int *got_packet)
  188. {
  189. MpegEncContext *s = avctx->priv_data;
  190. AVFrame *pic;
  191. int i, ret;
  192. int chroma_h_shift, chroma_v_shift;
  193. av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
  194. //CODEC_FLAG_EMU_EDGE have to be cleared
  195. if(s->avctx->flags & CODEC_FLAG_EMU_EDGE)
  196. return AVERROR(EINVAL);
  197. if ((avctx->height & 15) && avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
  198. av_log(avctx, AV_LOG_ERROR,
  199. "Heights which are not a multiple of 16 might fail with some decoders, "
  200. "use vstrict=-1 / -strict -1 to use %d anyway.\n", avctx->height);
  201. av_log(avctx, AV_LOG_WARNING, "If you have a device that plays AMV videos, please test if videos "
  202. "with such heights work with it and report your findings to ffmpeg-devel@ffmpeg.org\n");
  203. return AVERROR_EXPERIMENTAL;
  204. }
  205. pic = av_frame_clone(pic_arg);
  206. if (!pic)
  207. return AVERROR(ENOMEM);
  208. //picture should be flipped upside-down
  209. for(i=0; i < 3; i++) {
  210. int vsample = i ? 2 >> chroma_v_shift : 2;
  211. pic->data[i] += pic->linesize[i] * (vsample * s->height / V_MAX - 1);
  212. pic->linesize[i] *= -1;
  213. }
  214. ret = ff_mpv_encode_picture(avctx, pkt, pic, got_packet);
  215. av_frame_free(&pic);
  216. return ret;
  217. }
  218. #if CONFIG_MJPEG_ENCODER
  219. static const AVClass mjpeg_class = {
  220. .class_name = "mjpeg encoder",
  221. .item_name = av_default_item_name,
  222. .option = ff_mpv_generic_options,
  223. .version = LIBAVUTIL_VERSION_INT,
  224. };
  225. AVCodec ff_mjpeg_encoder = {
  226. .name = "mjpeg",
  227. .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
  228. .type = AVMEDIA_TYPE_VIDEO,
  229. .id = AV_CODEC_ID_MJPEG,
  230. .priv_data_size = sizeof(MpegEncContext),
  231. .init = ff_mpv_encode_init,
  232. .encode2 = ff_mpv_encode_picture,
  233. .close = ff_mpv_encode_end,
  234. .capabilities = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_INTRA_ONLY,
  235. .pix_fmts = (const enum AVPixelFormat[]){
  236. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE
  237. },
  238. .priv_class = &mjpeg_class,
  239. };
  240. #endif
  241. #if CONFIG_AMV_ENCODER
  242. static const AVClass amv_class = {
  243. .class_name = "amv encoder",
  244. .item_name = av_default_item_name,
  245. .option = ff_mpv_generic_options,
  246. .version = LIBAVUTIL_VERSION_INT,
  247. };
  248. AVCodec ff_amv_encoder = {
  249. .name = "amv",
  250. .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
  251. .type = AVMEDIA_TYPE_VIDEO,
  252. .id = AV_CODEC_ID_AMV,
  253. .priv_data_size = sizeof(MpegEncContext),
  254. .init = ff_mpv_encode_init,
  255. .encode2 = amv_encode_picture,
  256. .close = ff_mpv_encode_end,
  257. .pix_fmts = (const enum AVPixelFormat[]){
  258. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_NONE
  259. },
  260. .priv_class = &amv_class,
  261. };
  262. #endif