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