]> git.sesse.net Git - ffmpeg/blob - libavcodec/mjpegenc.c
vp9: add fate test for 422.
[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 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
71 av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
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 */
88     ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
89                                  m->huff_code_dc_luminance,
90                                  avpriv_mjpeg_bits_dc_luminance,
91                                  avpriv_mjpeg_val_dc);
92     ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
93                                  m->huff_code_dc_chrominance,
94                                  avpriv_mjpeg_bits_dc_chrominance,
95                                  avpriv_mjpeg_val_dc);
96     ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
97                                  m->huff_code_ac_luminance,
98                                  avpriv_mjpeg_bits_ac_luminance,
99                                  avpriv_mjpeg_val_ac_luminance);
100     ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
101                                  m->huff_code_ac_chrominance,
102                                  avpriv_mjpeg_bits_ac_chrominance,
103                                  avpriv_mjpeg_val_ac_chrominance);
104
105     init_uni_ac_vlc(m->huff_size_ac_luminance,   uni_ac_vlc_len);
106     init_uni_ac_vlc(m->huff_size_ac_chrominance, uni_chroma_ac_vlc_len);
107     s->intra_ac_vlc_length      =
108     s->intra_ac_vlc_last_length = uni_ac_vlc_len;
109     s->intra_chroma_ac_vlc_length      =
110     s->intra_chroma_ac_vlc_last_length = uni_chroma_ac_vlc_len;
111
112     s->mjpeg_ctx = m;
113     return 0;
114 }
115
116 av_cold void ff_mjpeg_encode_close(MpegEncContext *s)
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) {
134         ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
135         huff_size_ac = m->huff_size_ac_luminance;
136         huff_code_ac = m->huff_code_ac_luminance;
137     } else {
138         ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
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
216 static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
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     //CODEC_FLAG_EMU_EDGE have to be cleared
228     if(s->avctx->flags & CODEC_FLAG_EMU_EDGE)
229         return AVERROR(EINVAL);
230
231     if ((avctx->height & 15) && avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
232         av_log(avctx, AV_LOG_ERROR,
233                "Heights which are not a multiple of 16 might fail with some decoders, "
234                "use vstrict=-1 / -strict -1 to use %d anyway.\n", avctx->height);
235         av_log(avctx, AV_LOG_WARNING, "If you have a device that plays AMV videos, please test if videos "
236                "with such heights work with it and report your findings to ffmpeg-devel@ffmpeg.org\n");
237         return AVERROR_EXPERIMENTAL;
238     }
239
240     pic = av_frame_clone(pic_arg);
241     if (!pic)
242         return AVERROR(ENOMEM);
243     //picture should be flipped upside-down
244     for(i=0; i < 3; i++) {
245         int vsample = i ? 2 >> chroma_v_shift : 2;
246         pic->data[i] += pic->linesize[i] * (vsample * s->height / V_MAX - 1);
247         pic->linesize[i] *= -1;
248     }
249     ret = ff_mpv_encode_picture(avctx, pkt, pic, got_packet);
250     av_frame_free(&pic);
251     return ret;
252 }
253
254 #if CONFIG_MJPEG_ENCODER
255 FF_MPV_GENERIC_CLASS(mjpeg)
256
257 AVCodec ff_mjpeg_encoder = {
258     .name           = "mjpeg",
259     .long_name      = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
260     .type           = AVMEDIA_TYPE_VIDEO,
261     .id             = AV_CODEC_ID_MJPEG,
262     .priv_data_size = sizeof(MpegEncContext),
263     .init           = ff_mpv_encode_init,
264     .encode2        = ff_mpv_encode_picture,
265     .close          = ff_mpv_encode_end,
266     .capabilities   = CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS | CODEC_CAP_INTRA_ONLY,
267     .pix_fmts       = (const enum AVPixelFormat[]){
268         AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE
269     },
270     .priv_class     = &mjpeg_class,
271 };
272 #endif
273 #if CONFIG_AMV_ENCODER
274 FF_MPV_GENERIC_CLASS(amv)
275
276 AVCodec ff_amv_encoder = {
277     .name           = "amv",
278     .long_name      = NULL_IF_CONFIG_SMALL("AMV Video"),
279     .type           = AVMEDIA_TYPE_VIDEO,
280     .id             = AV_CODEC_ID_AMV,
281     .priv_data_size = sizeof(MpegEncContext),
282     .init           = ff_mpv_encode_init,
283     .encode2        = amv_encode_picture,
284     .close          = ff_mpv_encode_end,
285     .pix_fmts       = (const enum AVPixelFormat[]){
286         AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_NONE
287     },
288     .priv_class     = &amv_class,
289 };
290 #endif