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