Newer
Older
ret = avctx->codec->decode(avctx, sub, got_sub_ptr,
(uint8_t *)buf, buf_size);
if (*got_sub_ptr)
avctx->frame_number++;
return ret;
}
entangled_thread_counter++;
if(entangled_thread_counter != 1){
av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
entangled_thread_counter--;
return -1;
}
if (ENABLE_THREADS && avctx->thread_opaque)
avcodec_thread_free(avctx);
if (avctx->codec->close)
avctx->codec->close(avctx);
entangled_thread_counter--;
return 0;
}
AVCodec *avcodec_find_encoder(enum CodecID id)
{
AVCodec *p;
p = first_avcodec;
while (p) {
if (p->encode != NULL && p->id == id)
return p;
p = p->next;
}
return NULL;
}
AVCodec *avcodec_find_encoder_by_name(const char *name)
{
AVCodec *p;
p = first_avcodec;
while (p) {
if (p->encode != NULL && strcmp(name,p->name) == 0)
return p;
p = p->next;
}
return NULL;
}
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
AVCodec *avcodec_find_decoder(enum CodecID id)
{
AVCodec *p;
p = first_avcodec;
while (p) {
if (p->decode != NULL && p->id == id)
return p;
p = p->next;
}
return NULL;
}
AVCodec *avcodec_find_decoder_by_name(const char *name)
{
AVCodec *p;
p = first_avcodec;
while (p) {
if (p->decode != NULL && strcmp(name,p->name) == 0)
return p;
p = p->next;
}
return NULL;
}
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
{
const char *codec_name;
AVCodec *p;
char buf1[32];
char channels_str[100];
AVRational display_aspect_ratio;
if (encode)
p = avcodec_find_encoder(enc->codec_id);
else
p = avcodec_find_decoder(enc->codec_id);
if (p) {
codec_name = p->name;
Fabrice Bellard
committed
if (!encode && enc->codec_id == CODEC_ID_MP3) {
if (enc->sub_id == 2)
codec_name = "mp2";
else if (enc->sub_id == 1)
codec_name = "mp1";
}
Fabrice Bellard
committed
} else if (enc->codec_id == CODEC_ID_MPEG2TS) {
/* fake mpeg2 transport stream codec (currently not
registered) */
codec_name = "mpeg2ts";
} else if (enc->codec_name[0] != '\0') {
codec_name = enc->codec_name;
} else {
/* output avi tags */
if( isprint(enc->codec_tag&0xFF) && isprint((enc->codec_tag>>8)&0xFF)
&& isprint((enc->codec_tag>>16)&0xFF) && isprint((enc->codec_tag>>24)&0xFF)){
snprintf(buf1, sizeof(buf1), "%c%c%c%c / 0x%04X",
enc->codec_tag & 0xff,
(enc->codec_tag >> 8) & 0xff,
(enc->codec_tag >> 16) & 0xff,
(enc->codec_tag >> 24) & 0xff,
enc->codec_tag);
} else {
snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
}
codec_name = buf1;
}
switch(enc->codec_type) {
case CODEC_TYPE_VIDEO:
snprintf(buf, buf_size,
"Video: %s%s",
codec_name, enc->mb_decision ? " (hq)" : "");
if (enc->pix_fmt != PIX_FMT_NONE) {
snprintf(buf + strlen(buf), buf_size - strlen(buf),
", %s",
avcodec_get_pix_fmt_name(enc->pix_fmt));
if (enc->width) {
snprintf(buf + strlen(buf), buf_size - strlen(buf),
", %dx%d",
enc->width, enc->height);
av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
enc->width*enc->sample_aspect_ratio.num,
enc->height*enc->sample_aspect_ratio.den,
1024*1024);
snprintf(buf + strlen(buf), buf_size - strlen(buf),
" [PAR %d:%d DAR %d:%d]",
enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
display_aspect_ratio.num, display_aspect_ratio.den);
int g= ff_gcd(enc->time_base.num, enc->time_base.den);
snprintf(buf + strlen(buf), buf_size - strlen(buf),
", %d/%d",
enc->time_base.num/g, enc->time_base.den/g);
}
if (encode) {
snprintf(buf + strlen(buf), buf_size - strlen(buf),
", q=%d-%d", enc->qmin, enc->qmax);
}
break;
case CODEC_TYPE_AUDIO:
snprintf(buf, buf_size,
"Audio: %s",
codec_name);
switch (enc->channels) {
case 1:
strcpy(channels_str, "mono");
break;
case 2:
strcpy(channels_str, "stereo");
break;
case 6:
strcpy(channels_str, "5:1");
break;
default:
snprintf(channels_str, sizeof(channels_str), "%d channels", enc->channels);
break;
}
if (enc->sample_rate) {
snprintf(buf + strlen(buf), buf_size - strlen(buf),
", %d Hz, %s",
enc->sample_rate,
channels_str);
/* for PCM codecs, compute bitrate directly */
switch(enc->codec_id) {
case CODEC_ID_PCM_S32LE:
case CODEC_ID_PCM_S32BE:
case CODEC_ID_PCM_U32LE:
case CODEC_ID_PCM_U32BE:
bitrate = enc->sample_rate * enc->channels * 32;
break;
case CODEC_ID_PCM_S24LE:
case CODEC_ID_PCM_S24BE:
case CODEC_ID_PCM_U24LE:
case CODEC_ID_PCM_U24BE:
case CODEC_ID_PCM_S24DAUD:
bitrate = enc->sample_rate * enc->channels * 24;
break;
case CODEC_ID_PCM_S16LE:
case CODEC_ID_PCM_S16BE:
case CODEC_ID_PCM_S16LE_PLANAR:
case CODEC_ID_PCM_U16LE:
case CODEC_ID_PCM_U16BE:
break;
case CODEC_ID_PCM_S8:
case CODEC_ID_PCM_U8:
case CODEC_ID_PCM_ALAW:
case CODEC_ID_PCM_MULAW:
break;
default:
bitrate = enc->bit_rate;
break;
}
Fabrice Bellard
committed
case CODEC_TYPE_DATA:
snprintf(buf, buf_size, "Data: %s", codec_name);
bitrate = enc->bit_rate;
break;
case CODEC_TYPE_SUBTITLE:
snprintf(buf, buf_size, "Subtitle: %s", codec_name);
Fabrice Bellard
committed
bitrate = enc->bit_rate;
break;
snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
return;
if (encode) {
if (enc->flags & CODEC_FLAG_PASS1)
snprintf(buf + strlen(buf), buf_size - strlen(buf),
", pass 1");
if (enc->flags & CODEC_FLAG_PASS2)
snprintf(buf + strlen(buf), buf_size - strlen(buf),
", pass 2");
}
snprintf(buf + strlen(buf), buf_size - strlen(buf),
unsigned avcodec_version( void )
{
return LIBAVCODEC_VERSION_INT;
}
unsigned avcodec_build( void )
{
return LIBAVCODEC_BUILD;
}
#if LIBAVUTIL_VERSION_INT < (50<<16)
av_crc04C11DB7= av_mallocz_static(sizeof(AVCRC) * 257);
av_crc8005 = av_mallocz_static(sizeof(AVCRC) * 257);
av_crc07 = av_mallocz_static(sizeof(AVCRC) * 257);
av_crc_init(av_crc04C11DB7, 0, 32, AV_CRC_32_IEEE, sizeof(AVCRC)*257);
av_crc_init(av_crc8005 , 0, 16, AV_CRC_16 , sizeof(AVCRC)*257);
av_crc_init(av_crc07 , 0, 8, AV_CRC_8_ATM , sizeof(AVCRC)*257);
static int inited = 0;
if (inited != 0)
inited = 1;
void avcodec_flush_buffers(AVCodecContext *avctx)
{
if(avctx->codec->flush)
avctx->codec->flush(avctx);
void avcodec_default_free_buffers(AVCodecContext *s){
int i, j;
if(s->internal_buffer==NULL) return;
for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
for(j=0; j<4; j++){
av_freep(&buf->base[j]);
buf->data[j]= NULL;
}
}
av_freep(&s->internal_buffer);
s->internal_buffer_count=0;
}
char av_get_pict_type_char(int pict_type){
switch(pict_type){
case I_TYPE: return 'I';
case P_TYPE: return 'P';
case B_TYPE: return 'B';
case S_TYPE: return 'S';
case SI_TYPE:return 'i';
case SP_TYPE:return 'p';
int av_get_bits_per_sample(enum CodecID codec_id){
switch(codec_id){
case CODEC_ID_ADPCM_SBPRO_2:
case CODEC_ID_ADPCM_SBPRO_3:
case CODEC_ID_ADPCM_SBPRO_4:
case CODEC_ID_ADPCM_CT:
return 4;
case CODEC_ID_PCM_ALAW:
case CODEC_ID_PCM_MULAW:
case CODEC_ID_PCM_S8:
case CODEC_ID_PCM_U8:
return 8;
case CODEC_ID_PCM_S16BE:
case CODEC_ID_PCM_S16LE:
case CODEC_ID_PCM_S16LE_PLANAR:
case CODEC_ID_PCM_U16BE:
case CODEC_ID_PCM_U16LE:
return 16;
case CODEC_ID_PCM_S24DAUD:
case CODEC_ID_PCM_S24BE:
case CODEC_ID_PCM_S24LE:
case CODEC_ID_PCM_U24BE:
case CODEC_ID_PCM_U24LE:
return 24;
case CODEC_ID_PCM_S32BE:
case CODEC_ID_PCM_S32LE:
case CODEC_ID_PCM_U32BE:
case CODEC_ID_PCM_U32LE:
return 32;
default:
return 0;
}
}
int av_get_bits_per_sample_format(enum SampleFormat sample_fmt) {
switch (sample_fmt) {
case SAMPLE_FMT_U8:
return 8;
case SAMPLE_FMT_S16:
return 16;
case SAMPLE_FMT_S24:
return 24;
case SAMPLE_FMT_S32:
case SAMPLE_FMT_FLT:
return 32;
default:
return 0;
}
}
François Revol
committed
#if !defined(HAVE_THREADS)
int avcodec_thread_init(AVCodecContext *s, int thread_count){
return -1;
}
#endif
unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
{
unsigned int n = 0;
while(v >= 0xff) {
*s++ = 0xff;
v -= 0xff;
n++;
}
*s = v;
n++;
return n;
}
/* Wrapper to work around the lack of mkstemp() on mingw/cygin.
* Also, tries to create file in /tmp first, if possible.
* *prefix can be a character constant; *filename will be allocated internally.
* Returns file descriptor of opened file (or -1 on error)
* and opened file name in **filename. */
int av_tempfile(char *prefix, char **filename) {
int fd=-1;
*filename = tempnam(".", prefix);
#else
size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */
#endif
/* -----common section-----*/
if (*filename == NULL) {
av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
return -1;
}
fd = open(*filename, O_RDWR | O_BINARY | O_CREAT, 0444);
#else
snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
fd = mkstemp(*filename);
if (fd < 0) {
snprintf(*filename, len, "./%sXXXXXX", prefix);
fd = mkstemp(*filename);
}
#endif
/* -----common section-----*/
if (fd < 0) {
av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename);
return -1;
}
return fd; /* success */
}
Stefano Sabatini
committed
typedef struct {
const char *abbr;
Stefano Sabatini
committed
int width, height;
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
} VideoFrameSizeAbbr;
typedef struct {
const char *abbr;
int rate_num, rate_den;
} VideoFrameRateAbbr;
static VideoFrameSizeAbbr video_frame_size_abbrs[] = {
{ "ntsc", 720, 480 },
{ "pal", 720, 576 },
{ "qntsc", 352, 240 }, /* VCD compliant NTSC */
{ "qpal", 352, 288 }, /* VCD compliant PAL */
{ "sntsc", 640, 480 }, /* square pixel NTSC */
{ "spal", 768, 576 }, /* square pixel PAL */
{ "film", 352, 240 },
{ "ntsc-film", 352, 240 },
{ "sqcif", 128, 96 },
{ "qcif", 176, 144 },
{ "cif", 352, 288 },
{ "4cif", 704, 576 },
{ "qqvga", 160, 120 },
{ "qvga", 320, 240 },
{ "vga", 640, 480 },
{ "svga", 800, 600 },
{ "xga", 1024, 768 },
{ "uxga", 1600,1200 },
{ "qxga", 2048,1536 },
{ "sxga", 1280,1024 },
{ "qsxga", 2560,2048 },
{ "hsxga", 5120,4096 },
{ "wvga", 852, 480 },
{ "wxga", 1366, 768 },
{ "wsxga", 1600,1024 },
{ "wuxga", 1920,1200 },
{ "woxga", 2560,1600 },
{ "wqsxga", 3200,2048 },
{ "wquxga", 3840,2400 },
{ "whsxga", 6400,4096 },
{ "whuxga", 7680,4800 },
{ "cga", 320, 200 },
{ "ega", 640, 350 },
{ "hd480", 852, 480 },
{ "hd720", 1280, 720 },
{ "hd1080", 1920,1080 },
};
static VideoFrameRateAbbr video_frame_rate_abbrs[]= {
{ "ntsc", 30000, 1001 },
{ "pal", 25, 1 },
{ "qntsc", 30000, 1001 }, /* VCD compliant NTSC */
{ "qpal", 25, 1 }, /* VCD compliant PAL */
{ "sntsc", 30000, 1001 }, /* square pixel NTSC */
{ "spal", 25, 1 }, /* square pixel PAL */
{ "film", 24, 1 },
{ "ntsc-film", 24000, 1001 },
Stefano Sabatini
committed
};
int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
{
int i;
int n = sizeof(video_frame_size_abbrs) / sizeof(VideoFrameSizeAbbr);
Stefano Sabatini
committed
const char *p;
int frame_width = 0, frame_height = 0;
for(i=0;i<n;i++) {
if (!strcmp(video_frame_size_abbrs[i].abbr, str)) {
frame_width = video_frame_size_abbrs[i].width;
frame_height = video_frame_size_abbrs[i].height;
Stefano Sabatini
committed
break;
}
}
if (i == n) {
p = str;
frame_width = strtol(p, (char **)&p, 10);
if (*p)
p++;
frame_height = strtol(p, (char **)&p, 10);
}
if (frame_width <= 0 || frame_height <= 0)
return -1;
*width_ptr = frame_width;
*height_ptr = frame_height;
return 0;
}
int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
{
int i;
int n = sizeof(video_frame_rate_abbrs) / sizeof(VideoFrameRateAbbr);
Stefano Sabatini
committed
char* cp;
/* First, we check our abbreviation table */
for (i = 0; i < n; ++i)
if (!strcmp(video_frame_rate_abbrs[i].abbr, arg)) {
frame_rate->num = video_frame_rate_abbrs[i].rate_num;
frame_rate->den = video_frame_rate_abbrs[i].rate_den;
Stefano Sabatini
committed
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
return 0;
}
/* Then, we try to parse it as fraction */
cp = strchr(arg, '/');
if (!cp)
cp = strchr(arg, ':');
if (cp) {
char* cpp;
frame_rate->num = strtol(arg, &cpp, 10);
if (cpp != arg || cpp == cp)
frame_rate->den = strtol(cp+1, &cpp, 10);
else
frame_rate->num = 0;
}
else {
/* Finally we give up and parse it as double */
AVRational time_base = av_d2q(strtod(arg, 0), DEFAULT_FRAME_RATE_BASE);
frame_rate->den = time_base.den;
frame_rate->num = time_base.num;
}
if (!frame_rate->num || !frame_rate->den)
return -1;
else
return 0;
}