00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avcodec.h"
00023 #include "get_bits.h"
00024 #include "dsputil.h"
00025
00026 #define MAX_HUFF_CODES 16
00027
00028 #include "motionpixels_tablegen.h"
00029
00030 typedef struct HuffCode {
00031 int code;
00032 uint8_t size;
00033 uint8_t delta;
00034 } HuffCode;
00035
00036 typedef struct MotionPixelsContext {
00037 AVCodecContext *avctx;
00038 AVFrame frame;
00039 DSPContext dsp;
00040 uint8_t *changes_map;
00041 int offset_bits_len;
00042 int codes_count, current_codes_count;
00043 int max_codes_bits;
00044 HuffCode codes[MAX_HUFF_CODES];
00045 VLC vlc;
00046 YuvPixel *vpt, *hpt;
00047 uint8_t gradient_scale[3];
00048 uint8_t *bswapbuf;
00049 int bswapbuf_size;
00050 } MotionPixelsContext;
00051
00052 static av_cold int mp_decode_init(AVCodecContext *avctx)
00053 {
00054 MotionPixelsContext *mp = avctx->priv_data;
00055
00056 if(avctx->extradata_size < 2){
00057 av_log(avctx, AV_LOG_ERROR, "extradata too small\n");
00058 return AVERROR_INVALIDDATA;
00059 }
00060
00061 motionpixels_tableinit();
00062 mp->avctx = avctx;
00063 dsputil_init(&mp->dsp, avctx);
00064 mp->changes_map = av_mallocz(avctx->width * avctx->height);
00065 mp->offset_bits_len = av_log2(avctx->width * avctx->height) + 1;
00066 mp->vpt = av_mallocz(avctx->height * sizeof(YuvPixel));
00067 mp->hpt = av_mallocz(avctx->height * avctx->width / 16 * sizeof(YuvPixel));
00068 avctx->pix_fmt = PIX_FMT_RGB555;
00069 return 0;
00070 }
00071
00072 static void mp_read_changes_map(MotionPixelsContext *mp, GetBitContext *gb, int count, int bits_len, int read_color)
00073 {
00074 uint16_t *pixels;
00075 int offset, w, h, color = 0, x, y, i;
00076
00077 while (count--) {
00078 offset = get_bits_long(gb, mp->offset_bits_len);
00079 w = get_bits(gb, bits_len) + 1;
00080 h = get_bits(gb, bits_len) + 1;
00081 if (read_color)
00082 color = get_bits(gb, 15);
00083 x = offset % mp->avctx->width;
00084 y = offset / mp->avctx->width;
00085 if (y >= mp->avctx->height)
00086 continue;
00087 w = FFMIN(w, mp->avctx->width - x);
00088 h = FFMIN(h, mp->avctx->height - y);
00089 pixels = (uint16_t *)&mp->frame.data[0][y * mp->frame.linesize[0] + x * 2];
00090 while (h--) {
00091 mp->changes_map[offset] = w;
00092 if (read_color)
00093 for (i = 0; i < w; ++i)
00094 pixels[i] = color;
00095 offset += mp->avctx->width;
00096 pixels += mp->frame.linesize[0] / 2;
00097 }
00098 }
00099 }
00100
00101 static void mp_get_code(MotionPixelsContext *mp, GetBitContext *gb, int size, int code)
00102 {
00103 while (get_bits1(gb)) {
00104 ++size;
00105 if (size > mp->max_codes_bits) {
00106 av_log(mp->avctx, AV_LOG_ERROR, "invalid code size %d/%d\n", size, mp->max_codes_bits);
00107 return;
00108 }
00109 code <<= 1;
00110 mp_get_code(mp, gb, size, code + 1);
00111 }
00112 if (mp->current_codes_count >= MAX_HUFF_CODES) {
00113 av_log(mp->avctx, AV_LOG_ERROR, "too many codes\n");
00114 return;
00115 }
00116 mp->codes[mp->current_codes_count ].code = code;
00117 mp->codes[mp->current_codes_count++].size = size;
00118 }
00119
00120 static void mp_read_codes_table(MotionPixelsContext *mp, GetBitContext *gb)
00121 {
00122 if (mp->codes_count == 1) {
00123 mp->codes[0].delta = get_bits(gb, 4);
00124 } else {
00125 int i;
00126
00127 mp->max_codes_bits = get_bits(gb, 4);
00128 for (i = 0; i < mp->codes_count; ++i)
00129 mp->codes[i].delta = get_bits(gb, 4);
00130 mp->current_codes_count = 0;
00131 mp_get_code(mp, gb, 0, 0);
00132 }
00133 }
00134
00135 static int mp_gradient(MotionPixelsContext *mp, int component, int v)
00136 {
00137 int delta;
00138
00139 delta = (v - 7) * mp->gradient_scale[component];
00140 mp->gradient_scale[component] = (v == 0 || v == 14) ? 2 : 1;
00141 return delta;
00142 }
00143
00144 static YuvPixel mp_get_yuv_from_rgb(MotionPixelsContext *mp, int x, int y)
00145 {
00146 int color;
00147
00148 color = *(uint16_t *)&mp->frame.data[0][y * mp->frame.linesize[0] + x * 2];
00149 return mp_rgb_yuv_table[color];
00150 }
00151
00152 static void mp_set_rgb_from_yuv(MotionPixelsContext *mp, int x, int y, const YuvPixel *p)
00153 {
00154 int color;
00155
00156 color = mp_yuv_to_rgb(p->y, p->v, p->u, 1);
00157 *(uint16_t *)&mp->frame.data[0][y * mp->frame.linesize[0] + x * 2] = color;
00158 }
00159
00160 static int mp_get_vlc(MotionPixelsContext *mp, GetBitContext *gb)
00161 {
00162 int i;
00163
00164 i = (mp->codes_count == 1) ? 0 : get_vlc2(gb, mp->vlc.table, mp->max_codes_bits, 1);
00165 return mp->codes[i].delta;
00166 }
00167
00168 static void mp_decode_line(MotionPixelsContext *mp, GetBitContext *gb, int y)
00169 {
00170 YuvPixel p;
00171 const int y0 = y * mp->avctx->width;
00172 int w, i, x = 0;
00173
00174 p = mp->vpt[y];
00175 if (mp->changes_map[y0 + x] == 0) {
00176 memset(mp->gradient_scale, 1, sizeof(mp->gradient_scale));
00177 ++x;
00178 }
00179 while (x < mp->avctx->width) {
00180 w = mp->changes_map[y0 + x];
00181 if (w != 0) {
00182 if ((y & 3) == 0) {
00183 if (mp->changes_map[y0 + x + mp->avctx->width] < w ||
00184 mp->changes_map[y0 + x + mp->avctx->width * 2] < w ||
00185 mp->changes_map[y0 + x + mp->avctx->width * 3] < w) {
00186 for (i = (x + 3) & ~3; i < x + w; i += 4) {
00187 mp->hpt[((y / 4) * mp->avctx->width + i) / 4] = mp_get_yuv_from_rgb(mp, i, y);
00188 }
00189 }
00190 }
00191 x += w;
00192 memset(mp->gradient_scale, 1, sizeof(mp->gradient_scale));
00193 p = mp_get_yuv_from_rgb(mp, x - 1, y);
00194 } else {
00195 p.y += mp_gradient(mp, 0, mp_get_vlc(mp, gb));
00196 p.y = av_clip(p.y, 0, 31);
00197 if ((x & 3) == 0) {
00198 if ((y & 3) == 0) {
00199 p.v += mp_gradient(mp, 1, mp_get_vlc(mp, gb));
00200 p.v = av_clip(p.v, -32, 31);
00201 p.u += mp_gradient(mp, 2, mp_get_vlc(mp, gb));
00202 p.u = av_clip(p.u, -32, 31);
00203 mp->hpt[((y / 4) * mp->avctx->width + x) / 4] = p;
00204 } else {
00205 p.v = mp->hpt[((y / 4) * mp->avctx->width + x) / 4].v;
00206 p.u = mp->hpt[((y / 4) * mp->avctx->width + x) / 4].u;
00207 }
00208 }
00209 mp_set_rgb_from_yuv(mp, x, y, &p);
00210 ++x;
00211 }
00212 }
00213 }
00214
00215 static void mp_decode_frame_helper(MotionPixelsContext *mp, GetBitContext *gb)
00216 {
00217 YuvPixel p;
00218 int y, y0;
00219
00220 for (y = 0; y < mp->avctx->height; ++y) {
00221 if (mp->changes_map[y * mp->avctx->width] != 0) {
00222 memset(mp->gradient_scale, 1, sizeof(mp->gradient_scale));
00223 p = mp_get_yuv_from_rgb(mp, 0, y);
00224 } else {
00225 p.y += mp_gradient(mp, 0, mp_get_vlc(mp, gb));
00226 p.y = av_clip(p.y, 0, 31);
00227 if ((y & 3) == 0) {
00228 p.v += mp_gradient(mp, 1, mp_get_vlc(mp, gb));
00229 p.v = av_clip(p.v, -32, 31);
00230 p.u += mp_gradient(mp, 2, mp_get_vlc(mp, gb));
00231 p.u = av_clip(p.u, -32, 31);
00232 }
00233 mp->vpt[y] = p;
00234 mp_set_rgb_from_yuv(mp, 0, y, &p);
00235 }
00236 }
00237 for (y0 = 0; y0 < 2; ++y0)
00238 for (y = y0; y < mp->avctx->height; y += 2)
00239 mp_decode_line(mp, gb, y);
00240 }
00241
00242 static int mp_decode_frame(AVCodecContext *avctx,
00243 void *data, int *data_size,
00244 AVPacket *avpkt)
00245 {
00246 const uint8_t *buf = avpkt->data;
00247 int buf_size = avpkt->size;
00248 MotionPixelsContext *mp = avctx->priv_data;
00249 GetBitContext gb;
00250 int i, count1, count2, sz;
00251
00252 mp->frame.reference = 1;
00253 mp->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00254 if (avctx->reget_buffer(avctx, &mp->frame)) {
00255 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00256 return -1;
00257 }
00258
00259
00260 av_fast_malloc(&mp->bswapbuf, &mp->bswapbuf_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
00261 if (!mp->bswapbuf)
00262 return AVERROR(ENOMEM);
00263 mp->dsp.bswap_buf((uint32_t *)mp->bswapbuf, (const uint32_t *)buf, buf_size / 4);
00264 if (buf_size & 3)
00265 memcpy(mp->bswapbuf + (buf_size & ~3), buf + (buf_size & ~3), buf_size & 3);
00266 init_get_bits(&gb, mp->bswapbuf, buf_size * 8);
00267
00268 memset(mp->changes_map, 0, avctx->width * avctx->height);
00269 for (i = !(avctx->extradata[1] & 2); i < 2; ++i) {
00270 count1 = get_bits(&gb, 12);
00271 count2 = get_bits(&gb, 12);
00272 mp_read_changes_map(mp, &gb, count1, 8, i);
00273 mp_read_changes_map(mp, &gb, count2, 4, i);
00274 }
00275
00276 mp->codes_count = get_bits(&gb, 4);
00277 if (mp->codes_count == 0)
00278 goto end;
00279
00280 if (mp->changes_map[0] == 0) {
00281 *(uint16_t *)mp->frame.data[0] = get_bits(&gb, 15);
00282 mp->changes_map[0] = 1;
00283 }
00284 mp_read_codes_table(mp, &gb);
00285
00286 sz = get_bits(&gb, 18);
00287 if (avctx->extradata[0] != 5)
00288 sz += get_bits(&gb, 18);
00289 if (sz == 0)
00290 goto end;
00291
00292 init_vlc(&mp->vlc, mp->max_codes_bits, mp->codes_count, &mp->codes[0].size, sizeof(HuffCode), 1, &mp->codes[0].code, sizeof(HuffCode), 4, 0);
00293 mp_decode_frame_helper(mp, &gb);
00294 free_vlc(&mp->vlc);
00295
00296 end:
00297 *data_size = sizeof(AVFrame);
00298 *(AVFrame *)data = mp->frame;
00299 return buf_size;
00300 }
00301
00302 static av_cold int mp_decode_end(AVCodecContext *avctx)
00303 {
00304 MotionPixelsContext *mp = avctx->priv_data;
00305
00306 av_freep(&mp->changes_map);
00307 av_freep(&mp->vpt);
00308 av_freep(&mp->hpt);
00309 av_freep(&mp->bswapbuf);
00310 if (mp->frame.data[0])
00311 avctx->release_buffer(avctx, &mp->frame);
00312
00313 return 0;
00314 }
00315
00316 AVCodec motionpixels_decoder = {
00317 "motionpixels",
00318 AVMEDIA_TYPE_VIDEO,
00319 CODEC_ID_MOTIONPIXELS,
00320 sizeof(MotionPixelsContext),
00321 mp_decode_init,
00322 NULL,
00323 mp_decode_end,
00324 mp_decode_frame,
00325 CODEC_CAP_DR1,
00326 .long_name = NULL_IF_CONFIG_SMALL("Motion Pixels video"),
00327 };