Libav
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 Libav.
12  *
13  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26  */
27 
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 
43 {
44  MJpegContext *m;
45 
46  m = av_malloc(sizeof(MJpegContext));
47  if (!m)
48  return AVERROR(ENOMEM);
49 
50  s->min_qcoeff=-1023;
51  s->max_qcoeff= 1023;
52 
53  /* build all the huffman tables */
70 
71  s->mjpeg_ctx = m;
72  return 0;
73 }
74 
76 {
77  av_free(s->mjpeg_ctx);
78 }
79 
80 static void encode_block(MpegEncContext *s, int16_t *block, int n)
81 {
82  int mant, nbits, code, i, j;
83  int component, dc, run, last_index, val;
84  MJpegContext *m = s->mjpeg_ctx;
85  uint8_t *huff_size_ac;
86  uint16_t *huff_code_ac;
87 
88  /* DC coef */
89  component = (n <= 3 ? 0 : (n&1) + 1);
90  dc = block[0]; /* overflow is impossible */
91  val = dc - s->last_dc[component];
92  if (n < 4) {
94  huff_size_ac = m->huff_size_ac_luminance;
95  huff_code_ac = m->huff_code_ac_luminance;
96  } else {
98  huff_size_ac = m->huff_size_ac_chrominance;
99  huff_code_ac = m->huff_code_ac_chrominance;
100  }
101  s->last_dc[component] = dc;
102 
103  /* AC coefs */
104 
105  run = 0;
106  last_index = s->block_last_index[n];
107  for(i=1;i<=last_index;i++) {
108  j = s->intra_scantable.permutated[i];
109  val = block[j];
110  if (val == 0) {
111  run++;
112  } else {
113  while (run >= 16) {
114  put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
115  run -= 16;
116  }
117  mant = val;
118  if (val < 0) {
119  val = -val;
120  mant--;
121  }
122 
123  nbits= av_log2(val) + 1;
124  code = (run << 4) | nbits;
125 
126  put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
127 
128  put_sbits(&s->pb, nbits, mant);
129  run = 0;
130  }
131  }
132 
133  /* output EOB only if not already 64 values */
134  if (last_index < 63 || run != 0)
135  put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
136 }
137 
138 void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[8][64])
139 {
140  int i;
141  for(i=0;i<5;i++) {
142  encode_block(s, block[i], i);
143  }
144  if (s->chroma_format == CHROMA_420) {
145  encode_block(s, block[5], 5);
146  } else {
147  encode_block(s, block[6], 6);
148  encode_block(s, block[5], 5);
149  encode_block(s, block[7], 7);
150  }
151 
152  s->i_tex_bits += get_bits_diff(s);
153 }
154 
155 #define OFFSET(x) offsetof(MpegEncContext, x)
156 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
157 static const AVOption options[] = {
158 { "pred", "Prediction method", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 3, VE, "pred" },
159  { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
160  { "plane", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, VE, "pred" },
161  { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 3 }, INT_MIN, INT_MAX, VE, "pred" },
162 
163  { NULL},
164 };
165 
166 static const AVClass mjpeg_class = {
167  .class_name = "mjpeg",
168  .item_name = av_default_item_name,
169  .option = options,
170  .version = LIBAVUTIL_VERSION_INT,
171 };
172 
174  .name = "mjpeg",
175  .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
176  .type = AVMEDIA_TYPE_VIDEO,
177  .id = AV_CODEC_ID_MJPEG,
178  .priv_data_size = sizeof(MpegEncContext),
179  .priv_class = &mjpeg_class,
181  .encode2 = ff_mpv_encode_picture,
182  .close = ff_mpv_encode_end,
183  .pix_fmts = (const enum AVPixelFormat[]){
185  },
186 };
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
AVOption.
Definition: opt.h:234
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:172
MJPEG encoder.
int min_qcoeff
minimum encodable coefficient
Definition: mpegvideo.h:301
mpegvideo header.
av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
Definition: mjpegenc.c:42
uint8_t permutated[64]
Definition: idctdsp.h:31
uint8_t run
Definition: svq3.c:203
AVCodec.
Definition: avcodec.h:3120
MJPEG encoder and decoder.
uint16_t huff_code_dc_chrominance[12]
Definition: mjpegenc.h:46
static int16_t block[64]
Definition: dct.c:97
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:39
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:66
static void encode_block(MpegEncContext *s, int16_t *block, int n)
Definition: mjpegenc.c:80
static const AVClass mjpeg_class
Definition: mjpegenc.c:166
const uint8_t avpriv_mjpeg_bits_dc_luminance[17]
Definition: jpegtables.c:65
#define CHROMA_420
Definition: mpegvideo.h:457
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:72
uint8_t huff_size_dc_chrominance[12]
Definition: mjpegenc.h:45
int max_qcoeff
maximum encodable coefficient
Definition: mpegvideo.h:302
static int get_bits_diff(MpegEncContext *s)
Definition: mpegvideo.h:694
uint16_t huff_code_ac_chrominance[256]
Definition: mjpegenc.h:51
AVCodec ff_mjpeg_encoder
Definition: mjpegenc.c:173
uint8_t huff_size_ac_luminance[256]
Definition: mjpegenc.h:48
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
int last_dc[3]
last DC values for MPEG-1
Definition: mpegvideo.h:180
#define AVERROR(e)
Definition: error.h:43
const uint8_t avpriv_mjpeg_bits_dc_chrominance[17]
Definition: jpegtables.c:70
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
uint8_t huff_size_ac_chrominance[256]
Definition: mjpegenc.h:50
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:134
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:71
int block_last_index[12]
last non zero coefficient in block
Definition: mpegvideo.h:81
void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[8][64])
Definition: mjpegenc.c:138
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
static const float pred[4]
Definition: siprdata.h:259
struct MJpegContext * mjpeg_ctx
Definition: mpegvideo.h:409
#define OFFSET(x)
Definition: mjpegenc.c:155
NULL
Definition: eval.c:55
void ff_mjpeg_encode_dc(PutBitContext *pb, int val, uint8_t *huff_size, uint16_t *huff_code)
Libavcodec external API header.
av_default_item_name
Definition: dnxhdenc.c:55
void ff_mjpeg_encode_close(MpegEncContext *s)
Definition: mjpegenc.c:75
ScanTable intra_scantable
Definition: mpegvideo.h:86
int ff_mpv_encode_init(AVCodecContext *avctx)
#define VE
Definition: mjpegenc.c:156
const uint8_t avpriv_mjpeg_val_dc[12]
Definition: jpegtables.c:67
Describe the class of an AVClass context structure.
Definition: log.h:34
const uint8_t avpriv_mjpeg_bits_ac_chrominance[17]
Definition: jpegtables.c:99
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:257
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;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);return NULL;} return ac;} 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;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->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);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> dc
const uint8_t avpriv_mjpeg_val_ac_chrominance[]
Definition: jpegtables.c:102
MpegEncContext.
Definition: mpegvideo.h:76
uint16_t huff_code_dc_luminance[12]
Definition: mjpegenc.h:44
PutBitContext pb
bit output
Definition: mpegvideo.h:146
uint8_t huff_size_dc_luminance[12]
Definition: mjpegenc.h:43
int ff_mpv_encode_end(AVCodecContext *avctx)
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
const uint8_t avpriv_mjpeg_bits_ac_luminance[17]
Definition: jpegtables.c:73
const uint8_t avpriv_mjpeg_val_ac_luminance[]
Definition: jpegtables.c:75
#define av_log2
Definition: intmath.h:85
int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
static const AVOption options[]
Definition: mjpegenc.c:157