annotate anm.c @ 11305:1a349d669184 libavcodec

Optimize (amvd>2)+(amvd>32), about 1 cpu cycles faster. patch by Zhou Zongyi @ zhouzy () os punkt pku dot edu speck cn
author michael
date Fri, 26 Feb 2010 22:45:35 +0000
parents d1115fb7e914
children 8a4984c5cacc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10835
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
1 /*
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
2 * Deluxe Paint Animation decoder
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
3 * Copyright (c) 2009 Peter Ross
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
4 *
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
5 * This file is part of FFmpeg.
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
6 *
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
11 *
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
15 * Lesser General Public License for more details.
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
16 *
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
20 */
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
21
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
22 /**
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
23 * @file libavcodec/anm.c
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
24 * Deluxe Paint Animation decoder
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
25 */
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
26
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
27 #include "avcodec.h"
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
28 #include "bytestream.h"
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
29
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
30 typedef struct AnmContext {
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
31 AVFrame frame;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
32 int x; ///< x coordinate position
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
33 } AnmContext;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
34
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
35 static av_cold int decode_init(AVCodecContext *avctx)
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
36 {
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
37 AnmContext *s = avctx->priv_data;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
38 const uint8_t *buf;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
39 int i;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
40
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
41 avctx->pix_fmt = PIX_FMT_PAL8;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
42
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
43 if (avctx->extradata_size != 16*8 + 4*256)
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
44 return -1;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
45
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
46 s->frame.reference = 1;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
47 if (avctx->get_buffer(avctx, &s->frame) < 0) {
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
48 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
49 return -1;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
50 }
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
51
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
52 buf = avctx->extradata + 16*8;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
53 for (i = 0; i < 256; i++)
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
54 ((uint32_t*)s->frame.data[1])[i] = bytestream_get_le32(&buf);
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
55
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
56 return 0;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
57 }
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
58
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
59 /**
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
60 * Perform decode operation
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
61 * @param dst, dst_end Destination image buffer
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
62 * @param buf, buf_end Source buffer (optional, see below)
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
63 * @param pixel Fill color (optional, see below)
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
64 * @param count Pixel count
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
65 * @param x Pointer to x-axis counter
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
66 * @param width Image width
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
67 * @param linesize Destination image buffer linesize
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
68 * @return non-zero if destination buffer is exhausted
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
69 *
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
70 * a copy operation is achieved when 'buf' is set
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
71 * a fill operation is acheived when 'buf' is null and pixel is >= 0
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
72 * a skip operation is acheived when 'buf' is null and pixel is < 0
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
73 */
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
74 static inline int op(uint8_t **dst, const uint8_t *dst_end,
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
75 const uint8_t **buf, const uint8_t *buf_end,
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
76 int pixel, int count,
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
77 int *x, int width, int linesize)
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
78 {
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
79 int remaining = width - *x;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
80 while(count > 0) {
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
81 int striplen = FFMIN(count, remaining);
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
82 if (buf) {
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
83 striplen = FFMIN(striplen, buf_end - *buf);
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
84 memcpy(*dst, *buf, striplen);
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
85 *buf += striplen;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
86 } else if (pixel >= 0)
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
87 memset(*dst, pixel, striplen);
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
88 *dst += striplen;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
89 remaining -= striplen;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
90 count -= striplen;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
91 if (remaining <= 0) {
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
92 *dst += linesize - width;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
93 remaining = width;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
94 }
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
95 if (linesize > 0) {
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
96 if (*dst >= dst_end) goto exhausted;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
97 } else {
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
98 if (*dst <= dst_end) goto exhausted;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
99 }
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
100 }
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
101 *x = width - remaining;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
102 return 0;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
103
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
104 exhausted:
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
105 *x = width - remaining;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
106 return 1;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
107 }
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
108
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
109 static int decode_frame(AVCodecContext *avctx,
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
110 void *data, int *data_size,
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
111 AVPacket *avpkt)
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
112 {
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
113 AnmContext *s = avctx->priv_data;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
114 const uint8_t *buf = avpkt->data;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
115 const int buf_size = avpkt->size;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
116 const uint8_t *buf_end = buf + buf_size;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
117 uint8_t *dst, *dst_end;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
118 int count;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
119
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
120 if(avctx->reget_buffer(avctx, &s->frame) < 0){
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
121 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
122 return -1;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
123 }
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
124 dst = s->frame.data[0];
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
125 dst_end = s->frame.data[0] + s->frame.linesize[0]*avctx->height;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
126
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
127 if (buf[0] != 0x42) {
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
128 av_log_ask_for_sample(avctx, "unknown record type\n");
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
129 return buf_size;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
130 }
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
131 if (buf[1]) {
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
132 av_log_ask_for_sample(avctx, "padding bytes not supported\n");
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
133 return buf_size;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
134 }
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
135 buf += 4;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
136
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
137 s->x = 0;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
138 do {
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
139 /* if statements are ordered by probability */
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
140 #define OP(buf, pixel, count) \
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
141 op(&dst, dst_end, (buf), buf_end, (pixel), (count), &s->x, avctx->width, s->frame.linesize[0])
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
142
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
143 int type = bytestream_get_byte(&buf);
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
144 count = type & 0x7F;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
145 type >>= 7;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
146 if (count) {
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
147 if (OP(type ? NULL : &buf, -1, count)) break;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
148 } else if (!type) {
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
149 int pixel;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
150 count = bytestream_get_byte(&buf); /* count==0 gives nop */
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
151 pixel = bytestream_get_byte(&buf);
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
152 if (OP(NULL, pixel, count)) break;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
153 } else {
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
154 int pixel;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
155 type = bytestream_get_le16(&buf);
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
156 count = type & 0x3FFF;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
157 type >>= 14;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
158 if (!count) {
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
159 if (type == 0)
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
160 break; // stop
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
161 if (type == 2) {
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
162 av_log_ask_for_sample(avctx, "unknown opcode");
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
163 return AVERROR_INVALIDDATA;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
164 }
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
165 continue;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
166 }
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
167 pixel = type == 3 ? bytestream_get_byte(&buf) : -1;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
168 if (type == 1) count += 0x4000;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
169 if (OP(type == 2 ? &buf : NULL, pixel, count)) break;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
170 }
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
171 } while (buf + 1 < buf_end);
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
172
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
173 *data_size = sizeof(AVFrame);
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
174 *(AVFrame*)data = s->frame;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
175 return buf_size;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
176 }
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
177
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
178 static av_cold int decode_end(AVCodecContext *avctx)
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
179 {
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
180 AnmContext *s = avctx->priv_data;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
181 if (s->frame.data[0])
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
182 avctx->release_buffer(avctx, &s->frame);
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
183 return 0;
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
184 }
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
185
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
186 AVCodec anm_decoder = {
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
187 "anm",
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
188 CODEC_TYPE_VIDEO,
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
189 CODEC_ID_ANM,
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
190 sizeof(AnmContext),
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
191 decode_init,
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
192 NULL,
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
193 decode_end,
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
194 decode_frame,
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
195 CODEC_CAP_DR1,
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
196 .long_name = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
d1115fb7e914 Deluxe Paint Animation decoder
pross
parents:
diff changeset
197 };