• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/motionpixels.c

Go to the documentation of this file.
00001 /*
00002  * Motion Pixels Video Decoder
00003  * Copyright (c) 2008 Gregory Montoir (cyx@users.sourceforge.net)
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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             p.y = av_clip(p.y, 0, 31);
00243             if ((x & 3) == 0) {
00244                 if ((y & 3) == 0) {
00245                     p.v += mp_gradient(mp, 1, mp_get_vlc(mp, gb));
00246                     p.v = av_clip(p.v, -32, 31);
00247                     p.u += mp_gradient(mp, 2, mp_get_vlc(mp, gb));
00248                     p.u = av_clip(p.u, -32, 31);
00249                     mp->hpt[((y / 4) * mp->avctx->width + x) / 4] = p;
00250                 } else {
00251                     p.v = mp->hpt[((y / 4) * mp->avctx->width + x) / 4].v;
00252                     p.u = mp->hpt[((y / 4) * mp->avctx->width + x) / 4].u;
00253                 }
00254             }
00255             mp_set_rgb_from_yuv(mp, x, y, &p);
00256             ++x;
00257         }
00258     }
00259 }
00260 
00261 static void mp_decode_frame_helper(MotionPixelsContext *mp, GetBitContext *gb)
00262 {
00263     YuvPixel p;
00264     int y, y0;
00265 
00266     for (y = 0; y < mp->avctx->height; ++y) {
00267         if (mp->changes_map[y * mp->avctx->width] != 0) {
00268             memset(mp->gradient_scale, 1, sizeof(mp->gradient_scale));
00269             p = mp_get_yuv_from_rgb(mp, 0, y);
00270         } else {
00271             p.y += mp_gradient(mp, 0, mp_get_vlc(mp, gb));
00272             p.y = av_clip(p.y, 0, 31);
00273             if ((y & 3) == 0) {
00274                 p.v += mp_gradient(mp, 1, mp_get_vlc(mp, gb));
00275                 p.v = av_clip(p.v, -32, 31);
00276                 p.u += mp_gradient(mp, 2, mp_get_vlc(mp, gb));
00277                 p.u = av_clip(p.u, -32, 31);
00278             }
00279             mp->vpt[y] = p;
00280             mp_set_rgb_from_yuv(mp, 0, y, &p);
00281         }
00282     }
00283     for (y0 = 0; y0 < 2; ++y0)
00284         for (y = y0; y < mp->avctx->height; y += 2)
00285             mp_decode_line(mp, gb, y);
00286 }
00287 
00288 static int mp_decode_frame(AVCodecContext *avctx,
00289                                  void *data, int *data_size,
00290                                  const uint8_t *buf, int buf_size)
00291 {
00292     MotionPixelsContext *mp = avctx->priv_data;
00293     GetBitContext gb;
00294     int i, count1, count2, sz;
00295 
00296     mp->frame.reference = 1;
00297     mp->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00298     if (avctx->reget_buffer(avctx, &mp->frame)) {
00299         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00300         return -1;
00301     }
00302 
00303     /* le32 bitstream msb first */
00304     mp->bswapbuf = av_fast_realloc(mp->bswapbuf, &mp->bswapbuf_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
00305     mp->dsp.bswap_buf((uint32_t *)mp->bswapbuf, (const uint32_t *)buf, buf_size / 4);
00306     if (buf_size & 3)
00307         memcpy(mp->bswapbuf + (buf_size & ~3), buf + (buf_size & ~3), buf_size & 3);
00308     init_get_bits(&gb, mp->bswapbuf, buf_size * 8);
00309 
00310     memset(mp->changes_map, 0, avctx->width * avctx->height);
00311     for (i = !(avctx->extradata[1] & 2); i < 2; ++i) {
00312         count1 = get_bits(&gb, 12);
00313         count2 = get_bits(&gb, 12);
00314         mp_read_changes_map(mp, &gb, count1, 8, i);
00315         mp_read_changes_map(mp, &gb, count2, 4, i);
00316     }
00317 
00318     mp->codes_count = get_bits(&gb, 4);
00319     if (mp->codes_count == 0)
00320         goto end;
00321 
00322     if (mp->changes_map[0] == 0) {
00323         *(uint16_t *)mp->frame.data[0] = get_bits(&gb, 15);
00324         mp->changes_map[0] = 1;
00325     }
00326     mp_read_codes_table(mp, &gb);
00327 
00328     sz = get_bits(&gb, 18);
00329     if (avctx->extradata[0] != 5)
00330         sz += get_bits(&gb, 18);
00331     if (sz == 0)
00332         goto end;
00333 
00334     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);
00335     mp_decode_frame_helper(mp, &gb);
00336     free_vlc(&mp->vlc);
00337 
00338 end:
00339     *data_size = sizeof(AVFrame);
00340     *(AVFrame *)data = mp->frame;
00341     return buf_size;
00342 }
00343 
00344 static av_cold int mp_decode_end(AVCodecContext *avctx)
00345 {
00346     MotionPixelsContext *mp = avctx->priv_data;
00347 
00348     av_freep(&mp->changes_map);
00349     av_freep(&mp->vpt);
00350     av_freep(&mp->hpt);
00351     av_freep(&mp->bswapbuf);
00352     if (mp->frame.data[0])
00353         avctx->release_buffer(avctx, &mp->frame);
00354 
00355     return 0;
00356 }
00357 
00358 AVCodec motionpixels_decoder = {
00359     "motionpixels",
00360     CODEC_TYPE_VIDEO,
00361     CODEC_ID_MOTIONPIXELS,
00362     sizeof(MotionPixelsContext),
00363     mp_decode_init,
00364     NULL,
00365     mp_decode_end,
00366     mp_decode_frame,
00367     CODEC_CAP_DR1,
00368     .long_name = NULL_IF_CONFIG_SMALL("Motion Pixels video"),
00369 };

Generated on Sat Feb 16 2013 09:23:12 for ffmpeg by  doxygen 1.7.1