]> git.sesse.net Git - ffmpeg/blob - libavcodec/asvenc.c
hqx: Store shareable data in main decoder context
[ffmpeg] / libavcodec / asvenc.c
1 /*
2  * Copyright (c) 2003 Michael Niedermayer
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * ASUS V1/V2 encoder.
24  */
25
26 #include "libavutil/attributes.h"
27 #include "libavutil/mem.h"
28
29 #include "asv.h"
30 #include "avcodec.h"
31 #include "fdctdsp.h"
32 #include "mathops.h"
33 #include "mpeg12data.h"
34
35 static inline void asv2_put_bits(PutBitContext *pb, int n, int v)
36 {
37     put_bits(pb, n, ff_reverse[v << (8 - n)]);
38 }
39
40 static inline void asv1_put_level(PutBitContext *pb, int level)
41 {
42     unsigned int index = level + 3;
43
44     if (index <= 6) {
45         put_bits(pb, ff_asv_level_tab[index][1], ff_asv_level_tab[index][0]);
46     } else {
47         put_bits(pb, ff_asv_level_tab[3][1], ff_asv_level_tab[3][0]);
48         put_sbits(pb, 8, level);
49     }
50 }
51
52 static inline void asv2_put_level(PutBitContext *pb, int level)
53 {
54     unsigned int index = level + 31;
55
56     if (index <= 62) {
57         put_bits(pb, ff_asv2_level_tab[index][1], ff_asv2_level_tab[index][0]);
58     } else {
59         put_bits(pb, ff_asv2_level_tab[31][1], ff_asv2_level_tab[31][0]);
60         asv2_put_bits(pb, 8, level & 0xFF);
61     }
62 }
63
64 static inline void asv1_encode_block(ASV1Context *a, int16_t block[64])
65 {
66     int i;
67     int nc_count = 0;
68
69     put_bits(&a->pb, 8, (block[0] + 32) >> 6);
70     block[0] = 0;
71
72     for (i = 0; i < 10; i++) {
73         const int index = ff_asv_scantab[4 * i];
74         int ccp         = 0;
75
76         if ((block[index + 0] = (block[index + 0] *
77                                  a->q_intra_matrix[index + 0] + (1 << 15)) >> 16))
78             ccp |= 8;
79         if ((block[index + 8] = (block[index + 8] *
80                                  a->q_intra_matrix[index + 8] + (1 << 15)) >> 16))
81             ccp |= 4;
82         if ((block[index + 1] = (block[index + 1] *
83                                  a->q_intra_matrix[index + 1] + (1 << 15)) >> 16))
84             ccp |= 2;
85         if ((block[index + 9] = (block[index + 9] *
86                                  a->q_intra_matrix[index + 9] + (1 << 15)) >> 16))
87             ccp |= 1;
88
89         if (ccp) {
90             for (; nc_count; nc_count--)
91                 put_bits(&a->pb, ff_asv_ccp_tab[0][1], ff_asv_ccp_tab[0][0]);
92
93             put_bits(&a->pb, ff_asv_ccp_tab[ccp][1], ff_asv_ccp_tab[ccp][0]);
94
95             if (ccp & 8)
96                 asv1_put_level(&a->pb, block[index + 0]);
97             if (ccp & 4)
98                 asv1_put_level(&a->pb, block[index + 8]);
99             if (ccp & 2)
100                 asv1_put_level(&a->pb, block[index + 1]);
101             if (ccp & 1)
102                 asv1_put_level(&a->pb, block[index + 9]);
103         } else {
104             nc_count++;
105         }
106     }
107     put_bits(&a->pb, ff_asv_ccp_tab[16][1], ff_asv_ccp_tab[16][0]);
108 }
109
110 static inline int asv2_encode_block(ASV1Context *a, int16_t block[64])
111 {
112     int i;
113     int count = 0;
114
115     for (count = 63; count > 3; count--) {
116         const int index = ff_asv_scantab[count];
117         if ((block[index] * a->q_intra_matrix[index] + (1 << 15)) >> 16)
118             break;
119     }
120
121     count >>= 2;
122
123     asv2_put_bits(&a->pb, 4, count);
124     asv2_put_bits(&a->pb, 8, (block[0] + 32) >> 6);
125     block[0] = 0;
126
127     for (i = 0; i <= count; i++) {
128         const int index = ff_asv_scantab[4 * i];
129         int ccp         = 0;
130
131         if ((block[index + 0] = (block[index + 0] *
132                                  a->q_intra_matrix[index + 0] + (1 << 15)) >> 16))
133             ccp |= 8;
134         if ((block[index + 8] = (block[index + 8] *
135                                  a->q_intra_matrix[index + 8] + (1 << 15)) >> 16))
136             ccp |= 4;
137         if ((block[index + 1] = (block[index + 1] *
138                                  a->q_intra_matrix[index + 1] + (1 << 15)) >> 16))
139             ccp |= 2;
140         if ((block[index + 9] = (block[index + 9] *
141                                  a->q_intra_matrix[index + 9] + (1 << 15)) >> 16))
142             ccp |= 1;
143
144         if (!i && ccp >= 8)
145             return AVERROR_BUG;
146         if (i)
147             put_bits(&a->pb, ff_asv_ac_ccp_tab[ccp][1], ff_asv_ac_ccp_tab[ccp][0]);
148         else
149             put_bits(&a->pb, ff_asv_dc_ccp_tab[ccp][1], ff_asv_dc_ccp_tab[ccp][0]);
150
151         if (ccp) {
152             if (ccp & 8)
153                 asv2_put_level(&a->pb, block[index + 0]);
154             if (ccp & 4)
155                 asv2_put_level(&a->pb, block[index + 8]);
156             if (ccp & 2)
157                 asv2_put_level(&a->pb, block[index + 1]);
158             if (ccp & 1)
159                 asv2_put_level(&a->pb, block[index + 9]);
160         }
161     }
162
163     return 0;
164 }
165
166 #define MAX_MB_SIZE (30 * 16 * 16 * 3 / 2 / 8)
167
168 static inline int encode_mb(ASV1Context *a, int16_t block[6][64])
169 {
170     int i, ret;
171
172     if (a->pb.buf_end - a->pb.buf - (put_bits_count(&a->pb) >> 3) < MAX_MB_SIZE) {
173         av_log(a->avctx, AV_LOG_ERROR, "encoded frame too large\n");
174         return -1;
175     }
176
177     if (a->avctx->codec_id == AV_CODEC_ID_ASV1) {
178         for (i = 0; i < 6; i++)
179             asv1_encode_block(a, block[i]);
180     } else {
181         for (i = 0; i < 6; i++) {
182             ret = asv2_encode_block(a, block[i]);
183             if (ret < 0)
184                 return ret;
185         }
186     }
187     return 0;
188 }
189
190 static inline void dct_get(ASV1Context *a, const AVFrame *frame,
191                            int mb_x, int mb_y)
192 {
193     int16_t (*block)[64] = a->block;
194     int linesize = frame->linesize[0];
195     int i;
196
197     uint8_t *ptr_y  = frame->data[0] + (mb_y * 16 * linesize)           + mb_x * 16;
198     uint8_t *ptr_cb = frame->data[1] + (mb_y *  8 * frame->linesize[1]) + mb_x *  8;
199     uint8_t *ptr_cr = frame->data[2] + (mb_y *  8 * frame->linesize[2]) + mb_x *  8;
200
201     a->pdsp.get_pixels(block[0], ptr_y,                    linesize);
202     a->pdsp.get_pixels(block[1], ptr_y + 8,                linesize);
203     a->pdsp.get_pixels(block[2], ptr_y + 8 * linesize,     linesize);
204     a->pdsp.get_pixels(block[3], ptr_y + 8 * linesize + 8, linesize);
205     for (i = 0; i < 4; i++)
206         a->fdsp.fdct(block[i]);
207
208     if (!(a->avctx->flags & CODEC_FLAG_GRAY)) {
209         a->pdsp.get_pixels(block[4], ptr_cb, frame->linesize[1]);
210         a->pdsp.get_pixels(block[5], ptr_cr, frame->linesize[2]);
211         for (i = 4; i < 6; i++)
212             a->fdsp.fdct(block[i]);
213     }
214 }
215
216 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
217                         const AVFrame *pict, int *got_packet)
218 {
219     ASV1Context *const a = avctx->priv_data;
220     int size, ret;
221     int mb_x, mb_y;
222
223     if (!pkt->data &&
224         (ret = av_new_packet(pkt, a->mb_height * a->mb_width * MAX_MB_SIZE +
225                              FF_MIN_BUFFER_SIZE)) < 0) {
226         av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
227         return ret;
228     }
229
230     init_put_bits(&a->pb, pkt->data, pkt->size);
231
232     for (mb_y = 0; mb_y < a->mb_height2; mb_y++) {
233         for (mb_x = 0; mb_x < a->mb_width2; mb_x++) {
234             dct_get(a, pict, mb_x, mb_y);
235             encode_mb(a, a->block);
236         }
237     }
238
239     if (a->mb_width2 != a->mb_width) {
240         mb_x = a->mb_width2;
241         for (mb_y = 0; mb_y < a->mb_height2; mb_y++) {
242             dct_get(a, pict, mb_x, mb_y);
243             encode_mb(a, a->block);
244         }
245     }
246
247     if (a->mb_height2 != a->mb_height) {
248         mb_y = a->mb_height2;
249         for (mb_x = 0; mb_x < a->mb_width; mb_x++) {
250             dct_get(a, pict, mb_x, mb_y);
251             encode_mb(a, a->block);
252         }
253     }
254     emms_c();
255
256     avpriv_align_put_bits(&a->pb);
257     while (put_bits_count(&a->pb) & 31)
258         put_bits(&a->pb, 8, 0);
259
260     size = put_bits_count(&a->pb) / 32;
261
262     if (avctx->codec_id == AV_CODEC_ID_ASV1) {
263         a->bbdsp.bswap_buf((uint32_t *) pkt->data,
264                            (uint32_t *) pkt->data, size);
265     } else {
266         int i;
267         for (i = 0; i < 4 * size; i++)
268             pkt->data[i] = ff_reverse[pkt->data[i]];
269     }
270
271     pkt->size   = size * 4;
272     pkt->flags |= AV_PKT_FLAG_KEY;
273     *got_packet = 1;
274
275     return 0;
276 }
277
278 static av_cold int encode_init(AVCodecContext *avctx)
279 {
280     ASV1Context *const a = avctx->priv_data;
281     int i;
282     const int scale = avctx->codec_id == AV_CODEC_ID_ASV1 ? 1 : 2;
283
284     avctx->coded_frame = av_frame_alloc();
285     if (!avctx->coded_frame)
286         return AVERROR(ENOMEM);
287     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
288     avctx->coded_frame->key_frame = 1;
289
290     ff_asv_common_init(avctx);
291     ff_fdctdsp_init(&a->fdsp, avctx);
292     ff_pixblockdsp_init(&a->pdsp, avctx);
293
294     if (avctx->global_quality == 0)
295         avctx->global_quality = 4 * FF_QUALITY_SCALE;
296
297     a->inv_qscale = (32 * scale * FF_QUALITY_SCALE +
298                      avctx->global_quality / 2) / avctx->global_quality;
299
300     avctx->extradata                   = av_mallocz(8);
301     avctx->extradata_size              = 8;
302     ((uint32_t *) avctx->extradata)[0] = av_le2ne32(a->inv_qscale);
303     ((uint32_t *) avctx->extradata)[1] = av_le2ne32(AV_RL32("ASUS"));
304
305     for (i = 0; i < 64; i++) {
306         int q = 32 * scale * ff_mpeg1_default_intra_matrix[i];
307         a->q_intra_matrix[i] = ((a->inv_qscale << 16) + q / 2) / q;
308     }
309
310     return 0;
311 }
312
313 static av_cold int asv_encode_close(AVCodecContext *avctx)
314 {
315     av_frame_free(&avctx->coded_frame);
316
317     return 0;
318 }
319
320 #if CONFIG_ASV1_ENCODER
321 AVCodec ff_asv1_encoder = {
322     .name           = "asv1",
323     .long_name      = NULL_IF_CONFIG_SMALL("ASUS V1"),
324     .type           = AVMEDIA_TYPE_VIDEO,
325     .id             = AV_CODEC_ID_ASV1,
326     .priv_data_size = sizeof(ASV1Context),
327     .init           = encode_init,
328     .encode2        = encode_frame,
329     .close          = asv_encode_close,
330     .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
331                                                      AV_PIX_FMT_NONE },
332 };
333 #endif
334
335 #if CONFIG_ASV2_ENCODER
336 AVCodec ff_asv2_encoder = {
337     .name           = "asv2",
338     .long_name      = NULL_IF_CONFIG_SMALL("ASUS V2"),
339     .type           = AVMEDIA_TYPE_VIDEO,
340     .id             = AV_CODEC_ID_ASV2,
341     .priv_data_size = sizeof(ASV1Context),
342     .init           = encode_init,
343     .encode2        = encode_frame,
344     .close          = asv_encode_close,
345     .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
346                                                      AV_PIX_FMT_NONE },
347 };
348 #endif