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