2 * Copyright (c) 2003 Michael Niedermayer
4 * This file is part of Libav.
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.
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.
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
26 #include "libavutil/attributes.h"
27 #include "libavutil/mem.h"
33 #include "mpeg12data.h"
35 static inline void asv2_put_bits(PutBitContext *pb, int n, int v)
37 put_bits(pb, n, ff_reverse[v << (8 - n)]);
40 static inline void asv1_put_level(PutBitContext *pb, int level)
42 unsigned int index = level + 3;
45 put_bits(pb, ff_asv_level_tab[index][1], ff_asv_level_tab[index][0]);
47 put_bits(pb, ff_asv_level_tab[3][1], ff_asv_level_tab[3][0]);
48 put_sbits(pb, 8, level);
52 static inline void asv2_put_level(PutBitContext *pb, int level)
54 unsigned int index = level + 31;
57 put_bits(pb, ff_asv2_level_tab[index][1], ff_asv2_level_tab[index][0]);
59 put_bits(pb, ff_asv2_level_tab[31][1], ff_asv2_level_tab[31][0]);
60 asv2_put_bits(pb, 8, level & 0xFF);
64 static inline void asv1_encode_block(ASV1Context *a, int16_t block[64])
69 put_bits(&a->pb, 8, (block[0] + 32) >> 6);
72 for (i = 0; i < 10; i++) {
73 const int index = ff_asv_scantab[4 * i];
76 if ((block[index + 0] = (block[index + 0] *
77 a->q_intra_matrix[index + 0] + (1 << 15)) >> 16))
79 if ((block[index + 8] = (block[index + 8] *
80 a->q_intra_matrix[index + 8] + (1 << 15)) >> 16))
82 if ((block[index + 1] = (block[index + 1] *
83 a->q_intra_matrix[index + 1] + (1 << 15)) >> 16))
85 if ((block[index + 9] = (block[index + 9] *
86 a->q_intra_matrix[index + 9] + (1 << 15)) >> 16))
90 for (; nc_count; nc_count--)
91 put_bits(&a->pb, ff_asv_ccp_tab[0][1], ff_asv_ccp_tab[0][0]);
93 put_bits(&a->pb, ff_asv_ccp_tab[ccp][1], ff_asv_ccp_tab[ccp][0]);
96 asv1_put_level(&a->pb, block[index + 0]);
98 asv1_put_level(&a->pb, block[index + 8]);
100 asv1_put_level(&a->pb, block[index + 1]);
102 asv1_put_level(&a->pb, block[index + 9]);
107 put_bits(&a->pb, ff_asv_ccp_tab[16][1], ff_asv_ccp_tab[16][0]);
110 static inline void asv2_encode_block(ASV1Context *a, int16_t block[64])
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)
123 asv2_put_bits(&a->pb, 4, count);
124 asv2_put_bits(&a->pb, 8, (block[0] + 32) >> 6);
127 for (i = 0; i <= count; i++) {
128 const int index = ff_asv_scantab[4 * i];
131 if ((block[index + 0] = (block[index + 0] *
132 a->q_intra_matrix[index + 0] + (1 << 15)) >> 16))
134 if ((block[index + 8] = (block[index + 8] *
135 a->q_intra_matrix[index + 8] + (1 << 15)) >> 16))
137 if ((block[index + 1] = (block[index + 1] *
138 a->q_intra_matrix[index + 1] + (1 << 15)) >> 16))
140 if ((block[index + 9] = (block[index + 9] *
141 a->q_intra_matrix[index + 9] + (1 << 15)) >> 16))
144 assert(i || ccp < 8);
146 put_bits(&a->pb, ff_asv_ac_ccp_tab[ccp][1], ff_asv_ac_ccp_tab[ccp][0]);
148 put_bits(&a->pb, ff_asv_dc_ccp_tab[ccp][1], ff_asv_dc_ccp_tab[ccp][0]);
152 asv2_put_level(&a->pb, block[index + 0]);
154 asv2_put_level(&a->pb, block[index + 8]);
156 asv2_put_level(&a->pb, block[index + 1]);
158 asv2_put_level(&a->pb, block[index + 9]);
163 #define MAX_MB_SIZE (30 * 16 * 16 * 3 / 2 / 8)
165 static inline int encode_mb(ASV1Context *a, int16_t block[6][64])
169 if (a->pb.buf_end - a->pb.buf - (put_bits_count(&a->pb) >> 3) < MAX_MB_SIZE) {
170 av_log(a->avctx, AV_LOG_ERROR, "encoded frame too large\n");
174 if (a->avctx->codec_id == AV_CODEC_ID_ASV1) {
175 for (i = 0; i < 6; i++)
176 asv1_encode_block(a, block[i]);
178 for (i = 0; i < 6; i++)
179 asv2_encode_block(a, block[i]);
184 static inline void dct_get(ASV1Context *a, const AVFrame *frame,
187 int16_t (*block)[64] = a->block;
188 int linesize = frame->linesize[0];
191 uint8_t *ptr_y = frame->data[0] + (mb_y * 16 * linesize) + mb_x * 16;
192 uint8_t *ptr_cb = frame->data[1] + (mb_y * 8 * frame->linesize[1]) + mb_x * 8;
193 uint8_t *ptr_cr = frame->data[2] + (mb_y * 8 * frame->linesize[2]) + mb_x * 8;
195 a->pdsp.get_pixels(block[0], ptr_y, linesize);
196 a->pdsp.get_pixels(block[1], ptr_y + 8, linesize);
197 a->pdsp.get_pixels(block[2], ptr_y + 8 * linesize, linesize);
198 a->pdsp.get_pixels(block[3], ptr_y + 8 * linesize + 8, linesize);
199 for (i = 0; i < 4; i++)
200 a->fdsp.fdct(block[i]);
202 if (!(a->avctx->flags & CODEC_FLAG_GRAY)) {
203 a->pdsp.get_pixels(block[4], ptr_cb, frame->linesize[1]);
204 a->pdsp.get_pixels(block[5], ptr_cr, frame->linesize[2]);
205 for (i = 4; i < 6; i++)
206 a->fdsp.fdct(block[i]);
210 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
211 const AVFrame *pict, int *got_packet)
213 ASV1Context *const a = avctx->priv_data;
218 (ret = av_new_packet(pkt, a->mb_height * a->mb_width * MAX_MB_SIZE +
219 FF_MIN_BUFFER_SIZE)) < 0) {
220 av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
224 init_put_bits(&a->pb, pkt->data, pkt->size);
226 for (mb_y = 0; mb_y < a->mb_height2; mb_y++) {
227 for (mb_x = 0; mb_x < a->mb_width2; mb_x++) {
228 dct_get(a, pict, mb_x, mb_y);
229 encode_mb(a, a->block);
233 if (a->mb_width2 != a->mb_width) {
235 for (mb_y = 0; mb_y < a->mb_height2; mb_y++) {
236 dct_get(a, pict, mb_x, mb_y);
237 encode_mb(a, a->block);
241 if (a->mb_height2 != a->mb_height) {
242 mb_y = a->mb_height2;
243 for (mb_x = 0; mb_x < a->mb_width; mb_x++) {
244 dct_get(a, pict, mb_x, mb_y);
245 encode_mb(a, a->block);
250 avpriv_align_put_bits(&a->pb);
251 while (put_bits_count(&a->pb) & 31)
252 put_bits(&a->pb, 8, 0);
254 size = put_bits_count(&a->pb) / 32;
256 if (avctx->codec_id == AV_CODEC_ID_ASV1) {
257 a->bbdsp.bswap_buf((uint32_t *) pkt->data,
258 (uint32_t *) pkt->data, size);
261 for (i = 0; i < 4 * size; i++)
262 pkt->data[i] = ff_reverse[pkt->data[i]];
265 pkt->size = size * 4;
266 pkt->flags |= AV_PKT_FLAG_KEY;
272 static av_cold int encode_init(AVCodecContext *avctx)
274 ASV1Context *const a = avctx->priv_data;
276 const int scale = avctx->codec_id == AV_CODEC_ID_ASV1 ? 1 : 2;
278 avctx->coded_frame = av_frame_alloc();
279 if (!avctx->coded_frame)
280 return AVERROR(ENOMEM);
281 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
282 avctx->coded_frame->key_frame = 1;
284 ff_asv_common_init(avctx);
285 ff_fdctdsp_init(&a->fdsp, avctx);
286 ff_pixblockdsp_init(&a->pdsp, avctx);
288 if (avctx->global_quality == 0)
289 avctx->global_quality = 4 * FF_QUALITY_SCALE;
291 a->inv_qscale = (32 * scale * FF_QUALITY_SCALE +
292 avctx->global_quality / 2) / avctx->global_quality;
294 avctx->extradata = av_mallocz(8);
295 avctx->extradata_size = 8;
296 ((uint32_t *) avctx->extradata)[0] = av_le2ne32(a->inv_qscale);
297 ((uint32_t *) avctx->extradata)[1] = av_le2ne32(AV_RL32("ASUS"));
299 for (i = 0; i < 64; i++) {
300 int q = 32 * scale * ff_mpeg1_default_intra_matrix[i];
301 a->q_intra_matrix[i] = ((a->inv_qscale << 16) + q / 2) / q;
307 static av_cold int asv_encode_close(AVCodecContext *avctx)
309 av_frame_free(&avctx->coded_frame);
314 #if CONFIG_ASV1_ENCODER
315 AVCodec ff_asv1_encoder = {
317 .long_name = NULL_IF_CONFIG_SMALL("ASUS V1"),
318 .type = AVMEDIA_TYPE_VIDEO,
319 .id = AV_CODEC_ID_ASV1,
320 .priv_data_size = sizeof(ASV1Context),
322 .encode2 = encode_frame,
323 .close = asv_encode_close,
324 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
329 #if CONFIG_ASV2_ENCODER
330 AVCodec ff_asv2_encoder = {
332 .long_name = NULL_IF_CONFIG_SMALL("ASUS V2"),
333 .type = AVMEDIA_TYPE_VIDEO,
334 .id = AV_CODEC_ID_ASV2,
335 .priv_data_size = sizeof(ASV1Context),
337 .encode2 = encode_frame,
338 .close = asv_encode_close,
339 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,