Update avcodec to 20080825

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@27548 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
David McPaul 2008-09-15 14:04:00 +00:00
parent 66ba2a5263
commit c6ecd9c8dc
8 changed files with 1604 additions and 83 deletions

View File

@ -0,0 +1,402 @@
/*
* G.726 ADPCM audio codec
* Copyright (c) 2004 Roman Shaposhnik.
*
* This is a very straightforward rendition of the G.726
* Section 4 "Computational Details".
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <limits.h>
#include "avcodec.h"
#include "bitstream.h"
/**
* G.726 11bit float.
* G.726 Standard uses rather odd 11bit floating point arithmentic for
* numerous occasions. It's a mistery to me why they did it this way
* instead of simply using 32bit integer arithmetic.
*/
typedef struct Float11 {
uint8_t sign; /**< 1bit sign */
uint8_t exp; /**< 4bit exponent */
uint8_t mant; /**< 6bit mantissa */
} Float11;
static inline Float11* i2f(int i, Float11* f)
{
f->sign = (i < 0);
if (f->sign)
i = -i;
f->exp = av_log2_16bit(i) + !!i;
f->mant = i? (i<<6) >> f->exp : 1<<5;
return f;
}
static inline int16_t mult(Float11* f1, Float11* f2)
{
int res, exp;
exp = f1->exp + f2->exp;
res = (((f1->mant * f2->mant) + 0x30) >> 4);
res = exp > 19 ? res << (exp - 19) : res >> (19 - exp);
return (f1->sign ^ f2->sign) ? -res : res;
}
static inline int sgn(int value)
{
return (value < 0) ? -1 : 1;
}
typedef struct G726Tables {
const int* quant; /**< quantization table */
const int16_t* iquant; /**< inverse quantization table */
const int16_t* W; /**< special table #1 ;-) */
const uint8_t* F; /**< special table #2 */
} G726Tables;
typedef struct G726Context {
G726Tables tbls; /**< static tables needed for computation */
Float11 sr[2]; /**< prev. reconstructed samples */
Float11 dq[6]; /**< prev. difference */
int a[2]; /**< second order predictor coeffs */
int b[6]; /**< sixth order predictor coeffs */
int pk[2]; /**< signs of prev. 2 sez + dq */
int ap; /**< scale factor control */
int yu; /**< fast scale factor */
int yl; /**< slow scale factor */
int dms; /**< short average magnitude of F[i] */
int dml; /**< long average magnitude of F[i] */
int td; /**< tone detect */
int se; /**< estimated signal for the next iteration */
int sez; /**< estimated second order prediction */
int y; /**< quantizer scaling factor for the next iteration */
int code_size;
} G726Context;
static const int quant_tbl16[] = /**< 16kbit/s 2bits per sample */
{ 260, INT_MAX };
static const int16_t iquant_tbl16[] =
{ 116, 365, 365, 116 };
static const int16_t W_tbl16[] =
{ -22, 439, 439, -22 };
static const uint8_t F_tbl16[] =
{ 0, 7, 7, 0 };
static const int quant_tbl24[] = /**< 24kbit/s 3bits per sample */
{ 7, 217, 330, INT_MAX };
static const int16_t iquant_tbl24[] =
{ INT16_MIN, 135, 273, 373, 373, 273, 135, INT16_MIN };
static const int16_t W_tbl24[] =
{ -4, 30, 137, 582, 582, 137, 30, -4 };
static const uint8_t F_tbl24[] =
{ 0, 1, 2, 7, 7, 2, 1, 0 };
static const int quant_tbl32[] = /**< 32kbit/s 4bits per sample */
{ -125, 79, 177, 245, 299, 348, 399, INT_MAX };
static const int16_t iquant_tbl32[] =
{ INT16_MIN, 4, 135, 213, 273, 323, 373, 425,
425, 373, 323, 273, 213, 135, 4, INT16_MIN };
static const int16_t W_tbl32[] =
{ -12, 18, 41, 64, 112, 198, 355, 1122,
1122, 355, 198, 112, 64, 41, 18, -12};
static const uint8_t F_tbl32[] =
{ 0, 0, 0, 1, 1, 1, 3, 7, 7, 3, 1, 1, 1, 0, 0, 0 };
static const int quant_tbl40[] = /**< 40kbit/s 5bits per sample */
{ -122, -16, 67, 138, 197, 249, 297, 338,
377, 412, 444, 474, 501, 527, 552, INT_MAX };
static const int16_t iquant_tbl40[] =
{ INT16_MIN, -66, 28, 104, 169, 224, 274, 318,
358, 395, 429, 459, 488, 514, 539, 566,
566, 539, 514, 488, 459, 429, 395, 358,
318, 274, 224, 169, 104, 28, -66, INT16_MIN };
static const int16_t W_tbl40[] =
{ 14, 14, 24, 39, 40, 41, 58, 100,
141, 179, 219, 280, 358, 440, 529, 696,
696, 529, 440, 358, 280, 219, 179, 141,
100, 58, 41, 40, 39, 24, 14, 14 };
static const uint8_t F_tbl40[] =
{ 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 6,
6, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
static const G726Tables G726Tables_pool[] =
{{ quant_tbl16, iquant_tbl16, W_tbl16, F_tbl16 },
{ quant_tbl24, iquant_tbl24, W_tbl24, F_tbl24 },
{ quant_tbl32, iquant_tbl32, W_tbl32, F_tbl32 },
{ quant_tbl40, iquant_tbl40, W_tbl40, F_tbl40 }};
/**
* Para 4.2.2 page 18: Adaptive quantizer.
*/
static inline uint8_t quant(G726Context* c, int d)
{
int sign, exp, i, dln;
sign = i = 0;
if (d < 0) {
sign = 1;
d = -d;
}
exp = av_log2_16bit(d);
dln = ((exp<<7) + (((d<<7)>>exp)&0x7f)) - (c->y>>2);
while (c->tbls.quant[i] < INT_MAX && c->tbls.quant[i] < dln)
++i;
if (sign)
i = ~i;
if (c->code_size != 2 && i == 0) /* I'm not sure this is a good idea */
i = 0xff;
return i;
}
/**
* Para 4.2.3 page 22: Inverse adaptive quantizer.
*/
static inline int16_t inverse_quant(G726Context* c, int i)
{
int dql, dex, dqt;
dql = c->tbls.iquant[i] + (c->y >> 2);
dex = (dql>>7) & 0xf; /* 4bit exponent */
dqt = (1<<7) + (dql & 0x7f); /* log2 -> linear */
return (dql < 0) ? 0 : ((dqt<<dex) >> 7);
}
static int16_t g726_decode(G726Context* c, int I)
{
int dq, re_signal, pk0, fa1, i, tr, ylint, ylfrac, thr2, al, dq0;
Float11 f;
int I_sig= I >> (c->code_size - 1);
dq = inverse_quant(c, I);
/* Transition detect */
ylint = (c->yl >> 15);
ylfrac = (c->yl >> 10) & 0x1f;
thr2 = (ylint > 9) ? 0x1f << 10 : (0x20 + ylfrac) << ylint;
tr= (c->td == 1 && dq > ((3*thr2)>>2));
if (I_sig) /* get the sign */
dq = -dq;
re_signal = c->se + dq;
/* Update second order predictor coefficient A2 and A1 */
pk0 = (c->sez + dq) ? sgn(c->sez + dq) : 0;
dq0 = dq ? sgn(dq) : 0;
if (tr) {
c->a[0] = 0;
c->a[1] = 0;
for (i=0; i<6; i++)
c->b[i] = 0;
} else {
/* This is a bit crazy, but it really is +255 not +256 */
fa1 = av_clip((-c->a[0]*c->pk[0]*pk0)>>5, -256, 255);
c->a[1] += 128*pk0*c->pk[1] + fa1 - (c->a[1]>>7);
c->a[1] = av_clip(c->a[1], -12288, 12288);
c->a[0] += 64*3*pk0*c->pk[0] - (c->a[0] >> 8);
c->a[0] = av_clip(c->a[0], -(15360 - c->a[1]), 15360 - c->a[1]);
for (i=0; i<6; i++)
c->b[i] += 128*dq0*sgn(-c->dq[i].sign) - (c->b[i]>>8);
}
/* Update Dq and Sr and Pk */
c->pk[1] = c->pk[0];
c->pk[0] = pk0 ? pk0 : 1;
c->sr[1] = c->sr[0];
i2f(re_signal, &c->sr[0]);
for (i=5; i>0; i--)
c->dq[i] = c->dq[i-1];
i2f(dq, &c->dq[0]);
c->dq[0].sign = I_sig; /* Isn't it crazy ?!?! */
c->td = c->a[1] < -11776;
/* Update Ap */
c->dms += (c->tbls.F[I]<<4) + ((- c->dms) >> 5);
c->dml += (c->tbls.F[I]<<4) + ((- c->dml) >> 7);
if (tr)
c->ap = 256;
else {
c->ap += (-c->ap) >> 4;
if (c->y <= 1535 || c->td || abs((c->dms << 2) - c->dml) >= (c->dml >> 3))
c->ap += 0x20;
}
/* Update Yu and Yl */
c->yu = av_clip(c->y + c->tbls.W[I] + ((-c->y)>>5), 544, 5120);
c->yl += c->yu + ((-c->yl)>>6);
/* Next iteration for Y */
al = (c->ap >= 256) ? 1<<6 : c->ap >> 2;
c->y = (c->yl + (c->yu - (c->yl>>6))*al) >> 6;
/* Next iteration for SE and SEZ */
c->se = 0;
for (i=0; i<6; i++)
c->se += mult(i2f(c->b[i] >> 2, &f), &c->dq[i]);
c->sez = c->se >> 1;
for (i=0; i<2; i++)
c->se += mult(i2f(c->a[i] >> 2, &f), &c->sr[i]);
c->se >>= 1;
return av_clip(re_signal << 2, -0xffff, 0xffff);
}
static av_cold int g726_reset(G726Context* c, int index)
{
int i;
c->tbls = G726Tables_pool[index];
for (i=0; i<2; i++) {
c->sr[i].mant = 1<<5;
c->pk[i] = 1;
}
for (i=0; i<6; i++) {
c->dq[i].mant = 1<<5;
}
c->yu = 544;
c->yl = 34816;
c->y = 544;
return 0;
}
#ifdef CONFIG_ENCODERS
static int16_t g726_encode(G726Context* c, int16_t sig)
{
uint8_t i;
i = quant(c, sig/4 - c->se) & ((1<<c->code_size) - 1);
g726_decode(c, i);
return i;
}
#endif
/* Interfacing to the libavcodec */
static av_cold int g726_init(AVCodecContext * avctx)
{
G726Context* c = avctx->priv_data;
unsigned int index= (avctx->bit_rate + avctx->sample_rate/2) / avctx->sample_rate - 2;
if (avctx->bit_rate % avctx->sample_rate && avctx->codec->encode) {
av_log(avctx, AV_LOG_ERROR, "Bitrate - Samplerate combination is invalid\n");
return -1;
}
if(avctx->channels != 1){
av_log(avctx, AV_LOG_ERROR, "Only mono is supported\n");
return -1;
}
if(index>3){
av_log(avctx, AV_LOG_ERROR, "Unsupported number of bits %d\n", index+2);
return -1;
}
g726_reset(c, index);
c->code_size = index+2;
avctx->coded_frame = avcodec_alloc_frame();
if (!avctx->coded_frame)
return AVERROR(ENOMEM);
avctx->coded_frame->key_frame = 1;
if (avctx->codec->decode)
avctx->sample_fmt = SAMPLE_FMT_S16;
return 0;
}
static av_cold int g726_close(AVCodecContext *avctx)
{
av_freep(&avctx->coded_frame);
return 0;
}
#ifdef CONFIG_ENCODERS
static int g726_encode_frame(AVCodecContext *avctx,
uint8_t *dst, int buf_size, void *data)
{
G726Context *c = avctx->priv_data;
short *samples = data;
PutBitContext pb;
init_put_bits(&pb, dst, 1024*1024);
for (; buf_size; buf_size--)
put_bits(&pb, c->code_size, g726_encode(c, *samples++));
flush_put_bits(&pb);
return put_bits_count(&pb)>>3;
}
#endif
static int g726_decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
const uint8_t *buf, int buf_size)
{
G726Context *c = avctx->priv_data;
short *samples = data;
GetBitContext gb;
init_get_bits(&gb, buf, buf_size * 8);
while (get_bits_count(&gb) + c->code_size <= buf_size*8)
*samples++ = g726_decode(c, get_bits(&gb, c->code_size));
if(buf_size*8 != get_bits_count(&gb))
av_log(avctx, AV_LOG_ERROR, "Frame invalidly split, missing parser?\n");
*data_size = (uint8_t*)samples - (uint8_t*)data;
return buf_size;
}
#ifdef CONFIG_ENCODERS
AVCodec adpcm_g726_encoder = {
"g726",
CODEC_TYPE_AUDIO,
CODEC_ID_ADPCM_G726,
sizeof(G726Context),
g726_init,
g726_encode_frame,
g726_close,
NULL,
.sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
.long_name = NULL_IF_CONFIG_SMALL("G.726 ADPCM"),
};
#endif //CONFIG_ENCODERS
AVCodec adpcm_g726_decoder = {
"g726",
CODEC_TYPE_AUDIO,
CODEC_ID_ADPCM_G726,
sizeof(G726Context),
g726_init,
NULL,
g726_close,
g726_decode_frame,
.long_name = NULL_IF_CONFIG_SMALL("G.726 ADPCM"),
};

