Libav

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