Update avcodec to 20080825

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@27565 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
David McPaul
2008-09-15 14:13:44 +00:00
parent bc470b16bf
commit c0d69ba2f1
14 changed files with 3074 additions and 973 deletions
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,69 @@
/*
* AVCodecParser prototypes and definitions
* Copyright (c) 2003 Fabrice Bellard.
* Copyright (c) 2003 Michael Niedermayer.
*
* 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_PARSER_H
#define FFMPEG_PARSER_H
#include "avcodec.h"
typedef struct ParseContext{
uint8_t *buffer;
int index;
int last_index;
unsigned int buffer_size;
uint32_t state; ///< contains the last few bytes in MSB order
int frame_start_found;
int overread; ///< the number of bytes which where irreversibly read from the next frame
int overread_index; ///< the index into ParseContext.buffer of the overread bytes
} ParseContext;
struct MpegEncContext;
typedef struct ParseContext1{
ParseContext pc;
/* XXX/FIXME PC1 vs. PC */
/* MPEG-2-specific */
AVRational frame_rate;
int progressive_sequence;
int width, height;
/* XXX: suppress that, needed by MPEG-4 */
struct MpegEncContext *enc;
int first_picture;
} ParseContext1;
#define END_NOT_FOUND (-100)
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size);
int ff_mpeg4video_split(AVCodecContext *avctx, const uint8_t *buf,
int buf_size);
void ff_parse_close(AVCodecParserContext *s);
void ff_parse1_close(AVCodecParserContext *s);
/**
* Fetches timestamps for a specific byte within the current access unit.
* @param off byte position within the access unit
* @param remove Found timestamps will be removed if set to 1, kept if set to 0.
*/
void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove);
#endif /* FFMPEG_PARSER_H */
+332 -135
View File
@@ -2,19 +2,21 @@
* PCM codecs
* Copyright (c) 2001 Fabrice Bellard.
*
* 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
*/
/**
@@ -23,6 +25,10 @@
*/
#include "avcodec.h"
#include "bitstream.h" // for ff_reverse
#include "bytestream.h"
#define MAX_CHANNELS 64
/* from g711.c by SUN microsystems (unrestricted use) */
@@ -38,7 +44,7 @@
* alaw2linear() - Convert an A-law value to 16-bit linear PCM
*
*/
static int alaw2linear(unsigned char a_val)
static av_cold int alaw2linear(unsigned char a_val)
{
int t;
int seg;
@@ -50,10 +56,10 @@ static int alaw2linear(unsigned char a_val)
if(seg) t= (t + t + 1 + 32) << (seg + 2);
else t= (t + t + 1 ) << 3;
return ((a_val & SIGN_BIT) ? t : -t);
return (a_val & SIGN_BIT) ? t : -t;
}
static int ulaw2linear(unsigned char u_val)
static av_cold int ulaw2linear(unsigned char u_val)
{
int t;
@@ -67,17 +73,14 @@ static int ulaw2linear(unsigned char u_val)
t = ((u_val & QUANT_MASK) << 3) + BIAS;
t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
return (u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS);
}
/* 16384 entries per table */
static uint8_t *linear_to_alaw = NULL;
static int linear_to_alaw_ref = 0;
static uint8_t linear_to_alaw[16384];
static uint8_t linear_to_ulaw[16384];
static uint8_t *linear_to_ulaw = NULL;
static int linear_to_ulaw_ref = 0;
static void build_xlaw_table(uint8_t *linear_to_xlaw,
static av_cold void build_xlaw_table(uint8_t *linear_to_xlaw,
int (*xlaw2linear)(unsigned char),
int mask)
{
@@ -101,141 +104,168 @@ static void build_xlaw_table(uint8_t *linear_to_xlaw,
linear_to_xlaw[0] = linear_to_xlaw[1];
}
static int pcm_encode_init(AVCodecContext *avctx)
static av_cold int pcm_encode_init(AVCodecContext *avctx)
{
avctx->frame_size = 1;
switch(avctx->codec->id) {
case CODEC_ID_PCM_ALAW:
if (linear_to_alaw_ref == 0) {
linear_to_alaw = av_malloc(16384);
if (!linear_to_alaw)
return -1;
build_xlaw_table(linear_to_alaw, alaw2linear, 0xd5);
}
linear_to_alaw_ref++;
break;
case CODEC_ID_PCM_MULAW:
if (linear_to_ulaw_ref == 0) {
linear_to_ulaw = av_malloc(16384);
if (!linear_to_ulaw)
return -1;
build_xlaw_table(linear_to_ulaw, ulaw2linear, 0xff);
}
linear_to_ulaw_ref++;
break;
default:
break;
}
avctx->block_align = avctx->channels * av_get_bits_per_sample(avctx->codec->id)/8;
avctx->coded_frame= avcodec_alloc_frame();
avctx->coded_frame->key_frame= 1;
return 0;
}
static int pcm_encode_close(AVCodecContext *avctx)
static av_cold int pcm_encode_close(AVCodecContext *avctx)
{
av_freep(&avctx->coded_frame);
switch(avctx->codec->id) {
case CODEC_ID_PCM_ALAW:
if (--linear_to_alaw_ref == 0)
av_free(linear_to_alaw);
break;
case CODEC_ID_PCM_MULAW:
if (--linear_to_ulaw_ref == 0)
av_free(linear_to_ulaw);
break;
default:
/* nothing to free */
break;
}
return 0;
}
/**
* Write PCM samples macro
* @param type Datatype of native machine format
* @param endian bytestream_put_xxx() suffix
* @param src Source pointer (variable name)
* @param dst Destination pointer (variable name)
* @param n Total number of samples (variable name)
* @param shift Bitshift (bits)
* @param offset Sample value offset
*/
#define ENCODE(type, endian, src, dst, n, shift, offset) \
samples_##type = (type*)src; \
for(;n>0;n--) { \
register type v = (*samples_##type++ >> shift) + offset; \
bytestream_put_##endian(&dst, v); \
}
static int pcm_encode_frame(AVCodecContext *avctx,
unsigned char *frame, int buf_size, void *data)
{
int n, sample_size, v;
short *samples;
unsigned char *dst;
uint8_t *srcu8;
int16_t *samples_int16_t;
int32_t *samples_int32_t;
int64_t *samples_int64_t;
uint16_t *samples_uint16_t;
uint32_t *samples_uint32_t;
switch(avctx->codec->id) {
case CODEC_ID_PCM_S16LE:
case CODEC_ID_PCM_S16BE:
case CODEC_ID_PCM_U16LE:
case CODEC_ID_PCM_U16BE:
sample_size = 2;
break;
default:
sample_size = 1;
break;
}
sample_size = av_get_bits_per_sample(avctx->codec->id)/8;
n = buf_size / sample_size;
samples = data;
dst = frame;
switch(avctx->codec->id) {
case CODEC_ID_PCM_S16LE:
for(;n>0;n--) {
v = *samples++;
dst[0] = v & 0xff;
dst[1] = v >> 8;
dst += 2;
if (avctx->sample_fmt!=avctx->codec->sample_fmts[0]) {
av_log(avctx, AV_LOG_ERROR, "invalid sample_fmt\n");
return -1;
}
switch(avctx->codec->id) {
case CODEC_ID_PCM_U32LE:
ENCODE(uint32_t, le32, samples, dst, n, 0, 0x80000000)
break;
case CODEC_ID_PCM_S16BE:
case CODEC_ID_PCM_U32BE:
ENCODE(uint32_t, be32, samples, dst, n, 0, 0x80000000)
break;
case CODEC_ID_PCM_S24LE:
ENCODE(int32_t, le24, samples, dst, n, 8, 0)
break;
case CODEC_ID_PCM_S24BE:
ENCODE(int32_t, be24, samples, dst, n, 8, 0)
break;
case CODEC_ID_PCM_U24LE:
ENCODE(uint32_t, le24, samples, dst, n, 8, 0x800000)
break;
case CODEC_ID_PCM_U24BE:
ENCODE(uint32_t, be24, samples, dst, n, 8, 0x800000)
break;
case CODEC_ID_PCM_S24DAUD:
for(;n>0;n--) {
v = *samples++;
dst[0] = v >> 8;
dst[1] = v;
dst += 2;
uint32_t tmp = ff_reverse[(*samples >> 8) & 0xff] +
(ff_reverse[*samples & 0xff] << 8);
tmp <<= 4; // sync flags would go here
bytestream_put_be24(&dst, tmp);
samples++;
}
break;
case CODEC_ID_PCM_U16LE:
for(;n>0;n--) {
v = *samples++;
v += 0x8000;
dst[0] = v & 0xff;
dst[1] = v >> 8;
dst += 2;
}
ENCODE(uint16_t, le16, samples, dst, n, 0, 0x8000)
break;
case CODEC_ID_PCM_U16BE:
for(;n>0;n--) {
v = *samples++;
v += 0x8000;
dst[0] = v >> 8;
dst[1] = v;
dst += 2;
}
ENCODE(uint16_t, be16, samples, dst, n, 0, 0x8000)
break;
case CODEC_ID_PCM_S8:
srcu8= data;
for(;n>0;n--) {
v = *samples++;
dst[0] = v >> 8;
dst++;
v = *srcu8++;
*dst++ = v - 128;
}
break;
#if WORDS_BIGENDIAN
case CODEC_ID_PCM_F64LE:
ENCODE(int64_t, le64, samples, dst, n, 0, 0)
break;
case CODEC_ID_PCM_S32LE:
case CODEC_ID_PCM_F32LE:
ENCODE(int32_t, le32, samples, dst, n, 0, 0)
break;
case CODEC_ID_PCM_S16LE:
ENCODE(int16_t, le16, samples, dst, n, 0, 0)
break;
case CODEC_ID_PCM_F64BE:
case CODEC_ID_PCM_F32BE:
case CODEC_ID_PCM_S32BE:
case CODEC_ID_PCM_S16BE:
#else
case CODEC_ID_PCM_F64BE:
ENCODE(int64_t, be64, samples, dst, n, 0, 0)
break;
case CODEC_ID_PCM_F32BE:
case CODEC_ID_PCM_S32BE:
ENCODE(int32_t, be32, samples, dst, n, 0, 0)
break;
case CODEC_ID_PCM_S16BE:
ENCODE(int16_t, be16, samples, dst, n, 0, 0)
break;
case CODEC_ID_PCM_F64LE:
case CODEC_ID_PCM_F32LE:
case CODEC_ID_PCM_S32LE:
case CODEC_ID_PCM_S16LE:
#endif /* WORDS_BIGENDIAN */
case CODEC_ID_PCM_U8:
memcpy(dst, samples, n*sample_size);
dst += n*sample_size;
break;
case CODEC_ID_PCM_ZORK:
for(;n>0;n--) {
v = *samples++;
dst[0] = (v >> 8) + 128;
dst++;
v= *samples++ >> 8;
if(v<0) v = -v;
else v+= 128;
*dst++ = v;
}
break;
case CODEC_ID_PCM_ALAW:
for(;n>0;n--) {
v = *samples++;
dst[0] = linear_to_alaw[(v + 32768) >> 2];
dst++;
*dst++ = linear_to_alaw[(v + 32768) >> 2];
}
break;
case CODEC_ID_PCM_MULAW:
for(;n>0;n--) {
v = *samples++;
dst[0] = linear_to_ulaw[(v + 32768) >> 2];
dst++;
*dst++ = linear_to_ulaw[(v + 32768) >> 2];
}
break;
default:
@@ -250,7 +280,7 @@ typedef struct PCMDecode {
short table[256];
} PCMDecode;
static int pcm_decode_init(AVCodecContext * avctx)
static av_cold int pcm_decode_init(AVCodecContext * avctx)
{
PCMDecode *s = avctx->priv_data;
int i;
@@ -267,81 +297,217 @@ static int pcm_decode_init(AVCodecContext * avctx)
default:
break;
}
avctx->sample_fmt = avctx->codec->sample_fmts[0];
return 0;
}
/**
* Read PCM samples macro
* @param type Datatype of native machine format
* @param endian bytestream_get_xxx() endian suffix
* @param src Source pointer (variable name)
* @param dst Destination pointer (variable name)
* @param n Total number of samples (variable name)
* @param shift Bitshift (bits)
* @param offset Sample value offset
*/
#define DECODE(type, endian, src, dst, n, shift, offset) \
dst_##type = (type*)dst; \
for(;n>0;n--) { \
register type v = bytestream_get_##endian(&src); \
*dst_##type++ = (v - offset) << shift; \
} \
dst = (short*)dst_##type;
static int pcm_decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
uint8_t *buf, int buf_size)
const uint8_t *buf, int buf_size)
{
PCMDecode *s = avctx->priv_data;
int n;
int sample_size, c, n;
short *samples;
uint8_t *src;
const uint8_t *src, *src8, *src2[MAX_CHANNELS];
uint8_t *dstu8;
int16_t *dst_int16_t;
int32_t *dst_int32_t;
int64_t *dst_int64_t;
uint16_t *dst_uint16_t;
uint32_t *dst_uint32_t;
samples = data;
src = buf;
if (avctx->sample_fmt!=avctx->codec->sample_fmts[0]) {
av_log(avctx, AV_LOG_ERROR, "invalid sample_fmt\n");
return -1;
}
if(avctx->channels <= 0 || avctx->channels > MAX_CHANNELS){
av_log(avctx, AV_LOG_ERROR, "PCM channels out of bounds\n");
return -1;
}
sample_size = av_get_bits_per_sample(avctx->codec_id)/8;
/* av_get_bits_per_sample returns 0 for CODEC_ID_PCM_DVD */
if (CODEC_ID_PCM_DVD == avctx->codec_id)
/* 2 samples are interleaved per block in PCM_DVD */
sample_size = avctx->bits_per_sample * 2 / 8;
n = avctx->channels * sample_size;
if(n && buf_size % n){
av_log(avctx, AV_LOG_ERROR, "invalid PCM packet\n");
return -1;
}
buf_size= FFMIN(buf_size, *data_size/2);
*data_size=0;
n = buf_size/sample_size;
switch(avctx->codec->id) {
case CODEC_ID_PCM_S16LE:
n = buf_size >> 1;
case CODEC_ID_PCM_U32LE:
DECODE(uint32_t, le32, src, samples, n, 0, 0x80000000)
break;
case CODEC_ID_PCM_U32BE:
DECODE(uint32_t, be32, src, samples, n, 0, 0x80000000)
break;
case CODEC_ID_PCM_S24LE:
DECODE(int32_t, le24, src, samples, n, 8, 0)
break;
case CODEC_ID_PCM_S24BE:
DECODE(int32_t, be24, src, samples, n, 8, 0)
break;
case CODEC_ID_PCM_U24LE:
DECODE(uint32_t, le24, src, samples, n, 8, 0x800000)
break;
case CODEC_ID_PCM_U24BE:
DECODE(uint32_t, be24, src, samples, n, 8, 0x800000)
break;
case CODEC_ID_PCM_S24DAUD:
for(;n>0;n--) {
*samples++ = src[0] | (src[1] << 8);
src += 2;
uint32_t v = bytestream_get_be24(&src);
v >>= 4; // sync flags are here
*samples++ = ff_reverse[(v >> 8) & 0xff] +
(ff_reverse[v & 0xff] << 8);
}
break;
case CODEC_ID_PCM_S16BE:
n = buf_size >> 1;
for(;n>0;n--) {
*samples++ = (src[0] << 8) | src[1];
src += 2;
}
case CODEC_ID_PCM_S16LE_PLANAR:
n /= avctx->channels;
for(c=0;c<avctx->channels;c++)
src2[c] = &src[c*n*2];
for(;n>0;n--)
for(c=0;c<avctx->channels;c++)
*samples++ = bytestream_get_le16(&src2[c]);
src = src2[avctx->channels-1];
break;
case CODEC_ID_PCM_U16LE:
n = buf_size >> 1;
for(;n>0;n--) {
*samples++ = (src[0] | (src[1] << 8)) - 0x8000;
src += 2;
}
DECODE(uint16_t, le16, src, samples, n, 0, 0x8000)
break;
case CODEC_ID_PCM_U16BE:
n = buf_size >> 1;
for(;n>0;n--) {
*samples++ = ((src[0] << 8) | src[1]) - 0x8000;
src += 2;
}
DECODE(uint16_t, be16, src, samples, n, 0, 0x8000)
break;
case CODEC_ID_PCM_S8:
n = buf_size;
dstu8= (uint8_t*)samples;
for(;n>0;n--) {
*samples++ = src[0] << 8;
src++;
*dstu8++ = *src++ + 128;
}
samples= (short*)dstu8;
break;
#if WORDS_BIGENDIAN
case CODEC_ID_PCM_F64LE:
DECODE(int64_t, le64, src, samples, n, 0, 0)
break;
case CODEC_ID_PCM_S32LE:
case CODEC_ID_PCM_F32LE:
DECODE(int32_t, le32, src, samples, n, 0, 0)
break;
case CODEC_ID_PCM_S16LE:
DECODE(int16_t, le16, src, samples, n, 0, 0)
break;
case CODEC_ID_PCM_F64BE:
case CODEC_ID_PCM_F32BE:
case CODEC_ID_PCM_S32BE:
case CODEC_ID_PCM_S16BE:
#else
case CODEC_ID_PCM_F64BE:
DECODE(int64_t, be64, src, samples, n, 0, 0)
break;
case CODEC_ID_PCM_F32BE:
case CODEC_ID_PCM_S32BE:
DECODE(int32_t, be32, src, samples, n, 0, 0)
break;
case CODEC_ID_PCM_S16BE:
DECODE(int16_t, be16, src, samples, n, 0, 0)
break;
case CODEC_ID_PCM_F64LE:
case CODEC_ID_PCM_F32LE:
case CODEC_ID_PCM_S32LE:
case CODEC_ID_PCM_S16LE:
#endif /* WORDS_BIGENDIAN */
case CODEC_ID_PCM_U8:
n = buf_size;
memcpy(samples, src, n*sample_size);
src += n*sample_size;
samples = (short*)((uint8_t*)data + n*sample_size);
break;
case CODEC_ID_PCM_ZORK:
for(;n>0;n--) {
*samples++ = ((int)src[0] - 128) << 8;
src++;
int x= *src++;
if(x&128) x-= 128;
else x = -x;
*samples++ = x << 8;
}
break;
case CODEC_ID_PCM_ALAW:
case CODEC_ID_PCM_MULAW:
n = buf_size;
for(;n>0;n--) {
*samples++ = s->table[src[0]];
src++;
*samples++ = s->table[*src++];
}
break;
case CODEC_ID_PCM_DVD:
dst_int32_t = data;
n /= avctx->channels;
switch (avctx->bits_per_sample) {
case 20:
while (n--) {
c = avctx->channels;
src8 = src + 4*c;
while (c--) {
*dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8 &0xf0) << 8);
*dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++ &0x0f) << 12);
}
src = src8;
}
break;
case 24:
while (n--) {
c = avctx->channels;
src8 = src + 4*c;
while (c--) {
*dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++) << 8);
*dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++) << 8);
}
src = src8;
}
break;
default:
*data_size = 0;
av_log(avctx, AV_LOG_ERROR, "PCM DVD unsupported sample depth\n");
return -1;
break;
}
samples = (short *) dst_int32_t;
break;
default:
return -1;
}
*data_size = (uint8_t *)samples - (uint8_t *)data;
return src - buf;
}
#define PCM_CODEC(id, name) \
#ifdef CONFIG_ENCODERS
#define PCM_ENCODER(id,sample_fmt_,name,long_name_) \
AVCodec name ## _encoder = { \
#name, \
CODEC_TYPE_AUDIO, \
@@ -351,7 +517,15 @@ AVCodec name ## _encoder = { \
pcm_encode_frame, \
pcm_encode_close, \
NULL, \
}; \
.sample_fmts = (enum SampleFormat[]){sample_fmt_,SAMPLE_FMT_NONE}, \
.long_name = NULL_IF_CONFIG_SMALL(long_name_), \
};
#else
#define PCM_ENCODER(id,sample_fmt_,name,long_name_)
#endif
#ifdef CONFIG_DECODERS
#define PCM_DECODER(id,sample_fmt_,name,long_name_) \
AVCodec name ## _decoder = { \
#name, \
CODEC_TYPE_AUDIO, \
@@ -361,15 +535,38 @@ AVCodec name ## _decoder = { \
NULL, \
NULL, \
pcm_decode_frame, \
}
.sample_fmts = (enum SampleFormat[]){sample_fmt_,SAMPLE_FMT_NONE}, \
.long_name = NULL_IF_CONFIG_SMALL(long_name_), \
};
#else
#define PCM_DECODER(id,sample_fmt_,name,long_name_)
#endif
PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le);
PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be);
PCM_CODEC(CODEC_ID_PCM_U16LE, pcm_u16le);
PCM_CODEC(CODEC_ID_PCM_U16BE, pcm_u16be);
PCM_CODEC(CODEC_ID_PCM_S8, pcm_s8);
PCM_CODEC(CODEC_ID_PCM_U8, pcm_u8);
PCM_CODEC(CODEC_ID_PCM_ALAW, pcm_alaw);
PCM_CODEC(CODEC_ID_PCM_MULAW, pcm_mulaw);
#define PCM_CODEC(id, sample_fmt_, name, long_name_) \
PCM_ENCODER(id,sample_fmt_,name,long_name_) PCM_DECODER(id,sample_fmt_,name,long_name_)
#undef PCM_CODEC
/* Note: Do not forget to add new entries to the Makefile as well. */
PCM_CODEC (CODEC_ID_PCM_ALAW, SAMPLE_FMT_S16, pcm_alaw, "A-law PCM");
PCM_CODEC (CODEC_ID_PCM_DVD, SAMPLE_FMT_S32, pcm_dvd, "signed 20|24-bit big-endian PCM");
PCM_CODEC (CODEC_ID_PCM_F32BE, SAMPLE_FMT_FLT, pcm_f32be, "32-bit floating point big-endian PCM");
PCM_CODEC (CODEC_ID_PCM_F32LE, SAMPLE_FMT_FLT, pcm_f32le, "32-bit floating point little-endian PCM");
PCM_CODEC (CODEC_ID_PCM_F64BE, SAMPLE_FMT_DBL, pcm_f64be, "64-bit floating point big-endian PCM");
PCM_CODEC (CODEC_ID_PCM_F64LE, SAMPLE_FMT_DBL, pcm_f64le, "64-bit floating point little-endian PCM");
PCM_CODEC (CODEC_ID_PCM_MULAW, SAMPLE_FMT_S16, pcm_mulaw, "mu-law PCM");
PCM_CODEC (CODEC_ID_PCM_S8, SAMPLE_FMT_U8, pcm_s8, "signed 8-bit PCM");
PCM_CODEC (CODEC_ID_PCM_S16BE, SAMPLE_FMT_S16, pcm_s16be, "signed 16-bit big-endian PCM");
PCM_CODEC (CODEC_ID_PCM_S16LE, SAMPLE_FMT_S16, pcm_s16le, "signed 16-bit little-endian PCM");
PCM_DECODER(CODEC_ID_PCM_S16LE_PLANAR, SAMPLE_FMT_S16, pcm_s16le_planar, "16-bit little-endian planar PCM");
PCM_CODEC (CODEC_ID_PCM_S24BE, SAMPLE_FMT_S32, pcm_s24be, "signed 24-bit big-endian PCM");
PCM_CODEC (CODEC_ID_PCM_S24DAUD, SAMPLE_FMT_S16, pcm_s24daud, "D-Cinema audio signed 24-bit PCM");
PCM_CODEC (CODEC_ID_PCM_S24LE, SAMPLE_FMT_S32, pcm_s24le, "signed 24-bit little-endian PCM");
PCM_CODEC (CODEC_ID_PCM_S32BE, SAMPLE_FMT_S32, pcm_s32be, "signed 32-bit big-endian PCM");
PCM_CODEC (CODEC_ID_PCM_S32LE, SAMPLE_FMT_S32, pcm_s32le, "signed 32-bit little-endian PCM");
PCM_CODEC (CODEC_ID_PCM_U8, SAMPLE_FMT_U8, pcm_u8, "unsigned 8-bit PCM");
PCM_CODEC (CODEC_ID_PCM_U16BE, SAMPLE_FMT_S16, pcm_u16be, "unsigned 16-bit big-endian PCM");
PCM_CODEC (CODEC_ID_PCM_U16LE, SAMPLE_FMT_S16, pcm_u16le, "unsigned 16-bit little-endian PCM");
PCM_CODEC (CODEC_ID_PCM_U24BE, SAMPLE_FMT_S32, pcm_u24be, "unsigned 24-bit big-endian PCM");
PCM_CODEC (CODEC_ID_PCM_U24LE, SAMPLE_FMT_S32, pcm_u24le, "unsigned 24-bit little-endian PCM");
PCM_CODEC (CODEC_ID_PCM_U32BE, SAMPLE_FMT_S32, pcm_u32be, "unsigned 32-bit big-endian PCM");
PCM_CODEC (CODEC_ID_PCM_U32LE, SAMPLE_FMT_S32, pcm_u32le, "unsigned 32-bit little-endian PCM");
PCM_CODEC (CODEC_ID_PCM_ZORK, SAMPLE_FMT_S16, pcm_zork, "Zork PCM");
@@ -0,0 +1,248 @@
/*
* PC Paintbrush PCX (.pcx) image decoder
* Copyright (c) 2007, 2008 Ivo van Poorten
*
* This decoder does not support CGA palettes. I am unable to find samples
* and Netpbm cannot generate them.
*
* 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 "avcodec.h"
#include "bytestream.h"
#include "bitstream.h"
typedef struct PCXContext {
AVFrame picture;
} PCXContext;
static av_cold int pcx_init(AVCodecContext *avctx) {
PCXContext *s = avctx->priv_data;
avcodec_get_frame_defaults(&s->picture);
avctx->coded_frame= &s->picture;
return 0;
}
/**
* @return advanced src pointer
*/
static const uint8_t *pcx_rle_decode(const uint8_t *src, uint8_t *dst,
unsigned int bytes_per_scanline) {
unsigned int i = 0;
unsigned char run, value;
while (i<bytes_per_scanline) {
run = 1;
value = *src++;
if (value >= 0xc0) {
run = value & 0x3f;
value = *src++;
}
while (i<bytes_per_scanline && run--)
dst[i++] = value;
}
return src;
}
static void pcx_palette(const uint8_t **src, uint32_t *dst, unsigned int pallen) {
unsigned int i;
for (i=0; i<pallen; i++)
*dst++ = bytestream_get_be24(src);
memset(dst, 0, (256 - pallen) * sizeof(*dst));
}
static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
const uint8_t *buf, int buf_size) {
PCXContext * const s = avctx->priv_data;
AVFrame *picture = data;
AVFrame * const p = &s->picture;
int xmin, ymin, xmax, ymax;
unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, stride, y, x,
bytes_per_scanline;
uint8_t *ptr;
uint8_t const *bufstart = buf;
if (buf[0] != 0x0a || buf[1] > 5 || buf[1] == 1 || buf[2] != 1) {
av_log(avctx, AV_LOG_ERROR, "this is not PCX encoded data\n");
return -1;
}
xmin = AV_RL16(buf+ 4);
ymin = AV_RL16(buf+ 6);
xmax = AV_RL16(buf+ 8);
ymax = AV_RL16(buf+10);
if (xmax < xmin || ymax < ymin) {
av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
return -1;
}
w = xmax - xmin + 1;
h = ymax - ymin + 1;
bits_per_pixel = buf[3];
bytes_per_line = AV_RL16(buf+66);
nplanes = buf[65];
bytes_per_scanline = nplanes * bytes_per_line;
if (bytes_per_scanline < w * bits_per_pixel * nplanes / 8) {
av_log(avctx, AV_LOG_ERROR, "PCX data is corrupted\n");
return -1;
}
switch ((nplanes<<8) + bits_per_pixel) {
case 0x0308:
avctx->pix_fmt = PIX_FMT_RGB24;
break;
case 0x0108:
case 0x0104:
case 0x0102:
case 0x0101:
case 0x0401:
case 0x0301:
case 0x0201:
avctx->pix_fmt = PIX_FMT_PAL8;
break;
default:
av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
return -1;
}
buf += 128;
if (p->data[0])
avctx->release_buffer(avctx, p);
if (avcodec_check_dimensions(avctx, w, h))
return -1;
if (w != avctx->width || h != avctx->height)
avcodec_set_dimensions(avctx, w, h);
if (avctx->get_buffer(avctx, p) < 0) {
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return -1;
}
p->pict_type = FF_I_TYPE;
ptr = p->data[0];
stride = p->linesize[0];
if (nplanes == 3 && bits_per_pixel == 8) {
uint8_t scanline[bytes_per_scanline];
for (y=0; y<h; y++) {
buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
for (x=0; x<w; x++) {
ptr[3*x ] = scanline[x ];
ptr[3*x+1] = scanline[x+ bytes_per_line ];
ptr[3*x+2] = scanline[x+(bytes_per_line<<1)];
}
ptr += stride;
}
} else if (nplanes == 1 && bits_per_pixel == 8) {
uint8_t scanline[bytes_per_scanline];
const uint8_t *palstart = bufstart + buf_size - 769;
for (y=0; y<h; y++, ptr+=stride) {
buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
memcpy(ptr, scanline, w);
}
if (buf != palstart) {
av_log(avctx, AV_LOG_WARNING, "image data possibly corrupted\n");
buf = palstart;
}
if (*buf++ != 12) {
av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
return -1;
}
} else if (nplanes == 1) { /* all packed formats, max. 16 colors */
uint8_t scanline[bytes_per_scanline];
GetBitContext s;
for (y=0; y<h; y++) {
init_get_bits(&s, scanline, bytes_per_scanline<<3);
buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
for (x=0; x<w; x++)
ptr[x] = get_bits(&s, bits_per_pixel);
ptr += stride;
}
} else { /* planar, 4, 8 or 16 colors */
uint8_t scanline[bytes_per_scanline];
int i;
for (y=0; y<h; y++) {
buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
for (x=0; x<w; x++) {
int m = 0x80 >> (x&7), v = 0;
for (i=nplanes - 1; i>=0; i--) {
v <<= 1;
v += !!(scanline[i*bytes_per_line + (x>>3)] & m);
}
ptr[x] = v;
}
ptr += stride;
}
}
if (nplanes == 1 && bits_per_pixel == 8) {
pcx_palette(&buf, (uint32_t *) p->data[1], 256);
} else if (bits_per_pixel < 8) {
const uint8_t *palette = bufstart+16;
pcx_palette(&palette, (uint32_t *) p->data[1], 16);
}
*picture = s->picture;
*data_size = sizeof(AVFrame);
return buf - bufstart;
}
static av_cold int pcx_end(AVCodecContext *avctx) {
PCXContext *s = avctx->priv_data;
if(s->picture.data[0])
avctx->release_buffer(avctx, &s->picture);
return 0;
}
AVCodec pcx_decoder = {
"pcx",
CODEC_TYPE_VIDEO,
CODEC_ID_PCX,
sizeof(PCXContext),
pcx_init,
NULL,
pcx_end,
pcx_decode_frame,
0,
NULL,
.long_name = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),
};
@@ -0,0 +1,83 @@
/*
* PNG image format
* Copyright (c) 2003 Fabrice Bellard.
*
* 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 "avcodec.h"
#include "bytestream.h"
#include "png.h"
const uint8_t ff_pngsig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
const uint8_t ff_mngsig[8] = {138, 77, 78, 71, 13, 10, 26, 10};
/* Mask to determine which y pixels are valid in a pass */
const uint8_t ff_png_pass_ymask[NB_PASSES] = {
0x80, 0x80, 0x08, 0x88, 0x22, 0xaa, 0x55,
};
/* minimum x value */
const uint8_t ff_png_pass_xmin[NB_PASSES] = {
0, 4, 0, 2, 0, 1, 0
};
/* x shift to get row width */
const uint8_t ff_png_pass_xshift[NB_PASSES] = {
3, 3, 2, 2, 1, 1, 0
};
/* Mask to determine which pixels are valid in a pass */
const uint8_t ff_png_pass_mask[NB_PASSES] = {
0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff
};
void *ff_png_zalloc(void *opaque, unsigned int items, unsigned int size)
{
if(items >= UINT_MAX / size)
return NULL;
return av_malloc(items * size);
}
void ff_png_zfree(void *opaque, void *ptr)
{
av_free(ptr);
}
int ff_png_get_nb_channels(int color_type)
{
int channels;
channels = 1;
if ((color_type & (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)) ==
PNG_COLOR_MASK_COLOR)
channels = 3;
if (color_type & PNG_COLOR_MASK_ALPHA)
channels++;
return channels;
}
/* compute the row size of an interleaved pass */
int ff_png_pass_row_size(int pass, int bits_per_pixel, int width)
{
int shift, xmin, pass_width;
xmin = ff_png_pass_xmin[pass];
if (width <= xmin)
return 0;
shift = ff_png_pass_xshift[pass];
pass_width = (width - xmin + (1 << shift) - 1) >> shift;
return (pass_width * bits_per_pixel + 7) >> 3;
}
@@ -0,0 +1,77 @@
/*
* PNG image format
* Copyright (c) 2003 Fabrice Bellard.
*
* 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_PNG_H
#define FFMPEG_PNG_H
#include <stdint.h>
#define PNG_COLOR_MASK_PALETTE 1
#define PNG_COLOR_MASK_COLOR 2
#define PNG_COLOR_MASK_ALPHA 4
#define PNG_COLOR_TYPE_GRAY 0
#define PNG_COLOR_TYPE_PALETTE (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
#define PNG_COLOR_TYPE_RGB (PNG_COLOR_MASK_COLOR)
#define PNG_COLOR_TYPE_RGB_ALPHA (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
#define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
#define PNG_FILTER_TYPE_LOCO 64
#define PNG_FILTER_VALUE_NONE 0
#define PNG_FILTER_VALUE_SUB 1
#define PNG_FILTER_VALUE_UP 2
#define PNG_FILTER_VALUE_AVG 3
#define PNG_FILTER_VALUE_PAETH 4
#define PNG_FILTER_VALUE_MIXED 5
#define PNG_IHDR 0x0001
#define PNG_IDAT 0x0002
#define PNG_ALLIMAGE 0x0004
#define PNG_PLTE 0x0008
#define NB_PASSES 7
extern const uint8_t ff_pngsig[8];
extern const uint8_t ff_mngsig[8];
/* Mask to determine which y pixels are valid in a pass */
extern const uint8_t ff_png_pass_ymask[NB_PASSES];
/* minimum x value */
extern const uint8_t ff_png_pass_xmin[NB_PASSES];
/* x shift to get row width */
extern const uint8_t ff_png_pass_xshift[NB_PASSES];
/* Mask to determine which pixels are valid in a pass */
extern const uint8_t ff_png_pass_mask[NB_PASSES];
extern void *ff_png_zalloc(void *opaque, unsigned int items,
unsigned int size);
extern void ff_png_zfree(void *opaque, void *ptr);
extern int ff_png_get_nb_channels(int color_type);
/* compute the row size of an interleaved pass */
extern int ff_png_pass_row_size(int pass, int bits_per_pixel, int width);
#endif /* FFMPEG_PNG_H */
@@ -0,0 +1,622 @@
/*
* PNG image format
* Copyright (c) 2003 Fabrice Bellard.
*
* 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 "avcodec.h"
#include "bytestream.h"
#include "png.h"
#include "dsputil.h"
/* TODO:
* - add 2, 4 and 16 bit depth support
*/
#include <zlib.h>
//#define DEBUG
typedef struct PNGDecContext {
DSPContext dsp;
const uint8_t *bytestream;
const uint8_t *bytestream_start;
const uint8_t *bytestream_end;
AVFrame picture;
int state;
int width, height;
int bit_depth;
int color_type;
int compression_type;
int interlace_type;
int filter_type;
int channels;
int bits_per_pixel;
int bpp;
uint8_t *image_buf;
int image_linesize;
uint32_t palette[256];
uint8_t *crow_buf;
uint8_t *last_row;
uint8_t *tmp_row;
int pass;
int crow_size; /* compressed row size (include filter type) */
int row_size; /* decompressed row size */
int pass_row_size; /* decompress row size of the current pass */
int y;
z_stream zstream;
} PNGDecContext;
/* Mask to determine which y pixels can be written in a pass */
static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
};
/* Mask to determine which pixels to overwrite while displaying */
static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
};
/* NOTE: we try to construct a good looking image at each pass. width
is the original image width. We also do pixel format conversion at
this stage */
static void png_put_interlaced_row(uint8_t *dst, int width,
int bits_per_pixel, int pass,
int color_type, const uint8_t *src)
{
int x, mask, dsp_mask, j, src_x, b, bpp;
uint8_t *d;
const uint8_t *s;
mask = ff_png_pass_mask[pass];
dsp_mask = png_pass_dsp_mask[pass];
switch(bits_per_pixel) {
case 1:
/* we must initialize the line to zero before writing to it */
if (pass == 0)
memset(dst, 0, (width + 7) >> 3);
src_x = 0;
for(x = 0; x < width; x++) {
j = (x & 7);
if ((dsp_mask << j) & 0x80) {
b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
dst[x >> 3] |= b << (7 - j);
}
if ((mask << j) & 0x80)
src_x++;
}
break;
default:
bpp = bits_per_pixel >> 3;
d = dst;
s = src;
if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
for(x = 0; x < width; x++) {
j = x & 7;
if ((dsp_mask << j) & 0x80) {
*(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
}
d += bpp;
if ((mask << j) & 0x80)
s += bpp;
}
} else {
for(x = 0; x < width; x++) {
j = x & 7;
if ((dsp_mask << j) & 0x80) {
memcpy(d, s, bpp);
}
d += bpp;
if ((mask << j) & 0x80)
s += bpp;
}
}
break;
}
}
void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
{
int i;
for(i = 0; i < w; i++) {
int a, b, c, p, pa, pb, pc;
a = dst[i - bpp];
b = top[i];
c = top[i - bpp];
p = b - c;
pc = a - c;
pa = abs(p);
pb = abs(pc);
pc = abs(p + pc);
if (pa <= pb && pa <= pc)
p = a;
else if (pb <= pc)
p = b;
else
p = c;
dst[i] = p + src[i];
}
}
#define UNROLL1(bpp, op) {\
r = dst[0];\
if(bpp >= 2) g = dst[1];\
if(bpp >= 3) b = dst[2];\
if(bpp >= 4) a = dst[3];\
for(; i < size; i+=bpp) {\
dst[i+0] = r = op(r, src[i+0], last[i+0]);\
if(bpp == 1) continue;\
dst[i+1] = g = op(g, src[i+1], last[i+1]);\
if(bpp == 2) continue;\
dst[i+2] = b = op(b, src[i+2], last[i+2]);\
if(bpp == 3) continue;\
dst[i+3] = a = op(a, src[i+3], last[i+3]);\
}\
}
#define UNROLL_FILTER(op)\
if(bpp == 1) UNROLL1(1, op)\
else if(bpp == 2) UNROLL1(2, op)\
else if(bpp == 3) UNROLL1(3, op)\
else if(bpp == 4) UNROLL1(4, op)\
/* NOTE: 'dst' can be equal to 'last' */
static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type,
uint8_t *src, uint8_t *last, int size, int bpp)
{
int i, p, r, g, b, a;
switch(filter_type) {
case PNG_FILTER_VALUE_NONE:
memcpy(dst, src, size);
break;
case PNG_FILTER_VALUE_SUB:
for(i = 0; i < bpp; i++) {
dst[i] = src[i];
}
if(bpp == 4) {
p = *(int*)dst;
for(; i < size; i+=bpp) {
int s = *(int*)(src+i);
p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
*(int*)(dst+i) = p;
}
} else {
#define OP_SUB(x,s,l) x+s
UNROLL_FILTER(OP_SUB);
}
break;
case PNG_FILTER_VALUE_UP:
dsp->add_bytes_l2(dst, src, last, size);
break;
case PNG_FILTER_VALUE_AVG:
for(i = 0; i < bpp; i++) {
p = (last[i] >> 1);
dst[i] = p + src[i];
}
#define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
UNROLL_FILTER(OP_AVG);
break;
case PNG_FILTER_VALUE_PAETH:
for(i = 0; i < bpp; i++) {
p = last[i];
dst[i] = p + src[i];
}
if(bpp > 1 && size > 4) {
// would write off the end of the array if we let it process the last pixel with bpp=3
int w = bpp==4 ? size : size-3;
dsp->add_png_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
i = w;
}
ff_add_png_paeth_prediction(dst+i, src+i, last+i, size-i, bpp);
break;
}
}
static av_always_inline void convert_to_rgb32_loco(uint8_t *dst, const uint8_t *src, int width, int loco)
{
int j;
unsigned int r, g, b, a;
for(j = 0;j < width; j++) {
r = src[0];
g = src[1];
b = src[2];
a = src[3];
if(loco) {
r = (r+g)&0xff;
b = (b+g)&0xff;
}
*(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
dst += 4;
src += 4;
}
}
static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width, int loco)
{
if(loco)
convert_to_rgb32_loco(dst, src, width, 1);
else
convert_to_rgb32_loco(dst, src, width, 0);
}
static void deloco_rgb24(uint8_t *dst, int size)
{
int i;
for(i=0; i<size; i+=3) {
int g = dst[i+1];
dst[i+0] += g;
dst[i+2] += g;
}
}
/* process exactly one decompressed row */
static void png_handle_row(PNGDecContext *s)
{
uint8_t *ptr, *last_row;
int got_line;
if (!s->interlace_type) {
ptr = s->image_buf + s->image_linesize * s->y;
/* need to swap bytes correctly for RGB_ALPHA */
if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
s->last_row, s->row_size, s->bpp);
convert_to_rgb32(ptr, s->tmp_row, s->width, s->filter_type == PNG_FILTER_TYPE_LOCO);
FFSWAP(uint8_t*, s->last_row, s->tmp_row);
} else {
/* in normal case, we avoid one copy */
if (s->y == 0)
last_row = s->last_row;
else
last_row = ptr - s->image_linesize;
png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
last_row, s->row_size, s->bpp);
}
/* loco lags by 1 row so that it doesn't interfere with top prediction */
if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
s->color_type == PNG_COLOR_TYPE_RGB && s->y > 0)
deloco_rgb24(ptr - s->image_linesize, s->row_size);
s->y++;
if (s->y == s->height) {
s->state |= PNG_ALLIMAGE;
if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
s->color_type == PNG_COLOR_TYPE_RGB)
deloco_rgb24(ptr, s->row_size);
}
} else {
got_line = 0;
for(;;) {
ptr = s->image_buf + s->image_linesize * s->y;
if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
/* if we already read one row, it is time to stop to
wait for the next one */
if (got_line)
break;
png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
s->last_row, s->pass_row_size, s->bpp);
FFSWAP(uint8_t*, s->last_row, s->tmp_row);
got_line = 1;
}
if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
/* NOTE: RGB32 is handled directly in png_put_interlaced_row */
png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
s->color_type, s->last_row);
}
s->y++;
if (s->y == s->height) {
for(;;) {
if (s->pass == NB_PASSES - 1) {
s->state |= PNG_ALLIMAGE;
goto the_end;
} else {
s->pass++;
s->y = 0;
s->pass_row_size = ff_png_pass_row_size(s->pass,
s->bits_per_pixel,
s->width);
s->crow_size = s->pass_row_size + 1;
if (s->pass_row_size != 0)
break;
/* skip pass if empty row */
}
}
}
}
the_end: ;
}
}
static int png_decode_idat(PNGDecContext *s, int length)
{
int ret;
s->zstream.avail_in = length;
s->zstream.next_in = s->bytestream;
s->bytestream += length;
if(s->bytestream > s->bytestream_end)
return -1;
/* decode one line if possible */
while (s->zstream.avail_in > 0) {
ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
if (ret != Z_OK && ret != Z_STREAM_END) {
return -1;
}
if (s->zstream.avail_out == 0) {
if (!(s->state & PNG_ALLIMAGE)) {
png_handle_row(s);
}
s->zstream.avail_out = s->crow_size;
s->zstream.next_out = s->crow_buf;
}
}
return 0;
}
static int decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
const uint8_t *buf, int buf_size)
{
PNGDecContext * const s = avctx->priv_data;
AVFrame *picture = data;
AVFrame * const p= &s->picture;
uint32_t tag, length;
int ret, crc;
s->bytestream_start=
s->bytestream= buf;
s->bytestream_end= buf + buf_size;
/* check signature */
if (memcmp(s->bytestream, ff_pngsig, 8) != 0 &&
memcmp(s->bytestream, ff_mngsig, 8) != 0)
return -1;
s->bytestream+= 8;
s->y=
s->state=0;
// memset(s, 0, sizeof(PNGDecContext));
/* init the zlib */
s->zstream.zalloc = ff_png_zalloc;
s->zstream.zfree = ff_png_zfree;
s->zstream.opaque = NULL;
ret = inflateInit(&s->zstream);
if (ret != Z_OK)
return -1;
for(;;) {
int tag32;
if (s->bytestream >= s->bytestream_end)
goto fail;
length = bytestream_get_be32(&s->bytestream);
if (length > 0x7fffffff)
goto fail;
tag32 = bytestream_get_be32(&s->bytestream);
tag = bswap_32(tag32);
#ifdef DEBUG
av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
(tag & 0xff),
((tag >> 8) & 0xff),
((tag >> 16) & 0xff),
((tag >> 24) & 0xff), length);
#endif
switch(tag) {
case MKTAG('I', 'H', 'D', 'R'):
if (length != 13)
goto fail;
s->width = bytestream_get_be32(&s->bytestream);
s->height = bytestream_get_be32(&s->bytestream);
if(avcodec_check_dimensions(avctx, s->width, s->height)){
s->width= s->height= 0;
goto fail;
}
s->bit_depth = *s->bytestream++;
s->color_type = *s->bytestream++;
s->compression_type = *s->bytestream++;
s->filter_type = *s->bytestream++;
s->interlace_type = *s->bytestream++;
crc = bytestream_get_be32(&s->bytestream);
s->state |= PNG_IHDR;
#ifdef DEBUG
av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
s->width, s->height, s->bit_depth, s->color_type,
s->compression_type, s->filter_type, s->interlace_type);
#endif
break;
case MKTAG('I', 'D', 'A', 'T'):
if (!(s->state & PNG_IHDR))
goto fail;
if (!(s->state & PNG_IDAT)) {
/* init image info */
avctx->width = s->width;
avctx->height = s->height;
s->channels = ff_png_get_nb_channels(s->color_type);
s->bits_per_pixel = s->bit_depth * s->channels;
s->bpp = (s->bits_per_pixel + 7) >> 3;
s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
if (s->bit_depth == 8 &&
s->color_type == PNG_COLOR_TYPE_RGB) {
avctx->pix_fmt = PIX_FMT_RGB24;
} else if (s->bit_depth == 8 &&
s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
avctx->pix_fmt = PIX_FMT_RGB32;
} else if (s->bit_depth == 8 &&
s->color_type == PNG_COLOR_TYPE_GRAY) {
avctx->pix_fmt = PIX_FMT_GRAY8;
} else if (s->bit_depth == 16 &&
s->color_type == PNG_COLOR_TYPE_GRAY) {
avctx->pix_fmt = PIX_FMT_GRAY16BE;
} else if (s->bit_depth == 1 &&
s->color_type == PNG_COLOR_TYPE_GRAY) {
avctx->pix_fmt = PIX_FMT_MONOBLACK;
} else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
avctx->pix_fmt = PIX_FMT_PAL8;
} else {
goto fail;
}
if(p->data[0])
avctx->release_buffer(avctx, p);
p->reference= 0;
if(avctx->get_buffer(avctx, p) < 0){
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
goto fail;
}
p->pict_type= FF_I_TYPE;
p->key_frame= 1;
p->interlaced_frame = !!s->interlace_type;
/* compute the compressed row size */
if (!s->interlace_type) {
s->crow_size = s->row_size + 1;
} else {
s->pass = 0;
s->pass_row_size = ff_png_pass_row_size(s->pass,
s->bits_per_pixel,
s->width);
s->crow_size = s->pass_row_size + 1;
}
#ifdef DEBUG
av_log(avctx, AV_LOG_DEBUG, "row_size=%d crow_size =%d\n",
s->row_size, s->crow_size);
#endif
s->image_buf = p->data[0];
s->image_linesize = p->linesize[0];
/* copy the palette if needed */
if (s->color_type == PNG_COLOR_TYPE_PALETTE)
memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
/* empty row is used if differencing to the first row */
s->last_row = av_mallocz(s->row_size);
if (!s->last_row)
goto fail;
if (s->interlace_type ||
s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
s->tmp_row = av_malloc(s->row_size);
if (!s->tmp_row)
goto fail;
}
/* compressed row */
s->crow_buf = av_malloc(s->row_size + 1);
if (!s->crow_buf)
goto fail;
s->zstream.avail_out = s->crow_size;
s->zstream.next_out = s->crow_buf;
}
s->state |= PNG_IDAT;
if (png_decode_idat(s, length) < 0)
goto fail;
/* skip crc */
crc = bytestream_get_be32(&s->bytestream);
break;
case MKTAG('P', 'L', 'T', 'E'):
{
int n, i, r, g, b;
if ((length % 3) != 0 || length > 256 * 3)
goto skip_tag;
/* read the palette */
n = length / 3;
for(i=0;i<n;i++) {
r = *s->bytestream++;
g = *s->bytestream++;
b = *s->bytestream++;
s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
}
for(;i<256;i++) {
s->palette[i] = (0xff << 24);
}
s->state |= PNG_PLTE;
crc = bytestream_get_be32(&s->bytestream);
}
break;
case MKTAG('t', 'R', 'N', 'S'):
{
int v, i;
/* read the transparency. XXX: Only palette mode supported */
if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
length > 256 ||
!(s->state & PNG_PLTE))
goto skip_tag;
for(i=0;i<length;i++) {
v = *s->bytestream++;
s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
}
crc = bytestream_get_be32(&s->bytestream);
}
break;
case MKTAG('I', 'E', 'N', 'D'):
if (!(s->state & PNG_ALLIMAGE))
goto fail;
crc = bytestream_get_be32(&s->bytestream);
goto exit_loop;
default:
/* skip tag */
skip_tag:
s->bytestream += length + 4;
break;
}
}
exit_loop:
*picture= s->picture;
*data_size = sizeof(AVFrame);
ret = s->bytestream - s->bytestream_start;
the_end:
inflateEnd(&s->zstream);
av_freep(&s->crow_buf);
av_freep(&s->last_row);
av_freep(&s->tmp_row);
return ret;
fail:
ret = -1;
goto the_end;
}
static av_cold int png_dec_init(AVCodecContext *avctx){
PNGDecContext *s = avctx->priv_data;
avcodec_get_frame_defaults(&s->picture);
avctx->coded_frame= &s->picture;
dsputil_init(&s->dsp, avctx);
return 0;
}
AVCodec png_decoder = {
"png",
CODEC_TYPE_VIDEO,
CODEC_ID_PNG,
sizeof(PNGDecContext),
png_dec_init,
NULL,
NULL, //decode_end,
decode_frame,
0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
NULL,
.long_name = NULL_IF_CONFIG_SMALL("PNG image"),
};
@@ -0,0 +1,449 @@
/*
* PNG image format
* Copyright (c) 2003 Fabrice Bellard.
*
* 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 "avcodec.h"
#include "bytestream.h"
#include "dsputil.h"
#include "png.h"
/* TODO:
* - add 2, 4 and 16 bit depth support
*/
#include <zlib.h>
//#define DEBUG
#define IOBUF_SIZE 4096
typedef struct PNGEncContext {
DSPContext dsp;
uint8_t *bytestream;
uint8_t *bytestream_start;
uint8_t *bytestream_end;
AVFrame picture;
int filter_type;
z_stream zstream;
uint8_t buf[IOBUF_SIZE];
} PNGEncContext;
static void png_get_interlaced_row(uint8_t *dst, int row_size,
int bits_per_pixel, int pass,
const uint8_t *src, int width)
{
int x, mask, dst_x, j, b, bpp;
uint8_t *d;
const uint8_t *s;
mask = ff_png_pass_mask[pass];
switch(bits_per_pixel) {
case 1:
memset(dst, 0, row_size);
dst_x = 0;
for(x = 0; x < width; x++) {
j = (x & 7);
if ((mask << j) & 0x80) {
b = (src[x >> 3] >> (7 - j)) & 1;
dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
dst_x++;
}
}
break;
default:
bpp = bits_per_pixel >> 3;
d = dst;
s = src;
for(x = 0; x < width; x++) {
j = x & 7;
if ((mask << j) & 0x80) {
memcpy(d, s, bpp);
d += bpp;
}
s += bpp;
}
break;
}
}
static void sub_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
{
int i;
for(i = 0; i < w; i++) {
int a, b, c, p, pa, pb, pc;
a = src[i - bpp];
b = top[i];
c = top[i - bpp];
p = b - c;
pc = a - c;
pa = abs(p);
pb = abs(pc);
pc = abs(p + pc);
if (pa <= pb && pa <= pc)
p = a;
else if (pb <= pc)
p = b;
else
p = c;
dst[i] = src[i] - p;
}
}
static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type,
uint8_t *src, uint8_t *top, int size, int bpp)
{
int i;
switch(filter_type) {
case PNG_FILTER_VALUE_NONE:
memcpy(dst, src, size);
break;
case PNG_FILTER_VALUE_SUB:
dsp->diff_bytes(dst, src, src-bpp, size);
memcpy(dst, src, bpp);
break;
case PNG_FILTER_VALUE_UP:
dsp->diff_bytes(dst, src, top, size);
break;
case PNG_FILTER_VALUE_AVG:
for(i = 0; i < bpp; i++)
dst[i] = src[i] - (top[i] >> 1);
for(; i < size; i++)
dst[i] = src[i] - ((src[i-bpp] + top[i]) >> 1);
break;
case PNG_FILTER_VALUE_PAETH:
for(i = 0; i < bpp; i++)
dst[i] = src[i] - top[i];
sub_png_paeth_prediction(dst+i, src+i, top+i, size-i, bpp);
break;
}
}
static uint8_t *png_choose_filter(PNGEncContext *s, uint8_t *dst,
uint8_t *src, uint8_t *top, int size, int bpp)
{
int pred = s->filter_type;
assert(bpp || !pred);
if(!top && pred)
pred = PNG_FILTER_VALUE_SUB;
if(pred == PNG_FILTER_VALUE_MIXED) {
int i;
int cost, bcost = INT_MAX;
uint8_t *buf1 = dst, *buf2 = dst + size + 16;
for(pred=0; pred<5; pred++) {
png_filter_row(&s->dsp, buf1+1, pred, src, top, size, bpp);
buf1[0] = pred;
cost = 0;
for(i=0; i<=size; i++)
cost += abs((int8_t)buf1[i]);
if(cost < bcost) {
bcost = cost;
FFSWAP(uint8_t*, buf1, buf2);
}
}
return buf2;
} else {
png_filter_row(&s->dsp, dst+1, pred, src, top, size, bpp);
dst[0] = pred;
return dst;
}
}
static void convert_from_rgb32(uint8_t *dst, const uint8_t *src, int width)
{
uint8_t *d;
int j;
unsigned int v;
d = dst;
for(j = 0; j < width; j++) {
v = ((const uint32_t *)src)[j];
d[0] = v >> 16;
d[1] = v >> 8;
d[2] = v;
d[3] = v >> 24;
d += 4;
}
}
static void png_write_chunk(uint8_t **f, uint32_t tag,
const uint8_t *buf, int length)
{
uint32_t crc;
uint8_t tagbuf[4];
bytestream_put_be32(f, length);
crc = crc32(0, Z_NULL, 0);
AV_WL32(tagbuf, tag);
crc = crc32(crc, tagbuf, 4);
bytestream_put_be32(f, bswap_32(tag));
if (length > 0) {
crc = crc32(crc, buf, length);
memcpy(*f, buf, length);
*f += length;
}
bytestream_put_be32(f, crc);
}
/* XXX: do filtering */
static int png_write_row(PNGEncContext *s, const uint8_t *data, int size)
{
int ret;
s->zstream.avail_in = size;
s->zstream.next_in = (uint8_t *)data;
while (s->zstream.avail_in > 0) {
ret = deflate(&s->zstream, Z_NO_FLUSH);
if (ret != Z_OK)
return -1;
if (s->zstream.avail_out == 0) {
if(s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
s->zstream.avail_out = IOBUF_SIZE;
s->zstream.next_out = s->buf;
}
}
return 0;
}
static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
PNGEncContext *s = avctx->priv_data;
AVFrame *pict = data;
AVFrame * const p= &s->picture;
int bit_depth, color_type, y, len, row_size, ret, is_progressive;
int bits_per_pixel, pass_row_size;
int compression_level;
uint8_t *ptr, *top;
uint8_t *crow_base = NULL, *crow_buf, *crow;
uint8_t *progressive_buf = NULL;
uint8_t *rgba_buf = NULL;
uint8_t *top_buf = NULL;
*p = *pict;
p->pict_type= FF_I_TYPE;
p->key_frame= 1;
s->bytestream_start=
s->bytestream= buf;
s->bytestream_end= buf+buf_size;
is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
switch(avctx->pix_fmt) {
case PIX_FMT_RGB32:
bit_depth = 8;
color_type = PNG_COLOR_TYPE_RGB_ALPHA;
break;
case PIX_FMT_RGB24:
bit_depth = 8;
color_type = PNG_COLOR_TYPE_RGB;
break;
case PIX_FMT_GRAY8:
bit_depth = 8;
color_type = PNG_COLOR_TYPE_GRAY;
break;
case PIX_FMT_MONOBLACK:
bit_depth = 1;
color_type = PNG_COLOR_TYPE_GRAY;
break;
case PIX_FMT_PAL8:
bit_depth = 8;
color_type = PNG_COLOR_TYPE_PALETTE;
break;
default:
return -1;
}
bits_per_pixel = ff_png_get_nb_channels(color_type) * bit_depth;
row_size = (avctx->width * bits_per_pixel + 7) >> 3;
s->zstream.zalloc = ff_png_zalloc;
s->zstream.zfree = ff_png_zfree;
s->zstream.opaque = NULL;
compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT ?
Z_DEFAULT_COMPRESSION :
av_clip(avctx->compression_level, 0, 9);
ret = deflateInit2(&s->zstream, compression_level,
Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
if (ret != Z_OK)
return -1;
crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED));
if (!crow_base)
goto fail;
crow_buf = crow_base + 15; // pixel data should be aligned, but there's a control byte before it
if (is_progressive) {
progressive_buf = av_malloc(row_size + 1);
if (!progressive_buf)
goto fail;
}
if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
rgba_buf = av_malloc(row_size + 1);
if (!rgba_buf)
goto fail;
}
if (is_progressive || color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
top_buf = av_malloc(row_size + 1);
if (!top_buf)
goto fail;
}
/* write png header */
memcpy(s->bytestream, ff_pngsig, 8);
s->bytestream += 8;
AV_WB32(s->buf, avctx->width);
AV_WB32(s->buf + 4, avctx->height);
s->buf[8] = bit_depth;
s->buf[9] = color_type;
s->buf[10] = 0; /* compression type */
s->buf[11] = 0; /* filter type */
s->buf[12] = is_progressive; /* interlace type */
png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
/* put the palette if needed */
if (color_type == PNG_COLOR_TYPE_PALETTE) {
int has_alpha, alpha, i;
unsigned int v;
uint32_t *palette;
uint8_t *alpha_ptr;
palette = (uint32_t *)p->data[1];
ptr = s->buf;
alpha_ptr = s->buf + 256 * 3;
has_alpha = 0;
for(i = 0; i < 256; i++) {
v = palette[i];
alpha = v >> 24;
if (alpha && alpha != 0xff)
has_alpha = 1;
*alpha_ptr++ = alpha;
bytestream_put_be24(&ptr, v);
}
png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
if (has_alpha) {
png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
}
}
/* now put each row */
s->zstream.avail_out = IOBUF_SIZE;
s->zstream.next_out = s->buf;
if (is_progressive) {
int pass;
for(pass = 0; pass < NB_PASSES; pass++) {
/* NOTE: a pass is completely omited if no pixels would be
output */
pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
if (pass_row_size > 0) {
top = NULL;
for(y = 0; y < avctx->height; y++) {
if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
ptr = p->data[0] + y * p->linesize[0];
FFSWAP(uint8_t*, progressive_buf, top_buf);
if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
convert_from_rgb32(rgba_buf, ptr, avctx->width);
ptr = rgba_buf;
}
png_get_interlaced_row(progressive_buf, pass_row_size,
bits_per_pixel, pass,
ptr, avctx->width);
crow = png_choose_filter(s, crow_buf, progressive_buf, top, pass_row_size, bits_per_pixel>>3);
png_write_row(s, crow, pass_row_size + 1);
top = progressive_buf;
}
}
}
}
} else {
top = NULL;
for(y = 0; y < avctx->height; y++) {
ptr = p->data[0] + y * p->linesize[0];
if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
FFSWAP(uint8_t*, rgba_buf, top_buf);
convert_from_rgb32(rgba_buf, ptr, avctx->width);
ptr = rgba_buf;
}
crow = png_choose_filter(s, crow_buf, ptr, top, row_size, bits_per_pixel>>3);
png_write_row(s, crow, row_size + 1);
top = ptr;
}
}
/* compress last bytes */
for(;;) {
ret = deflate(&s->zstream, Z_FINISH);
if (ret == Z_OK || ret == Z_STREAM_END) {
len = IOBUF_SIZE - s->zstream.avail_out;
if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
}
s->zstream.avail_out = IOBUF_SIZE;
s->zstream.next_out = s->buf;
if (ret == Z_STREAM_END)
break;
} else {
goto fail;
}
}
png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
ret = s->bytestream - s->bytestream_start;
the_end:
av_free(crow_base);
av_free(progressive_buf);
av_free(rgba_buf);
av_free(top_buf);
deflateEnd(&s->zstream);
return ret;
fail:
ret = -1;
goto the_end;
}
static av_cold int png_enc_init(AVCodecContext *avctx){
PNGEncContext *s = avctx->priv_data;
avcodec_get_frame_defaults(&s->picture);
avctx->coded_frame= &s->picture;
dsputil_init(&s->dsp, avctx);
s->filter_type = av_clip(avctx->prediction_method, PNG_FILTER_VALUE_NONE, PNG_FILTER_VALUE_MIXED);
if(avctx->pix_fmt == PIX_FMT_MONOBLACK)
s->filter_type = PNG_FILTER_VALUE_NONE;
return 0;
}
AVCodec png_encoder = {
"png",
CODEC_TYPE_VIDEO,
CODEC_ID_PNG,
sizeof(PNGEncContext),
png_enc_init,
encode_frame,
NULL, //encode_end,
.pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_PAL8, PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, PIX_FMT_NONE},
.long_name= NULL_IF_CONFIG_SMALL("PNG image"),
};
@@ -0,0 +1,147 @@
/*
* PNM image format
* Copyright (c) 2002, 2003 Fabrice Bellard.
*
* 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 "avcodec.h"
#include "pnm.h"
static inline int pnm_space(int c)
{
return c == ' ' || c == '\n' || c == '\r' || c == '\t';
}
static void pnm_get(PNMContext *sc, char *str, int buf_size)
{
char *s;
int c;
/* skip spaces and comments */
for(;;) {
c = *sc->bytestream++;
if (c == '#') {
do {
c = *sc->bytestream++;
} while (c != '\n' && sc->bytestream < sc->bytestream_end);
} else if (!pnm_space(c)) {
break;
}
}
s = str;
while (sc->bytestream < sc->bytestream_end && !pnm_space(c)) {
if ((s - str) < buf_size - 1)
*s++ = c;
c = *sc->bytestream++;
}
*s = '\0';
}
int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s){
char buf1[32], tuple_type[32];
int h, w, depth, maxval;
pnm_get(s, buf1, sizeof(buf1));
if (!strcmp(buf1, "P4")) {
avctx->pix_fmt = PIX_FMT_MONOWHITE;
} else if (!strcmp(buf1, "P5")) {
if (avctx->codec_id == CODEC_ID_PGMYUV)
avctx->pix_fmt = PIX_FMT_YUV420P;
else
avctx->pix_fmt = PIX_FMT_GRAY8;
} else if (!strcmp(buf1, "P6")) {
avctx->pix_fmt = PIX_FMT_RGB24;
} else if (!strcmp(buf1, "P7")) {
w = -1;
h = -1;
maxval = -1;
depth = -1;
tuple_type[0] = '\0';
for(;;) {
pnm_get(s, buf1, sizeof(buf1));
if (!strcmp(buf1, "WIDTH")) {
pnm_get(s, buf1, sizeof(buf1));
w = strtol(buf1, NULL, 10);
} else if (!strcmp(buf1, "HEIGHT")) {
pnm_get(s, buf1, sizeof(buf1));
h = strtol(buf1, NULL, 10);
} else if (!strcmp(buf1, "DEPTH")) {
pnm_get(s, buf1, sizeof(buf1));
depth = strtol(buf1, NULL, 10);
} else if (!strcmp(buf1, "MAXVAL")) {
pnm_get(s, buf1, sizeof(buf1));
maxval = strtol(buf1, NULL, 10);
} else if (!strcmp(buf1, "TUPLETYPE")) {
pnm_get(s, tuple_type, sizeof(tuple_type));
} else if (!strcmp(buf1, "ENDHDR")) {
break;
} else {
return -1;
}
}
/* check that all tags are present */
if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0' || avcodec_check_dimensions(avctx, w, h))
return -1;
avctx->width = w;
avctx->height = h;
if (depth == 1) {
if (maxval == 1)
avctx->pix_fmt = PIX_FMT_MONOWHITE;
else
avctx->pix_fmt = PIX_FMT_GRAY8;
} else if (depth == 3) {
avctx->pix_fmt = PIX_FMT_RGB24;
} else if (depth == 4) {
avctx->pix_fmt = PIX_FMT_RGB32;
} else {
return -1;
}
return 0;
} else {
return -1;
}
pnm_get(s, buf1, sizeof(buf1));
avctx->width = atoi(buf1);
if (avctx->width <= 0)
return -1;
pnm_get(s, buf1, sizeof(buf1));
avctx->height = atoi(buf1);
if(avcodec_check_dimensions(avctx, avctx->width, avctx->height))
return -1;
if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
pnm_get(s, buf1, sizeof(buf1));
s->maxval = atoi(buf1);
if(s->maxval >= 256 && avctx->pix_fmt == PIX_FMT_GRAY8) {
avctx->pix_fmt = PIX_FMT_GRAY16BE;
if (s->maxval != 65535)
avctx->pix_fmt = PIX_FMT_GRAY16;
}
}
/* more check if YUV420 */
if (avctx->pix_fmt == PIX_FMT_YUV420P) {
if ((avctx->width & 1) != 0)
return -1;
h = (avctx->height * 2);
if ((h % 3) != 0)
return -1;
h /= 3;
avctx->height = h;
}
return 0;
}
@@ -0,0 +1,37 @@
/*
* PNM image format
* Copyright (c) 2002, 2003 Fabrice Bellard.
*
* 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_PNM_H
#define FFMPEG_PNM_H
#include "avcodec.h"
typedef struct PNMContext {
uint8_t *bytestream;
uint8_t *bytestream_start;
uint8_t *bytestream_end;
AVFrame picture;
int maxval; ///< maximum value of a pixel
} PNMContext;
int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s);
#endif /* FFMPEG_PNM_H */
@@ -0,0 +1,93 @@
/*
* PNM image parser
* Copyright (c) 2002, 2003 Fabrice Bellard.
*
* 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 "parser.h" //for ParseContext
#include "pnm.h"
static int pnm_parse(AVCodecParserContext *s,
AVCodecContext *avctx,
const uint8_t **poutbuf, int *poutbuf_size,
const uint8_t *buf, int buf_size)
{
ParseContext *pc = s->priv_data;
PNMContext pnmctx;
int next;
for(; pc->overread>0; pc->overread--){
pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
}
retry:
if(pc->index){
pnmctx.bytestream_start=
pnmctx.bytestream= pc->buffer;
pnmctx.bytestream_end= pc->buffer + pc->index;
}else{
pnmctx.bytestream_start=
pnmctx.bytestream= (uint8_t *) buf; /* casts avoid warnings */
pnmctx.bytestream_end= (uint8_t *) buf + buf_size;
}
if(ff_pnm_decode_header(avctx, &pnmctx) < 0){
if(pnmctx.bytestream < pnmctx.bytestream_end){
if(pc->index){
pc->index=0;
}else{
buf++;
buf_size--;
}
goto retry;
}
#if 0
if(pc->index && pc->index*2 + FF_INPUT_BUFFER_PADDING_SIZE < pc->buffer_size && buf_size > pc->index){
memcpy(pc->buffer + pc->index, buf, pc->index);
pc->index += pc->index;
buf += pc->index;
buf_size -= pc->index;
goto retry;
}
#endif
next= END_NOT_FOUND;
}else{
next= pnmctx.bytestream - pnmctx.bytestream_start
+ avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
if(pnmctx.bytestream_start!=buf)
next-= pc->index;
if(next > buf_size)
next= END_NOT_FOUND;
}
if(ff_combine_frame(pc, next, &buf, &buf_size)<0){
*poutbuf = NULL;
*poutbuf_size = 0;
return buf_size;
}
*poutbuf = buf;
*poutbuf_size = buf_size;
return next;
}
AVCodecParser pnm_parser = {
{ CODEC_ID_PGM, CODEC_ID_PGMYUV, CODEC_ID_PPM, CODEC_ID_PBM, CODEC_ID_PAM},
sizeof(ParseContext),
NULL,
pnm_parse,
ff_parse_close,
};
@@ -0,0 +1,430 @@
/*
* PNM image format
* Copyright (c) 2002, 2003 Fabrice Bellard.
*
* 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 "avcodec.h"
#include "bytestream.h"
#include "pnm.h"
static av_cold int common_init(AVCodecContext *avctx){
PNMContext *s = avctx->priv_data;
avcodec_get_frame_defaults((AVFrame*)&s->picture);
avctx->coded_frame= (AVFrame*)&s->picture;
return 0;
}
static int pnm_decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
const uint8_t *buf, int buf_size)
{
PNMContext * const s = avctx->priv_data;
AVFrame *picture = data;
AVFrame * const p= (AVFrame*)&s->picture;
int i, n, linesize, h, upgrade = 0;
unsigned char *ptr;
s->bytestream_start=
s->bytestream= buf;
s->bytestream_end= buf + buf_size;
if(ff_pnm_decode_header(avctx, s) < 0)
return -1;
if(p->data[0])
avctx->release_buffer(avctx, p);
p->reference= 0;
if(avctx->get_buffer(avctx, p) < 0){
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return -1;
}
p->pict_type= FF_I_TYPE;
p->key_frame= 1;
switch(avctx->pix_fmt) {
default:
return -1;
case PIX_FMT_RGB24:
n = avctx->width * 3;
goto do_read;
case PIX_FMT_GRAY8:
n = avctx->width;
if (s->maxval < 255)
upgrade = 1;
goto do_read;
case PIX_FMT_GRAY16BE:
case PIX_FMT_GRAY16LE:
n = avctx->width * 2;
if (s->maxval < 65535)
upgrade = 2;
goto do_read;
case PIX_FMT_MONOWHITE:
case PIX_FMT_MONOBLACK:
n = (avctx->width + 7) >> 3;
do_read:
ptr = p->data[0];
linesize = p->linesize[0];
if(s->bytestream + n*avctx->height > s->bytestream_end)
return -1;
for(i = 0; i < avctx->height; i++) {
if (!upgrade)
memcpy(ptr, s->bytestream, n);
else if (upgrade == 1) {
unsigned int j, f = (255*128 + s->maxval/2) / s->maxval;
for (j=0; j<n; j++)
ptr[j] = (s->bytestream[j] * f + 64) >> 7;
} else if (upgrade == 2) {
unsigned int j, v, f = (65535*32768 + s->maxval/2) / s->maxval;
for (j=0; j<n/2; j++) {
v = be2me_16(((uint16_t *)s->bytestream)[j]);
((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
}
}
s->bytestream += n;
ptr += linesize;
}
break;
case PIX_FMT_YUV420P:
{
unsigned char *ptr1, *ptr2;
n = avctx->width;
ptr = p->data[0];
linesize = p->linesize[0];
if(s->bytestream + n*avctx->height*3/2 > s->bytestream_end)
return -1;
for(i = 0; i < avctx->height; i++) {
memcpy(ptr, s->bytestream, n);
s->bytestream += n;
ptr += linesize;
}
ptr1 = p->data[1];
ptr2 = p->data[2];
n >>= 1;
h = avctx->height >> 1;
for(i = 0; i < h; i++) {
memcpy(ptr1, s->bytestream, n);
s->bytestream += n;
memcpy(ptr2, s->bytestream, n);
s->bytestream += n;
ptr1 += p->linesize[1];
ptr2 += p->linesize[2];
}
}
break;
case PIX_FMT_RGB32:
ptr = p->data[0];
linesize = p->linesize[0];
if(s->bytestream + avctx->width*avctx->height*4 > s->bytestream_end)
return -1;
for(i = 0; i < avctx->height; i++) {
int j, r, g, b, a;
for(j = 0;j < avctx->width; j++) {
r = *s->bytestream++;
g = *s->bytestream++;
b = *s->bytestream++;
a = *s->bytestream++;
((uint32_t *)ptr)[j] = (a << 24) | (r << 16) | (g << 8) | b;
}
ptr += linesize;
}
break;
}
*picture= *(AVFrame*)&s->picture;
*data_size = sizeof(AVPicture);
return s->bytestream - s->bytestream_start;
}
static int pnm_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data){
PNMContext *s = avctx->priv_data;
AVFrame *pict = data;
AVFrame * const p= (AVFrame*)&s->picture;
int i, h, h1, c, n, linesize;
uint8_t *ptr, *ptr1, *ptr2;
if(buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200){
av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
return -1;
}
*p = *pict;
p->pict_type= FF_I_TYPE;
p->key_frame= 1;
s->bytestream_start=
s->bytestream= outbuf;
s->bytestream_end= outbuf+buf_size;
h = avctx->height;
h1 = h;
switch(avctx->pix_fmt) {
case PIX_FMT_MONOWHITE:
c = '4';
n = (avctx->width + 7) >> 3;
break;
case PIX_FMT_GRAY8:
c = '5';
n = avctx->width;
break;
case PIX_FMT_GRAY16BE:
c = '5';
n = avctx->width * 2;
break;
case PIX_FMT_RGB24:
c = '6';
n = avctx->width * 3;
break;
case PIX_FMT_YUV420P:
c = '5';
n = avctx->width;
h1 = (h * 3) / 2;
break;
default:
return -1;
}
snprintf(s->bytestream, s->bytestream_end - s->bytestream,
"P%c\n%d %d\n",
c, avctx->width, h1);
s->bytestream += strlen(s->bytestream);
if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
snprintf(s->bytestream, s->bytestream_end - s->bytestream,
"%d\n", (avctx->pix_fmt != PIX_FMT_GRAY16BE) ? 255 : 65535);
s->bytestream += strlen(s->bytestream);
}
ptr = p->data[0];
linesize = p->linesize[0];
for(i=0;i<h;i++) {
memcpy(s->bytestream, ptr, n);
s->bytestream += n;
ptr += linesize;
}
if (avctx->pix_fmt == PIX_FMT_YUV420P) {
h >>= 1;
n >>= 1;
ptr1 = p->data[1];
ptr2 = p->data[2];
for(i=0;i<h;i++) {
memcpy(s->bytestream, ptr1, n);
s->bytestream += n;
memcpy(s->bytestream, ptr2, n);
s->bytestream += n;
ptr1 += p->linesize[1];
ptr2 += p->linesize[2];
}
}
return s->bytestream - s->bytestream_start;
}
static int pam_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data){
PNMContext *s = avctx->priv_data;
AVFrame *pict = data;
AVFrame * const p= (AVFrame*)&s->picture;
int i, h, w, n, linesize, depth, maxval;
const char *tuple_type;
uint8_t *ptr;
if(buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200){
av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
return -1;
}
*p = *pict;
p->pict_type= FF_I_TYPE;
p->key_frame= 1;
s->bytestream_start=
s->bytestream= outbuf;
s->bytestream_end= outbuf+buf_size;
h = avctx->height;
w = avctx->width;
switch(avctx->pix_fmt) {
case PIX_FMT_MONOWHITE:
n = (w + 7) >> 3;
depth = 1;
maxval = 1;
tuple_type = "BLACKANDWHITE";
break;
case PIX_FMT_GRAY8:
n = w;
depth = 1;
maxval = 255;
tuple_type = "GRAYSCALE";
break;
case PIX_FMT_RGB24:
n = w * 3;
depth = 3;
maxval = 255;
tuple_type = "RGB";
break;
case PIX_FMT_RGB32:
n = w * 4;
depth = 4;
maxval = 255;
tuple_type = "RGB_ALPHA";
break;
default:
return -1;
}
snprintf(s->bytestream, s->bytestream_end - s->bytestream,
"P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLETYPE %s\nENDHDR\n",
w, h, depth, maxval, tuple_type);
s->bytestream += strlen(s->bytestream);
ptr = p->data[0];
linesize = p->linesize[0];
if (avctx->pix_fmt == PIX_FMT_RGB32) {
int j;
unsigned int v;
for(i=0;i<h;i++) {
for(j=0;j<w;j++) {
v = ((uint32_t *)ptr)[j];
bytestream_put_be24(&s->bytestream, v);
*s->bytestream++ = v >> 24;
}
ptr += linesize;
}
} else {
for(i=0;i<h;i++) {
memcpy(s->bytestream, ptr, n);
s->bytestream += n;
ptr += linesize;
}
}
return s->bytestream - s->bytestream_start;
}
#if 0
static int pnm_probe(AVProbeData *pd)
{
const char *p = pd->buf;
if (pd->buf_size >= 8 &&
p[0] == 'P' &&
p[1] >= '4' && p[1] <= '6' &&
pnm_space(p[2]) )
return AVPROBE_SCORE_MAX - 1; /* to permit pgmyuv probe */
else
return 0;
}
static int pgmyuv_probe(AVProbeData *pd)
{
if (match_ext(pd->filename, "pgmyuv"))
return AVPROBE_SCORE_MAX;
else
return 0;
}
static int pam_probe(AVProbeData *pd)
{
const char *p = pd->buf;
if (pd->buf_size >= 8 &&
p[0] == 'P' &&
p[1] == '7' &&
p[2] == '\n')
return AVPROBE_SCORE_MAX;
else
return 0;
}
#endif
#ifdef CONFIG_PGM_ENCODER
AVCodec pgm_encoder = {
"pgm",
CODEC_TYPE_VIDEO,
CODEC_ID_PGM,
sizeof(PNMContext),
common_init,
pnm_encode_frame,
NULL, //encode_end,
pnm_decode_frame,
.pix_fmts= (enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE},
.long_name= NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
};
#endif // CONFIG_PGM_ENCODER
#ifdef CONFIG_PGMYUV_ENCODER
AVCodec pgmyuv_encoder = {
"pgmyuv",
CODEC_TYPE_VIDEO,
CODEC_ID_PGMYUV,
sizeof(PNMContext),
common_init,
pnm_encode_frame,
NULL, //encode_end,
pnm_decode_frame,
.pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
.long_name= NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
};
#endif // CONFIG_PGMYUV_ENCODER
#ifdef CONFIG_PPM_ENCODER
AVCodec ppm_encoder = {
"ppm",
CODEC_TYPE_VIDEO,
CODEC_ID_PPM,
sizeof(PNMContext),
common_init,
pnm_encode_frame,
NULL, //encode_end,
pnm_decode_frame,
.pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_NONE},
.long_name= NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
};
#endif // CONFIG_PPM_ENCODER
#ifdef CONFIG_PBM_ENCODER
AVCodec pbm_encoder = {
"pbm",
CODEC_TYPE_VIDEO,
CODEC_ID_PBM,
sizeof(PNMContext),
common_init,
pnm_encode_frame,
NULL, //encode_end,
pnm_decode_frame,
.pix_fmts= (enum PixelFormat[]){PIX_FMT_MONOWHITE, PIX_FMT_NONE},
.long_name= NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
};
#endif // CONFIG_PBM_ENCODER
#ifdef CONFIG_PAM_ENCODER
AVCodec pam_encoder = {
"pam",
CODEC_TYPE_VIDEO,
CODEC_ID_PAM,
sizeof(PNMContext),
common_init,
pam_encode_frame,
NULL, //encode_end,
pnm_decode_frame,
.pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, PIX_FMT_NONE},
.long_name= NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
};
#endif // CONFIG_PAM_ENCODER
@@ -0,0 +1,168 @@
/*
* Copyright (c) 2004 Roman Shaposhnik.
*
* Many thanks to Steven M. Schultz for providing clever ideas and
* to Michael Niedermayer <[email protected]> for writing initial
* implementation.
*
* 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 <pthread.h>
#include "avcodec.h"
typedef int (action_t)(AVCodecContext *c, void *arg);
typedef struct ThreadContext {
pthread_t *workers;
action_t *func;
void **args;
int *rets;
int rets_count;
int job_count;
pthread_cond_t last_job_cond;
pthread_cond_t current_job_cond;
pthread_mutex_t current_job_lock;
int current_job;
int done;
} ThreadContext;
static void* attribute_align_arg worker(void *v)
{
AVCodecContext *avctx = v;
ThreadContext *c = avctx->thread_opaque;
int our_job = c->job_count;
int thread_count = avctx->thread_count;
int self_id;
pthread_mutex_lock(&c->current_job_lock);
self_id = c->current_job++;
for (;;){
while (our_job >= c->job_count) {
if (c->current_job == thread_count + c->job_count)
pthread_cond_signal(&c->last_job_cond);
pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
our_job = self_id;
if (c->done) {
pthread_mutex_unlock(&c->current_job_lock);
return NULL;
}
}
pthread_mutex_unlock(&c->current_job_lock);
c->rets[our_job%c->rets_count] = c->func(avctx, c->args[our_job]);
pthread_mutex_lock(&c->current_job_lock);
our_job = c->current_job++;
}
}
static av_always_inline void avcodec_thread_park_workers(ThreadContext *c, int thread_count)
{
pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
pthread_mutex_unlock(&c->current_job_lock);
}
void avcodec_thread_free(AVCodecContext *avctx)
{
ThreadContext *c = avctx->thread_opaque;
int i;
pthread_mutex_lock(&c->current_job_lock);
c->done = 1;
pthread_cond_broadcast(&c->current_job_cond);
pthread_mutex_unlock(&c->current_job_lock);
for (i=0; i<avctx->thread_count; i++)
pthread_join(c->workers[i], NULL);
pthread_mutex_destroy(&c->current_job_lock);
pthread_cond_destroy(&c->current_job_cond);
pthread_cond_destroy(&c->last_job_cond);
av_free(c->workers);
av_freep(&avctx->thread_opaque);
}
int avcodec_thread_execute(AVCodecContext *avctx, action_t* func, void **arg, int *ret, int job_count)
{
ThreadContext *c= avctx->thread_opaque;
int dummy_ret;
if (job_count <= 0)
return 0;
pthread_mutex_lock(&c->current_job_lock);
c->current_job = avctx->thread_count;
c->job_count = job_count;
c->args = arg;
c->func = func;
if (ret) {
c->rets = ret;
c->rets_count = job_count;
} else {
c->rets = &dummy_ret;
c->rets_count = 1;
}
pthread_cond_broadcast(&c->current_job_cond);
avcodec_thread_park_workers(c, avctx->thread_count);
return 0;
}
int avcodec_thread_init(AVCodecContext *avctx, int thread_count)
{
int i;
ThreadContext *c;
c = av_mallocz(sizeof(ThreadContext));
if (!c)
return -1;
c->workers = av_mallocz(sizeof(pthread_t)*thread_count);
if (!c->workers) {
av_free(c);
return -1;
}
avctx->thread_opaque = c;
avctx->thread_count = thread_count;
c->current_job = 0;
c->job_count = 0;
c->done = 0;
pthread_cond_init(&c->current_job_cond, NULL);
pthread_cond_init(&c->last_job_cond, NULL);
pthread_mutex_init(&c->current_job_lock, NULL);
pthread_mutex_lock(&c->current_job_lock);
for (i=0; i<thread_count; i++) {
if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
avctx->thread_count = i;
pthread_mutex_unlock(&c->current_job_lock);
avcodec_thread_free(avctx);
return -1;
}
}
avcodec_thread_park_workers(c, thread_count);
avctx->execute = avcodec_thread_execute;
return 0;
}
@@ -0,0 +1,118 @@
/*
* V.Flash PTX (.ptx) image decoder
* Copyright (c) 2007 Ivo van Poorten
*
* 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 "avcodec.h"
typedef struct PTXContext {
AVFrame picture;
} PTXContext;
static av_cold int ptx_init(AVCodecContext *avctx) {
PTXContext *s = avctx->priv_data;
avcodec_get_frame_defaults(&s->picture);
avctx->coded_frame= &s->picture;
return 0;
}
static int ptx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
const uint8_t *buf, int buf_size) {
PTXContext * const s = avctx->priv_data;
AVFrame *picture = data;
AVFrame * const p = &s->picture;
unsigned int offset, w, h, y, stride, bytes_per_pixel;
uint8_t *ptr;
offset = AV_RL16(buf);
w = AV_RL16(buf+8);
h = AV_RL16(buf+10);
bytes_per_pixel = AV_RL16(buf+12) >> 3;
if (bytes_per_pixel != 2) {
av_log(avctx, AV_LOG_ERROR, "image format is not rgb15, please report on ffmpeg-users mailing list\n");
return -1;
}
avctx->pix_fmt = PIX_FMT_RGB555;
if (offset != 0x2c)
av_log(avctx, AV_LOG_WARNING, "offset != 0x2c, untested due to lack of sample files\n");
buf += offset;
if (p->data[0])
avctx->release_buffer(avctx, p);
if (avcodec_check_dimensions(avctx, w, h))
return -1;
if (w != avctx->width || h != avctx->height)
avcodec_set_dimensions(avctx, w, h);
if (avctx->get_buffer(avctx, p) < 0) {
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return -1;
}
p->pict_type = FF_I_TYPE;
ptr = p->data[0];
stride = p->linesize[0];
for (y=0; y<h; y++) {
#ifdef WORDS_BIGENDIAN
unsigned int x;
for (x=0; x<w*bytes_per_pixel; x+=bytes_per_pixel)
AV_WN16(ptr+x, AV_RL16(buf+x));
#else
memcpy(ptr, buf, w*bytes_per_pixel);
#endif
ptr += stride;
buf += w*bytes_per_pixel;
}
*picture = s->picture;
*data_size = sizeof(AVPicture);
return offset + w*h*bytes_per_pixel;
}
static av_cold int ptx_end(AVCodecContext *avctx) {
PTXContext *s = avctx->priv_data;
if(s->picture.data[0])
avctx->release_buffer(avctx, &s->picture);
return 0;
}
AVCodec ptx_decoder = {
"ptx",
CODEC_TYPE_VIDEO,
CODEC_ID_PTX,
sizeof(PTXContext),
ptx_init,
NULL,
ptx_end,
ptx_decode_frame,
0,
NULL,
.long_name = NULL_IF_CONFIG_SMALL("V.Flash PTX image"),
};