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