View File

@ -0,0 +1,29 @@
/*
* G.729 decoder
* Copyright (c) 2008 Vladimir Voroshilov
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef FFMPEG_G729_H
#define FFMPEG_G729_H
/**
* maximum possible subframe size
*/
#define MAX_SUBFRAME_SIZE 44
#endif // FFMPEG_G729_H

View File

@ -0,0 +1,206 @@
/*
* data for G.729 decoder
* Copyright (c) 2007 Vladimir Voroshilov
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef FFMPEG_G729DATA_H
#define FFMPEG_G729DATA_H
#include <stdint.h>
/// Moving Average (MA) prediction order
#define MA_NP 4
/**
* first stage LSP codebook
* (10-dimensional, with 128 entries (3.24 of G.729)
*/
static const int16_t cb_lsp_1st[1<<VQ_1ST_BITS][10] =
{ /* (2.13) */
{ 1486, 2168, 3751, 9074, 12134, 13944, 17983, 19173, 21190, 21820},
{ 1730, 2640, 3450, 4870, 6126, 7876, 15644, 17817, 20294, 21902},
{ 1568, 2256, 3088, 4874, 11063, 13393, 18307, 19293, 21109, 21741},
{ 1733, 2512, 3357, 4708, 6977, 10296, 17024, 17956, 19145, 20350},
{ 1744, 2436, 3308, 8731, 10432, 12007, 15614, 16639, 21359, 21913},
{ 1786, 2369, 3372, 4521, 6795, 12963, 17674, 18988, 20855, 21640},
{ 1631, 2433, 3361, 6328, 10709, 12013, 13277, 13904, 19441, 21088},
{ 1489, 2364, 3291, 6250, 9227, 10403, 13843, 15278, 17721, 21451},
{ 1869, 2533, 3475, 4365, 9152, 14513, 15908, 17022, 20611, 21411},
{ 2070, 3025, 4333, 5854, 7805, 9231, 10597, 16047, 20109, 21834},
{ 1910, 2673, 3419, 4261, 11168, 15111, 16577, 17591, 19310, 20265},
{ 1141, 1815, 2624, 4623, 6495, 9588, 13968, 16428, 19351, 21286},
{ 2192, 3171, 4707, 5808, 10904, 12500, 14162, 15664, 21124, 21789},
{ 1286, 1907, 2548, 3453, 9574, 11964, 15978, 17344, 19691, 22495},
{ 1921, 2720, 4604, 6684, 11503, 12992, 14350, 15262, 16997, 20791},
{ 2052, 2759, 3897, 5246, 6638, 10267, 15834, 16814, 18149, 21675},
{ 1798, 2497, 5617, 11449, 13189, 14711, 17050, 18195, 20307, 21182},
{ 1009, 1647, 2889, 5709, 9541, 12354, 15231, 18494, 20966, 22033},
{ 3016, 3794, 5406, 7469, 12488, 13984, 15328, 16334, 19952, 20791},
{ 2203, 3040, 3796, 5442, 11987, 13512, 14931, 16370, 17856, 18803},
{ 2912, 4292, 7988, 9572, 11562, 13244, 14556, 16529, 20004, 21073},
{ 2861, 3607, 5923, 7034, 9234, 12054, 13729, 18056, 20262, 20974},
{ 3069, 4311, 5967, 7367, 11482, 12699, 14309, 16233, 18333, 19172},
{ 2434, 3661, 4866, 5798, 10383, 11722, 13049, 15668, 18862, 19831},
{ 2020, 2605, 3860, 9241, 13275, 14644, 16010, 17099, 19268, 20251},
{ 1877, 2809, 3590, 4707, 11056, 12441, 15622, 17168, 18761, 19907},
{ 2107, 2873, 3673, 5799, 13579, 14687, 15938, 17077, 18890, 19831},
{ 1612, 2284, 2944, 3572, 8219, 13959, 15924, 17239, 18592, 20117},
{ 2420, 3156, 6542, 10215, 12061, 13534, 15305, 16452, 18717, 19880},
{ 1667, 2612, 3534, 5237, 10513, 11696, 12940, 16798, 18058, 19378},
{ 2388, 3017, 4839, 9333, 11413, 12730, 15024, 16248, 17449, 18677},
{ 1875, 2786, 4231, 6320, 8694, 10149, 11785, 17013, 18608, 19960},
{ 679, 1411, 4654, 8006, 11446, 13249, 15763, 18127, 20361, 21567},
{ 1838, 2596, 3578, 4608, 5650, 11274, 14355, 15886, 20579, 21754},
{ 1303, 1955, 2395, 3322, 12023, 13764, 15883, 18077, 20180, 21232},
{ 1438, 2102, 2663, 3462, 8328, 10362, 13763, 17248, 19732, 22344},
{ 860, 1904, 6098, 7775, 9815, 12007, 14821, 16709, 19787, 21132},
{ 1673, 2723, 3704, 6125, 7668, 9447, 13683, 14443, 20538, 21731},
{ 1246, 1849, 2902, 4508, 7221, 12710, 14835, 16314, 19335, 22720},
{ 1525, 2260, 3862, 5659, 7342, 11748, 13370, 14442, 18044, 21334},
{ 1196, 1846, 3104, 7063, 10972, 12905, 14814, 17037, 19922, 22636},
{ 2147, 3106, 4475, 6511, 8227, 9765, 10984, 12161, 18971, 21300},
{ 1585, 2405, 2994, 4036, 11481, 13177, 14519, 15431, 19967, 21275},
{ 1778, 2688, 3614, 4680, 9465, 11064, 12473, 16320, 19742, 20800},
{ 1862, 2586, 3492, 6719, 11708, 13012, 14364, 16128, 19610, 20425},
{ 1395, 2156, 2669, 3386, 10607, 12125, 13614, 16705, 18976, 21367},
{ 1444, 2117, 3286, 6233, 9423, 12981, 14998, 15853, 17188, 21857},
{ 2004, 2895, 3783, 4897, 6168, 7297, 12609, 16445, 19297, 21465},
{ 1495, 2863, 6360, 8100, 11399, 14271, 15902, 17711, 20479, 22061},
{ 2484, 3114, 5718, 7097, 8400, 12616, 14073, 14847, 20535, 21396},
{ 2424, 3277, 5296, 6284, 11290, 12903, 16022, 17508, 19333, 20283},
{ 2565, 3778, 5360, 6989, 8782, 10428, 14390, 15742, 17770, 21734},
{ 2727, 3384, 6613, 9254, 10542, 12236, 14651, 15687, 20074, 21102},
{ 1916, 2953, 6274, 8088, 9710, 10925, 12392, 16434, 20010, 21183},
{ 3384, 4366, 5349, 7667, 11180, 12605, 13921, 15324, 19901, 20754},
{ 3075, 4283, 5951, 7619, 9604, 11010, 12384, 14006, 20658, 21497},
{ 1751, 2455, 5147, 9966, 11621, 13176, 14739, 16470, 20788, 21756},
{ 1442, 2188, 3330, 6813, 8929, 12135, 14476, 15306, 19635, 20544},
{ 2294, 2895, 4070, 8035, 12233, 13416, 14762, 17367, 18952, 19688},
{ 1937, 2659, 4602, 6697, 9071, 12863, 14197, 15230, 16047, 18877},
{ 2071, 2663, 4216, 9445, 10887, 12292, 13949, 14909, 19236, 20341},
{ 1740, 2491, 3488, 8138, 9656, 11153, 13206, 14688, 20896, 21907},
{ 2199, 2881, 4675, 8527, 10051, 11408, 14435, 15463, 17190, 20597},
{ 1943, 2988, 4177, 6039, 7478, 8536, 14181, 15551, 17622, 21579},
{ 1825, 3175, 7062, 9818, 12824, 15450, 18330, 19856, 21830, 22412},
{ 2464, 3046, 4822, 5977, 7696, 15398, 16730, 17646, 20588, 21320},
{ 2550, 3393, 5305, 6920, 10235, 14083, 18143, 19195, 20681, 21336},
{ 3003, 3799, 5321, 6437, 7919, 11643, 15810, 16846, 18119, 18980},
{ 3455, 4157, 6838, 8199, 9877, 12314, 15905, 16826, 19949, 20892},
{ 3052, 3769, 4891, 5810, 6977, 10126, 14788, 15990, 19773, 20904},
{ 3671, 4356, 5827, 6997, 8460, 12084, 14154, 14939, 19247, 20423},
{ 2716, 3684, 5246, 6686, 8463, 10001, 12394, 14131, 16150, 19776},
{ 1945, 2638, 4130, 7995, 14338, 15576, 17057, 18206, 20225, 20997},
{ 2304, 2928, 4122, 4824, 5640, 13139, 15825, 16938, 20108, 21054},
{ 1800, 2516, 3350, 5219, 13406, 15948, 17618, 18540, 20531, 21252},
{ 1436, 2224, 2753, 4546, 9657, 11245, 15177, 16317, 17489, 19135},
{ 2319, 2899, 4980, 6936, 8404, 13489, 15554, 16281, 20270, 20911},
{ 2187, 2919, 4610, 5875, 7390, 12556, 14033, 16794, 20998, 21769},
{ 2235, 2923, 5121, 6259, 8099, 13589, 15340, 16340, 17927, 20159},
{ 1765, 2638, 3751, 5730, 7883, 10108, 13633, 15419, 16808, 18574},
{ 3460, 5741, 9596, 11742, 14413, 16080, 18173, 19090, 20845, 21601},
{ 3735, 4426, 6199, 7363, 9250, 14489, 16035, 17026, 19873, 20876},
{ 3521, 4778, 6887, 8680, 12717, 14322, 15950, 18050, 20166, 21145},
{ 2141, 2968, 6865, 8051, 10010, 13159, 14813, 15861, 17528, 18655},
{ 4148, 6128, 9028, 10871, 12686, 14005, 15976, 17208, 19587, 20595},
{ 4403, 5367, 6634, 8371, 10163, 11599, 14963, 16331, 17982, 18768},
{ 4091, 5386, 6852, 8770, 11563, 13290, 15728, 16930, 19056, 20102},
{ 2746, 3625, 5299, 7504, 10262, 11432, 13172, 15490, 16875, 17514},
{ 2248, 3556, 8539, 10590, 12665, 14696, 16515, 17824, 20268, 21247},
{ 1279, 1960, 3920, 7793, 10153, 14753, 16646, 18139, 20679, 21466},
{ 2440, 3475, 6737, 8654, 12190, 14588, 17119, 17925, 19110, 19979},
{ 1879, 2514, 4497, 7572, 10017, 14948, 16141, 16897, 18397, 19376},
{ 2804, 3688, 7490, 10086, 11218, 12711, 16307, 17470, 20077, 21126},
{ 2023, 2682, 3873, 8268, 10255, 11645, 15187, 17102, 18965, 19788},
{ 2823, 3605, 5815, 8595, 10085, 11469, 16568, 17462, 18754, 19876},
{ 2851, 3681, 5280, 7648, 9173, 10338, 14961, 16148, 17559, 18474},
{ 1348, 2645, 5826, 8785, 10620, 12831, 16255, 18319, 21133, 22586},
{ 2141, 3036, 4293, 6082, 7593, 10629, 17158, 18033, 21466, 22084},
{ 1608, 2375, 3384, 6878, 9970, 11227, 16928, 17650, 20185, 21120},
{ 2774, 3616, 5014, 6557, 7788, 8959, 17068, 18302, 19537, 20542},
{ 1934, 4813, 6204, 7212, 8979, 11665, 15989, 17811, 20426, 21703},
{ 2288, 3507, 5037, 6841, 8278, 9638, 15066, 16481, 21653, 22214},
{ 2951, 3771, 4878, 7578, 9016, 10298, 14490, 15242, 20223, 20990},
{ 3256, 4791, 6601, 7521, 8644, 9707, 13398, 16078, 19102, 20249},
{ 1827, 2614, 3486, 6039, 12149, 13823, 16191, 17282, 21423, 22041},
{ 1000, 1704, 3002, 6335, 8471, 10500, 14878, 16979, 20026, 22427},
{ 1646, 2286, 3109, 7245, 11493, 12791, 16824, 17667, 18981, 20222},
{ 1708, 2501, 3315, 6737, 8729, 9924, 16089, 17097, 18374, 19917},
{ 2623, 3510, 4478, 5645, 9862, 11115, 15219, 18067, 19583, 20382},
{ 2518, 3434, 4728, 6388, 8082, 9285, 13162, 18383, 19819, 20552},
{ 1726, 2383, 4090, 6303, 7805, 12845, 14612, 17608, 19269, 20181},
{ 2860, 3735, 4838, 6044, 7254, 8402, 14031, 16381, 18037, 19410},
{ 4247, 5993, 7952, 9792, 12342, 14653, 17527, 18774, 20831, 21699},
{ 3502, 4051, 5680, 6805, 8146, 11945, 16649, 17444, 20390, 21564},
{ 3151, 4893, 5899, 7198, 11418, 13073, 15124, 17673, 20520, 21861},
{ 3960, 4848, 5926, 7259, 8811, 10529, 15661, 16560, 18196, 20183},
{ 4499, 6604, 8036, 9251, 10804, 12627, 15880, 17512, 20020, 21046},
{ 4251, 5541, 6654, 8318, 9900, 11686, 15100, 17093, 20572, 21687},
{ 3769, 5327, 7865, 9360, 10684, 11818, 13660, 15366, 18733, 19882},
{ 3083, 3969, 6248, 8121, 9798, 10994, 12393, 13686, 17888, 19105},
{ 2731, 4670, 7063, 9201, 11346, 13735, 16875, 18797, 20787, 22360},
{ 1187, 2227, 4737, 7214, 9622, 12633, 15404, 17968, 20262, 23533},
{ 1911, 2477, 3915, 10098, 11616, 12955, 16223, 17138, 19270, 20729},
{ 1764, 2519, 3887, 6944, 9150, 12590, 16258, 16984, 17924, 18435},
{ 1400, 3674, 7131, 8718, 10688, 12508, 15708, 17711, 19720, 21068},
{ 2322, 3073, 4287, 8108, 9407, 10628, 15862, 16693, 19714, 21474},
{ 2630, 3339, 4758, 8360, 10274, 11333, 12880, 17374, 19221, 19936},
{ 1721, 2577, 5553, 7195, 8651, 10686, 15069, 16953, 18703, 19929}
};
/**
* second stage LSP codebook, high and low parts
(both 5-dimensional, with 32 entries (3.2.4 of G.729)
*/
static const int16_t cb_lsp_2nd[1<<VQ_2ND_BITS][10] =
{ /* (2.13) */
{ -435, -815, -742, 1033, -518, 582, -1201, 829, 86, 385},
{ -833, -891, 463, -8, -1251, 1450, 72, -231, 864, 661},
{-1021, 231, -306, 321, -220, -163, -526, -754, -1633, 267},
{ 57, -198, -339, -33, -1468, 573, 796, -169, -631, 816},
{ 171, -350, 294, 1660, 453, 519, 291, 159, -640, -1296},
{ -701, -842, -58, 950, 892, 1549, 715, 527, -714, -193},
{ 584, 31, -289, 356, -333, -457, 612, -283, -1381, -741},
{ -109, -808, 231, 77, -87, -344, 1341, 1087, -654, -569},
{ -859, 1236, 550, 854, 714, -543, -1752, -195, -98, -276},
{ -877, -954, -1248, -299, 212, -235, -728, 949, 1517, 895},
{ -77, 344, -620, 763, 413, 502, -362, -960, -483, 1386},
{ -314, -307, -256, -1260, -429, 450, -466, -108, 1010, 2223},
{ 711, 693, 521, 650, 1305, -28, -378, 744, -1005, 240},
{ -112, -271, -500, 946, 1733, 271, -15, 909, -259, 1688},
{ 575, -10, -468, -199, 1101, -1011, 581, -53, -747, 878},
{ 145, -285, -1280, -398, 36, -498, -1377, 18, -444, 1483},
{-1133, -835, 1350, 1284, -95, 1015, -222, 443, 372, -354},
{-1459, -1237, 416, -213, 466, 669, 659, 1640, 932, 534},
{ -15, 66, 468, 1019, -748, 1385, -182, -907, -721, -262},
{ -338, 148, 1445, 75, -760, 569, 1247, 337, 416, -121},
{ 389, 239, 1568, 981, 113, 369, -1003, -507, -587, -904},
{ -312, -98, 949, 31, 1104, 72, -141, 1465, 63, -785},
{ 1127, 584, 835, 277, -1159, 208, 301, -882, 117, -404},
{ 539, -114, 856, -493, 223, -912, 623, -76, 276, -440},
{ 2197, 2337, 1268, 670, 304, -267, -525, 140, 882, -139},
{-1596, 550, 801, -456, -56, -697, 865, 1060, 413, 446},
{ 1154, 593, -77, 1237, -31, 581, -1037, -895, 669, 297},
{ 397, 558, 203, -797, -919, 3, 692, -292, 1050, 782},
{ 334, 1475, 632, -80, 48, -1061, -484, 362, -597, -852},
{ -545, -330, -429, -680, 1133, -1182, -744, 1340, 262, 63},
{ 1320, 827, -398, -576, 341, -774, -483, -1247, -70, 98},
{ -163, 674, -11, -886, 531, -1125, -265, -242, 724, 934}
};
#endif // FFMPEG_G729DATA_H

