FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mjpegenc.c
Go to the documentation of this file.
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 /**
29  * @file
30  * MJPEG encoder.
31  */
32 
33 #include "libavutil/pixdesc.h"
34 
35 #include "avcodec.h"
36 #include "jpegtables.h"
37 #include "mjpegenc_common.h"
38 #include "mpegvideo.h"
39 #include "mjpeg.h"
40 #include "mjpegenc.h"
41 
42 static uint8_t uni_ac_vlc_len[64 * 64 * 2];
43 static uint8_t uni_chroma_ac_vlc_len[64 * 64 * 2];
44 
45 static av_cold void init_uni_ac_vlc(const uint8_t huff_size_ac[256], uint8_t *uni_ac_vlc_len)
46 {
47  int i;
48 
49  for (i = 0; i < 128; i++) {
50  int level = i - 64;
51  int run;
52  if (!level)
53  continue;
54  for (run = 0; run < 64; run++) {
55  int len, code, nbits;
56  int alevel = FFABS(level);
57 
58  len = (run >> 4) * huff_size_ac[0xf0];
59 
60  nbits= av_log2_16bit(alevel) + 1;
61  code = ((15&run) << 4) | nbits;
62 
63  len += huff_size_ac[code] + nbits;
64 
65  uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len;
66  // We ignore EOB as its just a constant which does not change generally
67  }
68  }
69 }
70 
72 {
73  MJpegContext *m;
74 
75  if (s->width > 65500 || s->height > 65500) {
76  av_log(s, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
77  return AVERROR(EINVAL);
78  }
79 
80  m = av_malloc(sizeof(MJpegContext));
81  if (!m)
82  return AVERROR(ENOMEM);
83 
84  s->min_qcoeff=-1023;
85  s->max_qcoeff= 1023;
86 
87  /* build all the huffman tables */
104 
111 
112  s->mjpeg_ctx = m;
113  return 0;
114 }
115 
117 {
118  av_freep(&s->mjpeg_ctx);
119 }
120 
121 static void encode_block(MpegEncContext *s, int16_t *block, int n)
122 {
123  int mant, nbits, code, i, j;
124  int component, dc, run, last_index, val;
125  MJpegContext *m = s->mjpeg_ctx;
126  uint8_t *huff_size_ac;
127  uint16_t *huff_code_ac;
128 
129  /* DC coef */
130  component = (n <= 3 ? 0 : (n&1) + 1);
131  dc = block[0]; /* overflow is impossible */
132  val = dc - s->last_dc[component];
133  if (n < 4) {
135  huff_size_ac = m->huff_size_ac_luminance;
136  huff_code_ac = m->huff_code_ac_luminance;
137  } else {
139  huff_size_ac = m->huff_size_ac_chrominance;
140  huff_code_ac = m->huff_code_ac_chrominance;
141  }
142  s->last_dc[component] = dc;
143 
144  /* AC coefs */
145 
146  run = 0;
147  last_index = s->block_last_index[n];
148  for(i=1;i<=last_index;i++) {
149  j = s->intra_scantable.permutated[i];
150  val = block[j];
151  if (val == 0) {
152  run++;
153  } else {
154  while (run >= 16) {
155  put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
156  run -= 16;
157  }
158  mant = val;
159  if (val < 0) {
160  val = -val;
161  mant--;
162  }
163 
164  nbits= av_log2_16bit(val) + 1;
165  code = (run << 4) | nbits;
166 
167  put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
168 
169  put_sbits(&s->pb, nbits, mant);
170  run = 0;
171  }
172  }
173 
174  /* output EOB only if not already 64 values */
175  if (last_index < 63 || run != 0)
176  put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
177 }
178 
179 void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64])
180 {
181  int i;
182  if (s->chroma_format == CHROMA_444) {
183  encode_block(s, block[0], 0);
184  encode_block(s, block[2], 2);
185  encode_block(s, block[4], 4);
186  encode_block(s, block[8], 8);
187  encode_block(s, block[5], 5);
188  encode_block(s, block[9], 9);
189 
190  if (16*s->mb_x+8 < s->width) {
191  encode_block(s, block[1], 1);
192  encode_block(s, block[3], 3);
193  encode_block(s, block[6], 6);
194  encode_block(s, block[10], 10);
195  encode_block(s, block[7], 7);
196  encode_block(s, block[11], 11);
197  }
198  } else {
199  for(i=0;i<5;i++) {
200  encode_block(s, block[i], i);
201  }
202  if (s->chroma_format == CHROMA_420) {
203  encode_block(s, block[5], 5);
204  } else {
205  encode_block(s, block[6], 6);
206  encode_block(s, block[5], 5);
207  encode_block(s, block[7], 7);
208  }
209  }
210 
211  s->i_tex_bits += get_bits_diff(s);
212 }
213 
214 // maximum over s->mjpeg_vsample[i]
215 #define V_MAX 2
217  const AVFrame *pic_arg, int *got_packet)
218 
219 {
220  MpegEncContext *s = avctx->priv_data;
221  AVFrame *pic;
222  int i, ret;
223  int chroma_h_shift, chroma_v_shift;
224 
225  av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
226 
227 #if FF_API_EMU_EDGE
228  //CODEC_FLAG_EMU_EDGE have to be cleared
230  return AVERROR(EINVAL);
231 #endif
232 
233  if ((avctx->height & 15) && avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
234  av_log(avctx, AV_LOG_ERROR,
235  "Heights which are not a multiple of 16 might fail with some decoders, "
236  "use vstrict=-1 / -strict -1 to use %d anyway.\n", avctx->height);
237  av_log(avctx, AV_LOG_WARNING, "If you have a device that plays AMV videos, please test if videos "
238  "with such heights work with it and report your findings to ffmpeg-devel@ffmpeg.org\n");
239  return AVERROR_EXPERIMENTAL;
240  }
241 
242  pic = av_frame_clone(pic_arg);
243  if (!pic)
244  return AVERROR(ENOMEM);
245  //picture should be flipped upside-down
246  for(i=0; i < 3; i++) {
247  int vsample = i ? 2 >> chroma_v_shift : 2;
248  pic->data[i] += pic->linesize[i] * (vsample * s->height / V_MAX - 1);
249  pic->linesize[i] *= -1;
250  }
251  ret = ff_mpv_encode_picture(avctx, pkt, pic, got_packet);
252  av_frame_free(&pic);
253  return ret;
254 }
255 
256 #if CONFIG_MJPEG_ENCODER
257 
258 static const AVClass mjpeg_class = {
259  .class_name = "mjpeg encoder",
260  .item_name = av_default_item_name,
261  .option = ff_mpv_generic_options,
262  .version = LIBAVUTIL_VERSION_INT,
263 };
264 
265 AVCodec ff_mjpeg_encoder = {
266  .name = "mjpeg",
267  .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
268  .type = AVMEDIA_TYPE_VIDEO,
269  .id = AV_CODEC_ID_MJPEG,
270  .priv_data_size = sizeof(MpegEncContext),
272  .encode2 = ff_mpv_encode_picture,
273  .close = ff_mpv_encode_end,
275  .pix_fmts = (const enum AVPixelFormat[]){
277  },
278  .priv_class = &mjpeg_class,
279 };
280 #endif
281 #if CONFIG_AMV_ENCODER
282 static const AVClass amv_class = {
283  .class_name = "amv encoder",
284  .item_name = av_default_item_name,
285  .option = ff_mpv_generic_options,
286  .version = LIBAVUTIL_VERSION_INT,
287 };
288 
289 AVCodec ff_amv_encoder = {
290  .name = "amv",
291  .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
292  .type = AVMEDIA_TYPE_VIDEO,
293  .id = AV_CODEC_ID_AMV,
294  .priv_data_size = sizeof(MpegEncContext),
296  .encode2 = amv_encode_picture,
297  .close = ff_mpv_encode_end,
298  .pix_fmts = (const enum AVPixelFormat[]){
300  },
301  .priv_class = &amv_class,
302 };
303 #endif
const char const char void * val
Definition: avisynth_c.h:634
const char * s
Definition: avisynth_c.h:631
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:192
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:167
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
MJPEG encoder.
static uint8_t uni_chroma_ac_vlc_len[64 *64 *2]
Definition: mjpegenc.c:43
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1722
int min_qcoeff
minimum encodable coefficient
Definition: mpegvideo.h:315
mpegvideo header.
av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
Definition: mjpegenc.c:71
uint8_t permutated[64]
Definition: idctdsp.h:31
uint8_t run
Definition: svq3.c:149
static AVPacket pkt
uint8_t * intra_ac_vlc_length
Definition: mpegvideo.h:318
#define UNI_AC_ENC_INDEX(run, level)
Definition: mpegvideo.h:325
int av_log2_16bit(unsigned v)
Definition: intmath.c:31
AVCodec.
Definition: avcodec.h:3472
MJPEG encoder and decoder.
uint16_t huff_code_dc_chrominance[12]
Definition: mjpegenc.h:46
#define AV_CODEC_CAP_INTRA_ONLY
Codec is intra only.
Definition: avcodec.h:940
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
const AVOption ff_mpv_generic_options[]
Definition: mpegvideo_enc.c:82
#define V_MAX
Definition: mjpegenc.c:215
void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code, const uint8_t *bits_table, const uint8_t *val_table)
Definition: jpegtables.c:127
uint16_t huff_code_ac_luminance[256]
Definition: mjpegenc.h:49
uint8_t
#define av_cold
Definition: attributes.h:74
#define av_malloc(s)
static uint8_t uni_ac_vlc_len[64 *64 *2]
Definition: mjpegenc.c:42
static void encode_block(MpegEncContext *s, int16_t *block, int n)
Definition: mjpegenc.c:121
static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pic_arg, int *got_packet)
Definition: mjpegenc.c:216
const uint8_t avpriv_mjpeg_bits_dc_luminance[17]
Definition: jpegtables.c:65
#define CHROMA_420
Definition: mpegvideo.h:476
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:76
uint8_t huff_size_dc_chrominance[12]
Definition: mjpegenc.h:45
int max_qcoeff
maximum encodable coefficient
Definition: mpegvideo.h:316
#define CHROMA_444
Definition: mpegvideo.h:478
#define av_log(a,...)
unsigned m
Definition: audioconvert.c:187
static int get_bits_diff(MpegEncContext *s)
Definition: mpegvideo.h:695
uint16_t huff_code_ac_chrominance[256]
Definition: mjpegenc.h:51
uint8_t huff_size_ac_luminance[256]
Definition: mjpegenc.h:48
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int last_dc[3]
last DC values for MPEG1
Definition: mpegvideo.h:192
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
const uint8_t avpriv_mjpeg_bits_dc_chrominance[17]
Definition: jpegtables.c:70
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:148
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:2157
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
uint8_t * intra_chroma_ac_vlc_length
Definition: mpegvideo.h:320
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1597
uint8_t huff_size_ac_chrominance[256]
Definition: mjpegenc.h:50
const char * name
Name of the codec implementation.
Definition: avcodec.h:3479
Libavcodec external API header.
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:920
uint8_t * intra_ac_vlc_last_length
Definition: mpegvideo.h:319
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:75
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:68
int block_last_index[12]
last non zero coefficient in block
Definition: mpegvideo.h:93
int n
Definition: avisynth_c.h:547
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:451
#define AVERROR_EXPERIMENTAL
Requested feature is flagged experimental. Set strict_std_compliance if you really want to use it...
Definition: error.h:72
struct MJpegContext * mjpeg_ctx
Definition: mpegvideo.h:429
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:924
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
Definition: avcodec.h:2824
void ff_mjpeg_encode_dc(PutBitContext *pb, int val, uint8_t *huff_size, uint16_t *huff_code)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
uint8_t * intra_chroma_ac_vlc_last_length
Definition: mpegvideo.h:321
main external API structure.
Definition: avcodec.h:1502
ScanTable intra_scantable
Definition: mpegvideo.h:98
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:107
int ff_mpv_encode_init(AVCodecContext *avctx)
const uint8_t avpriv_mjpeg_val_dc[12]
Definition: jpegtables.c:67
Describe the class of an AVClass context structure.
Definition: log.h:67
av_cold void ff_mjpeg_encode_close(MpegEncContext *s)
Definition: mjpegenc.c:116
const uint8_t avpriv_mjpeg_bits_ac_chrominance[17]
Definition: jpegtables.c:99
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:209
const uint8_t avpriv_mjpeg_val_ac_chrominance[]
Definition: jpegtables.c:102
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
uint8_t level
Definition: svq3.c:150
MpegEncContext.
Definition: mpegvideo.h:88
struct AVCodecContext * avctx
Definition: mpegvideo.h:105
uint16_t huff_code_dc_luminance[12]
Definition: mjpegenc.h:44
PutBitContext pb
bit output
Definition: mpegvideo.h:158
uint8_t huff_size_dc_luminance[12]
Definition: mjpegenc.h:43
#define CODEC_FLAG_EMU_EDGE
Definition: avcodec.h:985
static av_cold void init_uni_ac_vlc(const uint8_t huff_size_ac[256], uint8_t *uni_ac_vlc_len)
Definition: mjpegenc.c:45
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:77
int ff_mpv_encode_end(AVCodecContext *avctx)
const uint8_t avpriv_mjpeg_bits_ac_luminance[17]
Definition: jpegtables.c:73
void * priv_data
Definition: avcodec.h:1544
int len
const uint8_t avpriv_mjpeg_val_ac_luminance[]
Definition: jpegtables.c:75
void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64])
Definition: mjpegenc.c:179
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> dc
#define av_freep(p)
int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
This structure stores compressed data.
Definition: avcodec.h:1400
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:2820
static int16_t block[64]
Definition: dct-test.c:110