build: Split JPEG-related tables off into a separate component
[libav.git] / 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 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
28 /**
29 * @file
30 * MJPEG encoder.
31 */
32
33 #include <assert.h>
34
35 #include "libavutil/pixdesc.h"
36
37 #include "avcodec.h"
38 #include "jpegtables.h"
39 #include "mjpegenc_common.h"
40 #include "mpegvideo.h"
41 #include "mjpeg.h"
42 #include "mjpegenc.h"
43
44 av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
45 {
46 MJpegContext *m;
47
48 m = av_malloc(sizeof(MJpegContext));
49 if (!m)
50 return AVERROR(ENOMEM);
51
52 s->min_qcoeff=-1023;
53 s->max_qcoeff= 1023;
54
55 /* build all the huffman tables */
56 ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
57 m->huff_code_dc_luminance,
58 avpriv_mjpeg_bits_dc_luminance,
59 avpriv_mjpeg_val_dc);
60 ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
61 m->huff_code_dc_chrominance,
62 avpriv_mjpeg_bits_dc_chrominance,
63 avpriv_mjpeg_val_dc);
64 ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
65 m->huff_code_ac_luminance,
66 avpriv_mjpeg_bits_ac_luminance,
67 avpriv_mjpeg_val_ac_luminance);
68 ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
69 m->huff_code_ac_chrominance,
70 avpriv_mjpeg_bits_ac_chrominance,
71 avpriv_mjpeg_val_ac_chrominance);
72
73 s->mjpeg_ctx = m;
74 return 0;
75 }
76
77 void ff_mjpeg_encode_close(MpegEncContext *s)
78 {
79 av_free(s->mjpeg_ctx);
80 }
81
82 static void encode_block(MpegEncContext *s, int16_t *block, int n)
83 {
84 int mant, nbits, code, i, j;
85 int component, dc, run, last_index, val;
86 MJpegContext *m = s->mjpeg_ctx;
87 uint8_t *huff_size_ac;
88 uint16_t *huff_code_ac;
89
90 /* DC coef */
91 component = (n <= 3 ? 0 : (n&1) + 1);
92 dc = block[0]; /* overflow is impossible */
93 val = dc - s->last_dc[component];
94 if (n < 4) {
95 ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
96 huff_size_ac = m->huff_size_ac_luminance;
97 huff_code_ac = m->huff_code_ac_luminance;
98 } else {
99 ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
100 huff_size_ac = m->huff_size_ac_chrominance;
101 huff_code_ac = m->huff_code_ac_chrominance;
102 }
103 s->last_dc[component] = dc;
104
105 /* AC coefs */
106
107 run = 0;
108 last_index = s->block_last_index[n];
109 for(i=1;i<=last_index;i++) {
110 j = s->intra_scantable.permutated[i];
111 val = block[j];
112 if (val == 0) {
113 run++;
114 } else {
115 while (run >= 16) {
116 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
117 run -= 16;
118 }
119 mant = val;
120 if (val < 0) {
121 val = -val;
122 mant--;
123 }
124
125 nbits= av_log2(val) + 1;
126 code = (run << 4) | nbits;
127
128 put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
129
130 put_sbits(&s->pb, nbits, mant);
131 run = 0;
132 }
133 }
134
135 /* output EOB only if not already 64 values */
136 if (last_index < 63 || run != 0)
137 put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
138 }
139
140 void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[8][64])
141 {
142 int i;
143 for(i=0;i<5;i++) {
144 encode_block(s, block[i], i);
145 }
146 if (s->chroma_format == CHROMA_420) {
147 encode_block(s, block[5], 5);
148 } else {
149 encode_block(s, block[6], 6);
150 encode_block(s, block[5], 5);
151 encode_block(s, block[7], 7);
152 }
153
154 s->i_tex_bits += get_bits_diff(s);
155 }
156
157 AVCodec ff_mjpeg_encoder = {
158 .name = "mjpeg",
159 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
160 .type = AVMEDIA_TYPE_VIDEO,
161 .id = AV_CODEC_ID_MJPEG,
162 .priv_data_size = sizeof(MpegEncContext),
163 .init = ff_mpv_encode_init,
164 .encode2 = ff_mpv_encode_picture,
165 .close = ff_mpv_encode_end,
166 .pix_fmts = (const enum AVPixelFormat[]){
167 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_NONE
168 },
169 };