2003-03-06 13:32:04 +02:00
|
|
|
/**
|
|
|
|
* @file avcodec.c
|
|
|
|
* avcodec.
|
|
|
|
*/
|
|
|
|
|
2002-05-14 17:17:11 +03:00
|
|
|
#include "errno.h"
|
|
|
|
#include "avcodec.h"
|
|
|
|
|
|
|
|
#ifndef MKTAG
|
|
|
|
#define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// private structure used to hide all internal memory allocations
|
|
|
|
// and structures used for de/encoding - end user should
|
|
|
|
// never see any complicated structure
|
2002-11-11 11:37:40 +02:00
|
|
|
typedef struct private_handle
|
2002-05-14 17:17:11 +03:00
|
|
|
{
|
|
|
|
AVCodec* avcodec;
|
|
|
|
AVCodecContext avcontext;
|
2002-11-11 11:37:40 +02:00
|
|
|
struct private_handle* next;
|
|
|
|
struct private_handle* prev;
|
2002-05-14 17:17:11 +03:00
|
|
|
} private_handle_t;
|
|
|
|
|
2002-11-11 11:37:40 +02:00
|
|
|
static private_handle_t* handle_first = 0;
|
|
|
|
|
2002-05-14 17:17:11 +03:00
|
|
|
static AVCodec* avcodec_find_by_fcc(uint32_t fcc)
|
|
|
|
{
|
|
|
|
// translation table
|
|
|
|
static const struct fcc_to_avcodecid {
|
|
|
|
enum CodecID codec;
|
|
|
|
uint32_t list[4]; // maybe we could map more fcc to same codec
|
|
|
|
} lc[] = {
|
|
|
|
{ CODEC_ID_H263, { MKTAG('U', '2', '6', '3'), 0 } },
|
|
|
|
{ CODEC_ID_H263I, { MKTAG('I', '2', '6', '3'), 0 } },
|
2002-11-11 11:37:40 +02:00
|
|
|
{ CODEC_ID_MSMPEG4V3, { MKTAG('D', 'I', 'V', '3'), 0 } },
|
2002-05-14 17:17:11 +03:00
|
|
|
{ CODEC_ID_MPEG4, { MKTAG('D', 'I', 'V', 'X'), MKTAG('D', 'X', '5', '0'), 0 } },
|
|
|
|
{ CODEC_ID_MSMPEG4V2, { MKTAG('M', 'P', '4', '2'), 0 } },
|
|
|
|
{ CODEC_ID_MJPEG, { MKTAG('M', 'J', 'P', 'G'), 0 } },
|
|
|
|
{ CODEC_ID_MPEG1VIDEO, { MKTAG('P', 'I', 'M', '1'), 0 } },
|
|
|
|
{ CODEC_ID_AC3, { 0x2000, 0 } },
|
|
|
|
{ CODEC_ID_MP2, { 0x50, 0x55, 0 } },
|
2003-07-10 02:10:59 +03:00
|
|
|
{ CODEC_ID_FLV1, { MKTAG('F', 'L', 'V', '1'), 0 } },
|
2002-05-14 17:17:11 +03:00
|
|
|
|
|
|
|
{ CODEC_ID_NONE, {0}}
|
|
|
|
};
|
|
|
|
const struct fcc_to_avcodecid* c;
|
|
|
|
|
|
|
|
for (c = lc; c->codec != CODEC_ID_NONE; c++)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
while (c->list[i] != 0)
|
|
|
|
if (c->list[i++] == fcc)
|
|
|
|
return avcodec_find_decoder(c->codec);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-02-10 11:40:23 +02:00
|
|
|
static private_handle_t* create_handle(void)
|
2002-05-14 17:17:11 +03:00
|
|
|
{
|
2003-02-10 11:40:23 +02:00
|
|
|
private_handle_t* t = av_malloc(sizeof(private_handle_t));
|
2002-05-14 17:17:11 +03:00
|
|
|
if (!t)
|
|
|
|
return NULL;
|
2002-11-11 11:37:40 +02:00
|
|
|
memset(t, 0, sizeof(*t));
|
2002-05-14 17:17:11 +03:00
|
|
|
|
|
|
|
// register and fill
|
2002-11-11 11:37:40 +02:00
|
|
|
if (!handle_first)
|
|
|
|
{
|
|
|
|
avcodec_init();
|
|
|
|
avcodec_register_all();
|
|
|
|
handle_first = t;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
t->prev = handle_first->next;
|
|
|
|
handle_first->next = t;
|
|
|
|
t->next = handle_first;
|
|
|
|
}
|
|
|
|
|
2002-05-14 17:17:11 +03:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_handle(private_handle_t* handle)
|
|
|
|
{
|
|
|
|
if (handle)
|
|
|
|
{
|
|
|
|
if (handle->avcodec)
|
|
|
|
{
|
|
|
|
avcodec_close(&handle->avcontext);
|
|
|
|
}
|
2003-02-10 11:40:23 +02:00
|
|
|
av_free(handle);
|
2002-05-14 17:17:11 +03:00
|
|
|
|
|
|
|
// count referencies
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int avcodec(void* handle, avc_cmd_t cmd, void* pin, void* pout)
|
|
|
|
{
|
|
|
|
AVCodecContext* ctx = handle;
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case AVC_OPEN_BY_NAME:
|
|
|
|
{
|
|
|
|
// pin char* codec name
|
2003-02-10 11:40:23 +02:00
|
|
|
private_handle_t* h = create_handle();
|
|
|
|
(private_handle_t**)pout = h;
|
|
|
|
if (!h)
|
2002-05-14 17:17:11 +03:00
|
|
|
return -ENOMEM;
|
2003-02-10 11:40:23 +02:00
|
|
|
if (!h->avcodec)
|
2002-05-14 17:17:11 +03:00
|
|
|
{
|
2003-02-10 11:40:23 +02:00
|
|
|
destroy_handle(h);
|
2002-05-14 17:17:11 +03:00
|
|
|
(private_handle_t**)pout = NULL;
|
|
|
|
return -1;// better error
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case AVC_OPEN_BY_CODEC_ID:
|
|
|
|
{
|
|
|
|
// pin uint32_t codec fourcc
|
2003-02-10 11:40:23 +02:00
|
|
|
private_handle_t* h = create_handle();
|
|
|
|
(private_handle_t**)pout = h;
|
|
|
|
if (!h)
|
2002-05-14 17:17:11 +03:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2003-02-10 11:40:23 +02:00
|
|
|
if (!h->avcodec)
|
2002-05-14 17:17:11 +03:00
|
|
|
{
|
2003-02-10 11:40:23 +02:00
|
|
|
destroy_handle(h);
|
2002-05-14 17:17:11 +03:00
|
|
|
(private_handle_t**)pout = NULL;
|
|
|
|
return -1;// better error
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case AVC_OPEN_BY_FOURCC:
|
|
|
|
{
|
|
|
|
// pin uint32_t codec fourcc
|
2003-02-10 11:40:23 +02:00
|
|
|
private_handle_t* h = create_handle();
|
|
|
|
(private_handle_t**)pout = h;
|
|
|
|
if (!h)
|
2002-05-14 17:17:11 +03:00
|
|
|
return -ENOMEM;
|
2003-02-10 11:40:23 +02:00
|
|
|
h->avcodec = avcodec_find_by_fcc((uint32_t) pin);
|
|
|
|
if (!h->avcodec)
|
2002-05-14 17:17:11 +03:00
|
|
|
{
|
2003-02-10 11:40:23 +02:00
|
|
|
destroy_handle(h);
|
2002-05-14 17:17:11 +03:00
|
|
|
(private_handle_t**)pout = NULL;
|
|
|
|
return -1;// better error
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case AVC_CLOSE:
|
|
|
|
// uninit part
|
|
|
|
// eventually close all allocated space if this was last
|
|
|
|
// instance
|
|
|
|
destroy_handle(handle);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AVC_FLUSH:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AVC_DECODE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AVC_ENCODE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AVC_GET_VERSION:
|
|
|
|
(int*) pout = 500;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|