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