View File

@ -0,0 +1,70 @@
/*
* G.729 decoder
* Copyright (c) 2008 Vladimir Voroshilov
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <inttypes.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include "avcodec.h"
#include "libavutil/avutil.h"
#include "bitstream.h"
/**
* minimum quantized LSF value (3.2.4)
* 0.005 in Q13
*/
#define LSFQ_MIN 40
/**
* maximum quantized LSF value (3.2.4)
* 3.135 in Q13
*/
#define LSFQ_MAX 25681
/**
* minimum LSF distance (3.2.4)
* 0.0391 in Q13
*/
#define LSFQ_DIFF_MIN 321
/**
* \brief pseudo random number generator
*/
static inline uint16_t g729_random(uint16_t value)
{
return 31821 * value + 13849;
}
AVCodec g729_decoder =
{
"g729",
CODEC_TYPE_AUDIO,
CODEC_ID_G729,
sizeof(G729_Context),
ff_g729_decoder_init,
NULL,
NULL,
ff_g729_decode_frame,
.long_name = NULL_IF_CONFIG_SMALL("G.729"),
};

View File

@ -0,0 +1,345 @@
/*
* GIF encoder.
* Copyright (c) 2000 Fabrice Bellard.
* Copyright (c) 2002 Francois Revol.
* Copyright (c) 2006 Baptiste Coudurier.
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* First version by Francois Revol revol@free.fr
*
* Features and limitations:
* - currently no compression is performed,
* in fact the size of the data is 9/8 the size of the image in 8bpp
* - uses only a global standard palette
* - tested with IE 5.0, Opera for BeOS, NetPositive (BeOS), and Mozilla (BeOS).
*
* Reference documents:
* http://www.goice.co.jp/member/mo/formats/gif.html
* http://astronomy.swin.edu.au/pbourke/dataformats/gif/
* http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/GIF89a.txt
*
* this url claims to have an LZW algorithm not covered by Unisys patent:
* http://www.msg.net/utility/whirlgif/gifencod.html
* could help reduce the size of the files _a lot_...
* some sites mentions an RLE type compression also.
*/
#include "avcodec.h"
#include "bytestream.h"
#include "bitstream.h"
/* bitstream minipacket size */
#define GIF_CHUNKS 100
/* slows down the decoding (and some browsers don't like it) */
/* update on the 'some browsers don't like it issue from above: this was probably due to missing 'Data Sub-block Terminator' (byte 19) in the app_header */
#define GIF_ADD_APP_HEADER // required to enable looping of animated gif
typedef struct {
unsigned char r;
unsigned char g;
unsigned char b;
} rgb_triplet;
/* we use the standard 216 color palette */
/* this script was used to create the palette:
* for r in 00 33 66 99 cc ff; do for g in 00 33 66 99 cc ff; do echo -n " "; for b in 00 33 66 99 cc ff; do
* echo -n "{ 0x$r, 0x$g, 0x$b }, "; done; echo ""; done; done
*/
static const rgb_triplet gif_clut[216] = {
{ 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x33 }, { 0x00, 0x00, 0x66 }, { 0x00, 0x00, 0x99 }, { 0x00, 0x00, 0xcc }, { 0x00, 0x00, 0xff },
{ 0x00, 0x33, 0x00 }, { 0x00, 0x33, 0x33 }, { 0x00, 0x33, 0x66 }, { 0x00, 0x33, 0x99 }, { 0x00, 0x33, 0xcc }, { 0x00, 0x33, 0xff },
{ 0x00, 0x66, 0x00 }, { 0x00, 0x66, 0x33 }, { 0x00, 0x66, 0x66 }, { 0x00, 0x66, 0x99 }, { 0x00, 0x66, 0xcc }, { 0x00, 0x66, 0xff },
{ 0x00, 0x99, 0x00 }, { 0x00, 0x99, 0x33 }, { 0x00, 0x99, 0x66 }, { 0x00, 0x99, 0x99 }, { 0x00, 0x99, 0xcc }, { 0x00, 0x99, 0xff },
{ 0x00, 0xcc, 0x00 }, { 0x00, 0xcc, 0x33 }, { 0x00, 0xcc, 0x66 }, { 0x00, 0xcc, 0x99 }, { 0x00, 0xcc, 0xcc }, { 0x00, 0xcc, 0xff },
{ 0x00, 0xff, 0x00 }, { 0x00, 0xff, 0x33 }, { 0x00, 0xff, 0x66 }, { 0x00, 0xff, 0x99 }, { 0x00, 0xff, 0xcc }, { 0x00, 0xff, 0xff },
{ 0x33, 0x00, 0x00 }, { 0x33, 0x00, 0x33 }, { 0x33, 0x00, 0x66 }, { 0x33, 0x00, 0x99 }, { 0x33, 0x00, 0xcc }, { 0x33, 0x00, 0xff },
{ 0x33, 0x33, 0x00 }, { 0x33, 0x33, 0x33 }, { 0x33, 0x33, 0x66 }, { 0x33, 0x33, 0x99 }, { 0x33, 0x33, 0xcc }, { 0x33, 0x33, 0xff },
{ 0x33, 0x66, 0x00 }, { 0x33, 0x66, 0x33 }, { 0x33, 0x66, 0x66 }, { 0x33, 0x66, 0x99 }, { 0x33, 0x66, 0xcc }, { 0x33, 0x66, 0xff },
{ 0x33, 0x99, 0x00 }, { 0x33, 0x99, 0x33 }, { 0x33, 0x99, 0x66 }, { 0x33, 0x99, 0x99 }, { 0x33, 0x99, 0xcc }, { 0x33, 0x99, 0xff },
{ 0x33, 0xcc, 0x00 }, { 0x33, 0xcc, 0x33 }, { 0x33, 0xcc, 0x66 }, { 0x33, 0xcc, 0x99 }, { 0x33, 0xcc, 0xcc }, { 0x33, 0xcc, 0xff },
{ 0x33, 0xff, 0x00 }, { 0x33, 0xff, 0x33 }, { 0x33, 0xff, 0x66 }, { 0x33, 0xff, 0x99 }, { 0x33, 0xff, 0xcc }, { 0x33, 0xff, 0xff },
{ 0x66, 0x00, 0x00 }, { 0x66, 0x00, 0x33 }, { 0x66, 0x00, 0x66 }, { 0x66, 0x00, 0x99 }, { 0x66, 0x00, 0xcc }, { 0x66, 0x00, 0xff },
{ 0x66, 0x33, 0x00 }, { 0x66, 0x33, 0x33 }, { 0x66, 0x33, 0x66 }, { 0x66, 0x33, 0x99 }, { 0x66, 0x33, 0xcc }, { 0x66, 0x33, 0xff },
{ 0x66, 0x66, 0x00 }, { 0x66, 0x66, 0x33 }, { 0x66, 0x66, 0x66 }, { 0x66, 0x66, 0x99 }, { 0x66, 0x66, 0xcc }, { 0x66, 0x66, 0xff },
{ 0x66, 0x99, 0x00 }, { 0x66, 0x99, 0x33 }, { 0x66, 0x99, 0x66 }, { 0x66, 0x99, 0x99 }, { 0x66, 0x99, 0xcc }, { 0x66, 0x99, 0xff },
{ 0x66, 0xcc, 0x00 }, { 0x66, 0xcc, 0x33 }, { 0x66, 0xcc, 0x66 }, { 0x66, 0xcc, 0x99 }, { 0x66, 0xcc, 0xcc }, { 0x66, 0xcc, 0xff },
{ 0x66, 0xff, 0x00 }, { 0x66, 0xff, 0x33 }, { 0x66, 0xff, 0x66 }, { 0x66, 0xff, 0x99 }, { 0x66, 0xff, 0xcc }, { 0x66, 0xff, 0xff },
{ 0x99, 0x00, 0x00 }, { 0x99, 0x00, 0x33 }, { 0x99, 0x00, 0x66 }, { 0x99, 0x00, 0x99 }, { 0x99, 0x00, 0xcc }, { 0x99, 0x00, 0xff },
{ 0x99, 0x33, 0x00 }, { 0x99, 0x33, 0x33 }, { 0x99, 0x33, 0x66 }, { 0x99, 0x33, 0x99 }, { 0x99, 0x33, 0xcc }, { 0x99, 0x33, 0xff },
{ 0x99, 0x66, 0x00 }, { 0x99, 0x66, 0x33 }, { 0x99, 0x66, 0x66 }, { 0x99, 0x66, 0x99 }, { 0x99, 0x66, 0xcc }, { 0x99, 0x66, 0xff },
{ 0x99, 0x99, 0x00 }, { 0x99, 0x99, 0x33 }, { 0x99, 0x99, 0x66 }, { 0x99, 0x99, 0x99 }, { 0x99, 0x99, 0xcc }, { 0x99, 0x99, 0xff },
{ 0x99, 0xcc, 0x00 }, { 0x99, 0xcc, 0x33 }, { 0x99, 0xcc, 0x66 }, { 0x99, 0xcc, 0x99 }, { 0x99, 0xcc, 0xcc }, { 0x99, 0xcc, 0xff },
{ 0x99, 0xff, 0x00 }, { 0x99, 0xff, 0x33 }, { 0x99, 0xff, 0x66 }, { 0x99, 0xff, 0x99 }, { 0x99, 0xff, 0xcc }, { 0x99, 0xff, 0xff },
{ 0xcc, 0x00, 0x00 }, { 0xcc, 0x00, 0x33 }, { 0xcc, 0x00, 0x66 }, { 0xcc, 0x00, 0x99 }, { 0xcc, 0x00, 0xcc }, { 0xcc, 0x00, 0xff },
{ 0xcc, 0x33, 0x00 }, { 0xcc, 0x33, 0x33 }, { 0xcc, 0x33, 0x66 }, { 0xcc, 0x33, 0x99 }, { 0xcc, 0x33, 0xcc }, { 0xcc, 0x33, 0xff },
{ 0xcc, 0x66, 0x00 }, { 0xcc, 0x66, 0x33 }, { 0xcc, 0x66, 0x66 }, { 0xcc, 0x66, 0x99 }, { 0xcc, 0x66, 0xcc }, { 0xcc, 0x66, 0xff },
{ 0xcc, 0x99, 0x00 }, { 0xcc, 0x99, 0x33 }, { 0xcc, 0x99, 0x66 }, { 0xcc, 0x99, 0x99 }, { 0xcc, 0x99, 0xcc }, { 0xcc, 0x99, 0xff },
{ 0xcc, 0xcc, 0x00 }, { 0xcc, 0xcc, 0x33 }, { 0xcc, 0xcc, 0x66 }, { 0xcc, 0xcc, 0x99 }, { 0xcc, 0xcc, 0xcc }, { 0xcc, 0xcc, 0xff },
{ 0xcc, 0xff, 0x00 }, { 0xcc, 0xff, 0x33 }, { 0xcc, 0xff, 0x66 }, { 0xcc, 0xff, 0x99 }, { 0xcc, 0xff, 0xcc }, { 0xcc, 0xff, 0xff },
{ 0xff, 0x00, 0x00 }, { 0xff, 0x00, 0x33 }, { 0xff, 0x00, 0x66 }, { 0xff, 0x00, 0x99 }, { 0xff, 0x00, 0xcc }, { 0xff, 0x00, 0xff },
{ 0xff, 0x33, 0x00 }, { 0xff, 0x33, 0x33 }, { 0xff, 0x33, 0x66 }, { 0xff, 0x33, 0x99 }, { 0xff, 0x33, 0xcc }, { 0xff, 0x33, 0xff },
{ 0xff, 0x66, 0x00 }, { 0xff, 0x66, 0x33 }, { 0xff, 0x66, 0x66 }, { 0xff, 0x66, 0x99 }, { 0xff, 0x66, 0xcc }, { 0xff, 0x66, 0xff },
{ 0xff, 0x99, 0x00 }, { 0xff, 0x99, 0x33 }, { 0xff, 0x99, 0x66 }, { 0xff, 0x99, 0x99 }, { 0xff, 0x99, 0xcc }, { 0xff, 0x99, 0xff },
{ 0xff, 0xcc, 0x00 }, { 0xff, 0xcc, 0x33 }, { 0xff, 0xcc, 0x66 }, { 0xff, 0xcc, 0x99 }, { 0xff, 0xcc, 0xcc }, { 0xff, 0xcc, 0xff },
{ 0xff, 0xff, 0x00 }, { 0xff, 0xff, 0x33 }, { 0xff, 0xff, 0x66 }, { 0xff, 0xff, 0x99 }, { 0xff, 0xff, 0xcc }, { 0xff, 0xff, 0xff },
};
/* The GIF format uses reversed order for bitstreams... */
/* at least they don't use PDP_ENDIAN :) */
/* so we 'extend' PutBitContext. hmmm, OOP :) */
/* seems this thing changed slightly since I wrote it... */
#ifdef ALT_BITSTREAM_WRITER
# error no ALT_BITSTREAM_WRITER support for now
#endif
static void gif_put_bits_rev(PutBitContext *s, int n, unsigned int value)
{
unsigned int bit_buf;
int bit_cnt;
// printf("put_bits=%d %x\n", n, value);
assert(n == 32 || value < (1U << n));
bit_buf = s->bit_buf;
bit_cnt = 32 - s->bit_left; /* XXX:lazyness... was = s->bit_cnt; */
// printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
/* XXX: optimize */
if (n < (32-bit_cnt)) {
bit_buf |= value << (bit_cnt);
bit_cnt+=n;
} else {
bit_buf |= value << (bit_cnt);
bytestream_put_le32(&s->buf_ptr, bit_buf);
//printf("bitbuf = %08x\n", bit_buf);
if (s->buf_ptr >= s->buf_end)
abort();
// flush_buffer_rev(s);
bit_cnt=bit_cnt + n - 32;
if (bit_cnt == 0) {
bit_buf = 0;
} else {
bit_buf = value >> (n - bit_cnt);
}
}
s->bit_buf = bit_buf;
s->bit_left = 32 - bit_cnt;
}
/* pad the end of the output stream with zeros */
static void gif_flush_put_bits_rev(PutBitContext *s)
{
while (s->bit_left < 32) {
/* XXX: should test end of buffer */
*s->buf_ptr++=s->bit_buf & 0xff;
s->bit_buf>>=8;
s->bit_left+=8;
}
// flush_buffer_rev(s);
s->bit_left=32;
s->bit_buf=0;
}
/* !RevPutBitContext */
/* GIF header */
static int gif_image_write_header(uint8_t **bytestream,
int width, int height, int loop_count,
uint32_t *palette)
{
int i;
unsigned int v;
bytestream_put_buffer(bytestream, "GIF", 3);
bytestream_put_buffer(bytestream, "89a", 3);
bytestream_put_le16(bytestream, width);
bytestream_put_le16(bytestream, height);
bytestream_put_byte(bytestream, 0xf7); /* flags: global clut, 256 entries */
bytestream_put_byte(bytestream, 0x1f); /* background color index */
bytestream_put_byte(bytestream, 0); /* aspect ratio */
/* the global palette */
if (!palette) {
bytestream_put_buffer(bytestream, (const unsigned char *)gif_clut, 216*3);
for(i=0;i<((256-216)*3);i++)
bytestream_put_byte(bytestream, 0);
} else {
for(i=0;i<256;i++) {
v = palette[i];
bytestream_put_be24(bytestream, v);
}
}
/* update: this is the 'NETSCAPE EXTENSION' that allows for looped animated gif
see http://members.aol.com/royalef/gifabout.htm#net-extension
byte 1 : 33 (hex 0x21) GIF Extension code
byte 2 : 255 (hex 0xFF) Application Extension Label
byte 3 : 11 (hex (0x0B) Length of Application Block
(eleven bytes of data to follow)
bytes 4 to 11 : "NETSCAPE"
bytes 12 to 14 : "2.0"
byte 15 : 3 (hex 0x03) Length of Data Sub-Block
(three bytes of data to follow)
byte 16 : 1 (hex 0x01)
bytes 17 to 18 : 0 to 65535, an unsigned integer in
lo-hi byte format. This indicate the
number of iterations the loop should
be executed.
bytes 19 : 0 (hex 0x00) a Data Sub-block Terminator
*/
/* application extension header */
#ifdef GIF_ADD_APP_HEADER
if (loop_count >= 0 && loop_count <= 65535) {
bytestream_put_byte(bytestream, 0x21);
bytestream_put_byte(bytestream, 0xff);
bytestream_put_byte(bytestream, 0x0b);
bytestream_put_buffer(bytestream, "NETSCAPE2.0", 11); // bytes 4 to 14
bytestream_put_byte(bytestream, 0x03); // byte 15
bytestream_put_byte(bytestream, 0x01); // byte 16
bytestream_put_le16(bytestream, (uint16_t)loop_count);
bytestream_put_byte(bytestream, 0x00); // byte 19
}
#endif
return 0;
}
/* this is maybe slow, but allows for extensions */
static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
{
return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
}
static int gif_image_write_image(uint8_t **bytestream,
int x1, int y1, int width, int height,
const uint8_t *buf, int linesize, int pix_fmt)
{
PutBitContext p;
uint8_t buffer[200]; /* 100 * 9 / 8 = 113 */
int i, left, w, v;
const uint8_t *ptr;
/* image block */
bytestream_put_byte(bytestream, 0x2c);
bytestream_put_le16(bytestream, x1);
bytestream_put_le16(bytestream, y1);
bytestream_put_le16(bytestream, width);
bytestream_put_le16(bytestream, height);
bytestream_put_byte(bytestream, 0x00); /* flags */
/* no local clut */
bytestream_put_byte(bytestream, 0x08);
left= width * height;
init_put_bits(&p, buffer, 130);
/*
* the thing here is the bitstream is written as little packets, with a size byte before
* but it's still the same bitstream between packets (no flush !)
*/
ptr = buf;
w = width;
while(left>0) {
gif_put_bits_rev(&p, 9, 0x0100); /* clear code */
for(i=(left<GIF_CHUNKS)?left:GIF_CHUNKS;i;i--) {
if (pix_fmt == PIX_FMT_RGB24) {
v = gif_clut_index(ptr[0], ptr[1], ptr[2]);
ptr+=3;
} else {
v = *ptr++;
}
gif_put_bits_rev(&p, 9, v);
if (--w == 0) {
w = width;
buf += linesize;
ptr = buf;
}
}
if(left<=GIF_CHUNKS) {
gif_put_bits_rev(&p, 9, 0x101); /* end of stream */
gif_flush_put_bits_rev(&p);
}
if(pbBufPtr(&p) - p.buf > 0) {
bytestream_put_byte(bytestream, pbBufPtr(&p) - p.buf); /* byte count of the packet */
bytestream_put_buffer(bytestream, p.buf, pbBufPtr(&p) - p.buf); /* the actual buffer */
p.buf_ptr = p.buf; /* dequeue the bytes off the bitstream */
}
left-=GIF_CHUNKS;
}
bytestream_put_byte(bytestream, 0x00); /* end of image block */
bytestream_put_byte(bytestream, 0x3b);
return 0;
}
typedef struct {
int64_t time, file_time;
uint8_t buffer[100]; /* data chunks */
AVFrame picture;
} GIFContext;
static av_cold int gif_encode_init(AVCodecContext *avctx)
{
GIFContext *s = avctx->priv_data;
avctx->coded_frame = &s->picture;
return 0;
}
/* better than nothing gif encoder */
static int gif_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data)
{
GIFContext *s = avctx->priv_data;
AVFrame *pict = data;
AVFrame *const p = (AVFrame *)&s->picture;
uint8_t *outbuf_ptr = outbuf;
*p = *pict;
p->pict_type = FF_I_TYPE;
p->key_frame = 1;
gif_image_write_header(&outbuf_ptr, avctx->width, avctx->height, -1, (uint32_t *)pict->data[1]);
gif_image_write_image(&outbuf_ptr, 0, 0, avctx->width, avctx->height, pict->data[0], pict->linesize[0], PIX_FMT_PAL8);
return outbuf_ptr - outbuf;
}
AVCodec gif_encoder = {
"gif",
CODEC_TYPE_VIDEO,
CODEC_ID_GIF,
sizeof(GIFContext),
gif_encode_init,
gif_encode_frame,
NULL, //encode_end,
.pix_fmts= (enum PixelFormat[]){PIX_FMT_PAL8, PIX_FMT_NONE},
.long_name= NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
};

View File

@ -0,0 +1,337 @@
/*
* GIF decoder
* Copyright (c) 2003 Fabrice Bellard.
* Copyright (c) 2006 Baptiste Coudurier.
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
//#define DEBUG
#include "avcodec.h"
#include "bytestream.h"
#include "lzw.h"
#define GCE_DISPOSAL_NONE 0
#define GCE_DISPOSAL_INPLACE 1
#define GCE_DISPOSAL_BACKGROUND 2
#define GCE_DISPOSAL_RESTORE 3
typedef struct GifState {
AVFrame picture;
int screen_width;
int screen_height;
int bits_per_pixel;
int background_color_index;
int transparent_color_index;
int color_resolution;
uint32_t *image_palette;
/* after the frame is displayed, the disposal method is used */
int gce_disposal;
/* delay during which the frame is shown */
int gce_delay;
/* LZW compatible decoder */
const uint8_t *bytestream;
const uint8_t *bytestream_end;
LZWState *lzw;
/* aux buffers */
uint8_t global_palette[256 * 3];
uint8_t local_palette[256 * 3];
AVCodecContext* avctx;
} GifState;
static const uint8_t gif87a_sig[6] = "GIF87a";
static const uint8_t gif89a_sig[6] = "GIF89a";
static int gif_read_image(GifState *s)
{
int left, top, width, height, bits_per_pixel, code_size, flags;
int is_interleaved, has_local_palette, y, pass, y1, linesize, n, i;
uint8_t *ptr, *spal, *palette, *ptr1;
left = bytestream_get_le16(&s->bytestream);
top = bytestream_get_le16(&s->bytestream);
width = bytestream_get_le16(&s->bytestream);
height = bytestream_get_le16(&s->bytestream);
flags = bytestream_get_byte(&s->bytestream);
is_interleaved = flags & 0x40;
has_local_palette = flags & 0x80;
bits_per_pixel = (flags & 0x07) + 1;
#ifdef DEBUG
dprintf(s->avctx, "gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
#endif
if (has_local_palette) {
bytestream_get_buffer(&s->bytestream, s->local_palette, 3 * (1 << bits_per_pixel));
palette = s->local_palette;
} else {
palette = s->global_palette;
bits_per_pixel = s->bits_per_pixel;
}
/* verify that all the image is inside the screen dimensions */
if (left + width > s->screen_width ||
top + height > s->screen_height)
return AVERROR(EINVAL);
/* build the palette */
n = (1 << bits_per_pixel);
spal = palette;
for(i = 0; i < n; i++) {
s->image_palette[i] = (0xff << 24) | AV_RB24(spal);
spal += 3;
}
for(; i < 256; i++)
s->image_palette[i] = (0xff << 24);
/* handle transparency */
if (s->transparent_color_index >= 0)
s->image_palette[s->transparent_color_index] = 0;
/* now get the image data */
code_size = bytestream_get_byte(&s->bytestream);
ff_lzw_decode_init(s->lzw, code_size, s->bytestream,
s->bytestream_end - s->bytestream, FF_LZW_GIF);
/* read all the image */
linesize = s->picture.linesize[0];
ptr1 = s->picture.data[0] + top * linesize + left;
ptr = ptr1;
pass = 0;
y1 = 0;
for (y = 0; y < height; y++) {
ff_lzw_decode(s->lzw, ptr, width);
if (is_interleaved) {
switch(pass) {
default:
case 0:
case 1:
y1 += 8;
ptr += linesize * 8;
if (y1 >= height) {
y1 = pass ? 2 : 4;
ptr = ptr1 + linesize * y1;
pass++;
}
break;
case 2:
y1 += 4;
ptr += linesize * 4;
if (y1 >= height) {
y1 = 1;
ptr = ptr1 + linesize;
pass++;
}
break;
case 3:
y1 += 2;
ptr += linesize * 2;
break;
}
} else {
ptr += linesize;
}
}
/* read the garbage data until end marker is found */
ff_lzw_decode_tail(s->lzw);
s->bytestream = ff_lzw_cur_ptr(s->lzw);
return 0;
}
static int gif_read_extension(GifState *s)
{
int ext_code, ext_len, i, gce_flags, gce_transparent_index;
/* extension */
ext_code = bytestream_get_byte(&s->bytestream);
ext_len = bytestream_get_byte(&s->bytestream);
#ifdef DEBUG
dprintf(s->avctx, "gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
#endif
switch(ext_code) {
case 0xf9:
if (ext_len != 4)
goto discard_ext;
s->transparent_color_index = -1;
gce_flags = bytestream_get_byte(&s->bytestream);
s->gce_delay = bytestream_get_le16(&s->bytestream);
gce_transparent_index = bytestream_get_byte(&s->bytestream);
if (gce_flags & 0x01)
s->transparent_color_index = gce_transparent_index;
else
s->transparent_color_index = -1;
s->gce_disposal = (gce_flags >> 2) & 0x7;
#ifdef DEBUG
dprintf(s->avctx, "gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
gce_flags, s->gce_delay,
s->transparent_color_index, s->gce_disposal);
#endif
ext_len = bytestream_get_byte(&s->bytestream);
break;
}
/* NOTE: many extension blocks can come after */
discard_ext:
while (ext_len != 0) {
for (i = 0; i < ext_len; i++)
bytestream_get_byte(&s->bytestream);
ext_len = bytestream_get_byte(&s->bytestream);
#ifdef DEBUG
dprintf(s->avctx, "gif: ext_len1=%d\n", ext_len);
#endif
}
return 0;
}
static int gif_read_header1(GifState *s)
{
uint8_t sig[6];
int v, n;
int has_global_palette;
if (s->bytestream_end < s->bytestream + 13)
return -1;
/* read gif signature */
bytestream_get_buffer(&s->bytestream, sig, 6);
if (memcmp(sig, gif87a_sig, 6) != 0 &&
memcmp(sig, gif89a_sig, 6) != 0)
return -1;
/* read screen header */
s->transparent_color_index = -1;
s->screen_width = bytestream_get_le16(&s->bytestream);
s->screen_height = bytestream_get_le16(&s->bytestream);
if( (unsigned)s->screen_width > 32767
|| (unsigned)s->screen_height > 32767){
av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
return -1;
}
v = bytestream_get_byte(&s->bytestream);
s->color_resolution = ((v & 0x70) >> 4) + 1;
has_global_palette = (v & 0x80);
s->bits_per_pixel = (v & 0x07) + 1;
s->background_color_index = bytestream_get_byte(&s->bytestream);
bytestream_get_byte(&s->bytestream); /* ignored */
#ifdef DEBUG
dprintf(s->avctx, "gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
s->screen_width, s->screen_height, s->bits_per_pixel,
has_global_palette);
#endif
if (has_global_palette) {
n = 1 << s->bits_per_pixel;
if (s->bytestream_end < s->bytestream + n * 3)
return -1;
bytestream_get_buffer(&s->bytestream, s->global_palette, n * 3);
}
return 0;
}
static int gif_parse_next_image(GifState *s)
{
while (s->bytestream < s->bytestream_end) {
int code = bytestream_get_byte(&s->bytestream);
#ifdef DEBUG
dprintf(s->avctx, "gif: code=%02x '%c'\n", code, code);
#endif
switch (code) {
case ',':
return gif_read_image(s);
case '!':
if (gif_read_extension(s) < 0)
return -1;
break;
case ';':
/* end of image */
default:
/* error or erroneous EOF */
return -1;
}
}
return -1;
}
static av_cold int gif_decode_init(AVCodecContext *avctx)
{
GifState *s = avctx->priv_data;
s->avctx = avctx;
avcodec_get_frame_defaults(&s->picture);
avctx->coded_frame= &s->picture;
s->picture.data[0] = NULL;
ff_lzw_decode_open(&s->lzw);
return 0;
}
static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size)
{
GifState *s = avctx->priv_data;
AVFrame *picture = data;
int ret;
s->bytestream = buf;
s->bytestream_end = buf + buf_size;
if (gif_read_header1(s) < 0)
return -1;
avctx->pix_fmt = PIX_FMT_PAL8;
if (avcodec_check_dimensions(avctx, s->screen_width, s->screen_height))
return -1;
avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
if (s->picture.data[0])
avctx->release_buffer(avctx, &s->picture);
if (avctx->get_buffer(avctx, &s->picture) < 0) {
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return -1;
}
s->image_palette = (uint32_t *)s->picture.data[1];
ret = gif_parse_next_image(s);
if (ret < 0)
return ret;
*picture = s->picture;
*data_size = sizeof(AVPicture);
return s->bytestream - buf;
}
static av_cold int gif_decode_close(AVCodecContext *avctx)
{
GifState *s = avctx->priv_data;
ff_lzw_decode_close(&s->lzw);
if(s->picture.data[0])
avctx->release_buffer(avctx, &s->picture);
return 0;
}
AVCodec gif_decoder = {
"gif",
CODEC_TYPE_VIDEO,
CODEC_ID_GIF,
sizeof(GifState),
gif_decode_init,
NULL,
gif_decode_close,
gif_decode_frame,
.long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
};

View File

@ -2,31 +2,32 @@
* exp golomb vlc stuff
* Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
*
* This library is free software; you can redistribute it and/or
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file golomb.c
* @brief
* @brief
* exp golomb vlc stuff
* @author Michael Niedermayer <michaelni@gmx.at>
*/
#include "common.h"
#include "libavutil/common.h"
const uint8_t ff_golomb_vlc_len[512]={
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
@ -46,7 +47,7 @@ const uint8_t ff_golomb_vlc_len[512]={
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
};
const uint8_t ff_ue_golomb_vlc_code[512]={
const uint8_t ff_ue_golomb_vlc_code[512]={
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9,10,10,10,10,11,11,11,11,12,12,12,12,13,13,13,13,14,14,14,14,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
@ -65,7 +66,7 @@ const uint8_t ff_ue_golomb_vlc_code[512]={
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
const int8_t ff_se_golomb_vlc_code[512]={
const int8_t ff_se_golomb_vlc_code[512]={
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, -8, 9, -9, 10,-10, 11,-11, 12,-12, 13,-13, 14,-14, 15,-15,
4, 4, 4, 4, -4, -4, -4, -4, 5, 5, 5, 5, -5, -5, -5, -5, 6, 6, 6, 6, -6, -6, -6, -6, 7, 7, 7, 7, -7, -7, -7, -7,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
@ -85,7 +86,7 @@ const int8_t ff_se_golomb_vlc_code[512]={
};
const uint8_t ff_ue_golomb_len[256]={
const uint8_t ff_ue_golomb_len[256]={
1, 3, 3, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,11,
11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,13,
13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
@ -115,12 +116,12 @@ const uint8_t ff_interleaved_golomb_vlc_len[256]={
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
};
const uint8_t ff_interleaved_ue_golomb_vlc_code[256]={
const uint8_t ff_interleaved_ue_golomb_vlc_code[256]={
15,16,7, 7, 17,18,8, 8, 3, 3, 3, 3, 3, 3, 3, 3,
19,20,9, 9, 21,22,10,10,4, 4, 4, 4, 4, 4, 4, 4,
19,20,9, 9, 21,22,10,10,4, 4, 4, 4, 4, 4, 4, 4,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
23,24,11,11,25,26,12,12,5, 5, 5, 5, 5, 5, 5, 5,
23,24,11,11,25,26,12,12,5, 5, 5, 5, 5, 5, 5, 5,
27,28,13,13,29,30,14,14,6, 6, 6, 6, 6, 6, 6, 6,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
@ -134,7 +135,7 @@ const uint8_t ff_interleaved_ue_golomb_vlc_code[256]={
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
const int8_t ff_interleaved_se_golomb_vlc_code[256]={
const int8_t ff_interleaved_se_golomb_vlc_code[256]={
8, -8, 4, 4, 9, -9, -4, -4, 2, 2, 2, 2, 2, 2, 2, 2,
10,-10, 5, 5, 11,-11, -5, -5, -2, -2, -2, -2, -2, -2, -2, -2,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
@ -152,3 +153,21 @@ const int8_t ff_interleaved_se_golomb_vlc_code[256]={
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
const uint8_t ff_interleaved_dirac_golomb_vlc_code[256]={
0, 1, 0, 0, 2, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
4, 5, 2, 2, 6, 7, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8, 9, 4, 4, 10,11,5, 5, 2, 2, 2, 2, 2, 2, 2, 2,
12,13,6, 6, 14,15,7, 7, 3, 3, 3, 3, 3, 3, 3, 3,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};

View File

@ -1,30 +1,38 @@
/*
* exp golomb vlc stuff
* Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
* Copyright (c) 2004 Alex Beregszaszi
*
* This library is free software; you can redistribute it and/or
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file golomb.h
* @brief
* @brief
* exp golomb vlc stuff
* @author Michael Niedermayer <michaelni@gmx.at>
* @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
*/
#ifndef FFMPEG_GOLOMB_H
#define FFMPEG_GOLOMB_H
#include <stdint.h>
#include "bitstream.h"
#define INVALID_VLC 0x80000000
extern const uint8_t ff_golomb_vlc_len[512];
@ -35,24 +43,25 @@ extern const uint8_t ff_ue_golomb_len[256];
extern const uint8_t ff_interleaved_golomb_vlc_len[256];
extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
extern const int8_t ff_interleaved_se_golomb_vlc_code[256];
extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256];
/**
* read unsigned exp golomb code.
*/
static inline int get_ue_golomb(GetBitContext *gb){
unsigned int buf;
int log;
OPEN_READER(re, gb);
UPDATE_CACHE(re, gb);
buf=GET_CACHE(re, gb);
if(buf >= (1<<27)){
buf >>= 32 - 9;
LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
CLOSE_READER(re, gb);
return ff_ue_golomb_vlc_code[buf];
}else{
log= 2*av_log2(buf) - 31;
@ -60,38 +69,43 @@ static inline int get_ue_golomb(GetBitContext *gb){
buf--;
LAST_SKIP_BITS(re, gb, 32 - log);
CLOSE_READER(re, gb);
return buf;
}
}
static inline int svq3_get_ue_golomb(GetBitContext *gb){
uint32_t buf;
int log;
OPEN_READER(re, gb);
UPDATE_CACHE(re, gb);
buf=GET_CACHE(re, gb);
if(buf&0xAA800000){
buf >>= 32 - 8;
LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
CLOSE_READER(re, gb);
return ff_interleaved_ue_golomb_vlc_code[buf];
}else{
buf|=1;
if((buf & 0xAAAAAAAA) == 0)
return INVALID_VLC;
int ret = 1;
for(log=31; (buf & 0x80000000) == 0; log--){
buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
while (1) {
buf >>= 32 - 8;
LAST_SKIP_BITS(re, gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
if (ff_interleaved_golomb_vlc_len[buf] != 9){
ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
break;
}
ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
UPDATE_CACHE(re, gb);
buf = GET_CACHE(re, gb);
}
LAST_SKIP_BITS(re, gb, 63 - 2*log);
CLOSE_READER(re, gb);
return ((buf << log) >> log) - 1;
return ret - 1;
}
}
@ -100,7 +114,7 @@ static inline int svq3_get_ue_golomb(GetBitContext *gb){
*/
static inline int get_te0_golomb(GetBitContext *gb, int range){
assert(range >= 1);
if(range==1) return 0;
else if(range==2) return get_bits1(gb)^1;
else return get_ue_golomb(gb);
@ -111,7 +125,7 @@ static inline int get_te0_golomb(GetBitContext *gb, int range){
*/
static inline int get_te_golomb(GetBitContext *gb, int range){
assert(range >= 1);
if(range==2) return get_bits1(gb)^1;
else return get_ue_golomb(gb);
}
@ -123,24 +137,24 @@ static inline int get_te_golomb(GetBitContext *gb, int range){
static inline int get_se_golomb(GetBitContext *gb){
unsigned int buf;
int log;
OPEN_READER(re, gb);
UPDATE_CACHE(re, gb);
buf=GET_CACHE(re, gb);
if(buf >= (1<<27)){
buf >>= 32 - 9;
LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
CLOSE_READER(re, gb);
return ff_se_golomb_vlc_code[buf];
}else{
log= 2*av_log2(buf) - 31;
buf>>= log;
LAST_SKIP_BITS(re, gb, 32 - log);
CLOSE_READER(re, gb);
if(buf&1) buf= -(buf>>1);
else buf= (buf>>1);
@ -160,10 +174,13 @@ static inline int svq3_get_se_golomb(GetBitContext *gb){
buf >>= 32 - 8;
LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
CLOSE_READER(re, gb);
return ff_interleaved_se_golomb_vlc_code[buf];
}else{
buf |=1;
LAST_SKIP_BITS(re, gb, 8);
UPDATE_CACHE(re, gb);
buf |= 1 | (GET_CACHE(re, gb) >> 8);
if((buf & 0xAAAAAAAA) == 0)
return INVALID_VLC;
@ -171,20 +188,38 @@ static inline int svq3_get_se_golomb(GetBitContext *gb){
buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
}
LAST_SKIP_BITS(re, gb, 63 - 2*log);
LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
CLOSE_READER(re, gb);
return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
}
}
static inline int dirac_get_se_golomb(GetBitContext *gb){
uint32_t buf;
uint32_t ret;
ret = svq3_get_ue_golomb(gb);
if (ret) {
OPEN_READER(re, gb);
UPDATE_CACHE(re, gb);
buf = SHOW_SBITS(re, gb, 1);
LAST_SKIP_BITS(re, gb, 1);
ret = (ret ^ buf) - buf;
CLOSE_READER(re, gb);
}
return ret;
}
/**
* read unsigned golomb rice code (ffv1).
*/
static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
unsigned int buf;
int log;
OPEN_READER(re, gb);
UPDATE_CACHE(re, gb);
buf=GET_CACHE(re, gb);
@ -196,13 +231,13 @@ static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len
buf += (30-log)<<k;
LAST_SKIP_BITS(re, gb, 32 + k - log);
CLOSE_READER(re, gb);
return buf;
}else{
buf >>= 32 - limit - esc_len;
LAST_SKIP_BITS(re, gb, esc_len + limit);
CLOSE_READER(re, gb);
return buf + limit - 1;
}
}
@ -213,19 +248,19 @@ static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len
static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
unsigned int buf;
int log;
OPEN_READER(re, gb);
UPDATE_CACHE(re, gb);
buf=GET_CACHE(re, gb);
log= av_log2(buf);
if(log > 31-11){
buf >>= log - k;
buf += (30-log)<<k;
LAST_SKIP_BITS(re, gb, 32 + k - log);
CLOSE_READER(re, gb);
return buf;
}else{
int i;
@ -249,54 +284,96 @@ static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int
buf = SHOW_UBITS(re, gb, esc_len);
LAST_SKIP_BITS(re, gb, esc_len);
CLOSE_READER(re, gb);
return buf + 1;
}else
return -1;
}
}
/**
* read signed golomb rice code (ffv1).
*/
static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
int v= get_ur_golomb(gb, k, limit, esc_len);
v++;
if (v&1) return v>>1;
else return -(v>>1);
// return (v>>1) ^ -(v&1);
}
/**
* read signed golomb rice code (flac).
*/
static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
return (v>>1) ^ -(v&1);
}
/**
* read unsigned golomb rice code (shorten).
*/
static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
}
/**
* read signed golomb rice code (shorten).
*/
static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
{
int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
if (uvar & 1)
return ~(uvar >> 1);
else
return uvar >> 1;
}
#ifdef TRACE
static inline int get_ue(GetBitContext *s, char *file, char *func, int line){
static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
int show= show_bits(s, 24);
int pos= get_bits_count(s);
int i= get_ue_golomb(s);
int len= get_bits_count(s) - pos;
int bits= show>>(24-len);
print_bin(bits, len);
printf("%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
return i;
}
static inline int get_se(GetBitContext *s, char *file, char *func, int line){
static inline int get_se(GetBitContext *s, char *file, const char *func, int line){
int show= show_bits(s, 24);
int pos= get_bits_count(s);
int i= get_se_golomb(s);
int len= get_bits_count(s) - pos;
int bits= show>>(24-len);
print_bin(bits, len);
printf("%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
return i;
}
static inline int get_te(GetBitContext *s, int r, char *file, char *func, int line){
static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
int show= show_bits(s, 24);
int pos= get_bits_count(s);
int i= get_te0_golomb(s, r);
int len= get_bits_count(s) - pos;
int bits= show>>(24-len);
print_bin(bits, len);
printf("%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
return i;
}
@ -312,7 +389,7 @@ static inline int get_te(GetBitContext *s, int r, char *file, char *func, int li
*/
static inline void set_ue_golomb(PutBitContext *pb, int i){
int e;
assert(i>=0);
#if 0
@ -325,7 +402,7 @@ static inline void set_ue_golomb(PutBitContext *pb, int i){
put_bits(pb, ff_ue_golomb_len[i], i+1);
else{
e= av_log2(i+1);
put_bits(pb, 2*e+1, i+1);
}
}
@ -342,10 +419,12 @@ static inline void set_te_golomb(PutBitContext *pb, int i, int range){
}
/**
* write signed exp golomb code.
* write signed exp golomb code. 16 bits at most.
*/
static inline void set_se_golomb(PutBitContext *pb, int i){
#if 0
// if (i>32767 || i<-32767)
// av_log(NULL,AV_LOG_ERROR,"value out of range %d\n", i);
#if 0
if(i<=0) i= -2*i;
else i= 2*i-1;
#elif 1
@ -363,9 +442,9 @@ static inline void set_se_golomb(PutBitContext *pb, int i){
*/
static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
int e;
assert(i>=0);
e= i>>k;
if(e<limit){
put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
@ -379,16 +458,50 @@ static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int
*/
static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
int e;
assert(i>=0);
e= (i>>k) + 1;
if(e<limit){
while(e > 31) {
put_bits(pb, 31, 0);
e -= 31;
}
put_bits(pb, e, 1);
if(k)
put_bits(pb, k, i&((1<<k)-1));
put_sbits(pb, k, i);
}else{
while(limit > 31) {
put_bits(pb, 31, 0);
limit -= 31;
}
put_bits(pb, limit , 1);
put_bits(pb, esc_len, i - 1);
}
}
/**
* write signed golomb rice code (ffv1).
*/
static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
int v;
v = -2*i-1;
v ^= (v>>31);
set_ur_golomb(pb, v, k, limit, esc_len);
}
/**
* write signed golomb rice code (flac).
*/
static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
int v;
v = -2*i-1;
v ^= (v>>31);
set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
}
#endif /* FFMPEG_GOLOMB_H */