5 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
8 // private structure used to hide all internal memory allocations
9 // and structures used for de/encoding - end user should
10 // never see any complicated structure
11 typedef struct private_handle
14 AVCodecContext avcontext;
15 struct private_handle* next;
16 struct private_handle* prev;
19 static private_handle_t* handle_first = 0;
21 static AVCodec* avcodec_find_by_fcc(uint32_t fcc)
24 static const struct fcc_to_avcodecid {
26 uint32_t list[4]; // maybe we could map more fcc to same codec
28 { CODEC_ID_H263, { MKTAG('U', '2', '6', '3'), 0 } },
29 { CODEC_ID_H263I, { MKTAG('I', '2', '6', '3'), 0 } },
30 { CODEC_ID_MSMPEG4V3, { MKTAG('D', 'I', 'V', '3'), 0 } },
31 { CODEC_ID_MPEG4, { MKTAG('D', 'I', 'V', 'X'), MKTAG('D', 'X', '5', '0'), 0 } },
32 { CODEC_ID_MSMPEG4V2, { MKTAG('M', 'P', '4', '2'), 0 } },
33 { CODEC_ID_MJPEG, { MKTAG('M', 'J', 'P', 'G'), 0 } },
34 { CODEC_ID_MPEG1VIDEO, { MKTAG('P', 'I', 'M', '1'), 0 } },
35 { CODEC_ID_AC3, { 0x2000, 0 } },
36 { CODEC_ID_MP2, { 0x50, 0x55, 0 } },
40 const struct fcc_to_avcodecid* c;
42 for (c = lc; c->codec != CODEC_ID_NONE; c++)
45 while (c->list[i] != 0)
46 if (c->list[i++] == fcc)
47 return avcodec_find_decoder(c->codec);
53 static private_handle_t* create_handle()
55 private_handle_t* t = malloc(sizeof(private_handle_t));
58 memset(t, 0, sizeof(*t));
64 avcodec_register_all();
69 t->prev = handle_first->next;
70 handle_first->next = t;
71 t->next = handle_first;
77 static void destroy_handle(private_handle_t* handle)
83 avcodec_close(&handle->avcontext);
91 int avcodec(void* handle, avc_cmd_t cmd, void* pin, void* pout)
93 AVCodecContext* ctx = handle;
96 case AVC_OPEN_BY_NAME:
98 // pin char* codec name
99 private_handle_t* handle = create_handle();
100 (private_handle_t**)pout = handle;
103 if (!handle->avcodec)
105 destroy_handle(handle);
106 (private_handle_t**)pout = NULL;
107 return -1;// better error
111 case AVC_OPEN_BY_CODEC_ID:
113 // pin uint32_t codec fourcc
114 private_handle_t* handle = create_handle();
115 (private_handle_t**)pout = handle;
119 if (!handle->avcodec)
121 destroy_handle(handle);
122 (private_handle_t**)pout = NULL;
123 return -1;// better error
127 case AVC_OPEN_BY_FOURCC:
129 // pin uint32_t codec fourcc
130 private_handle_t* handle = create_handle();
131 (private_handle_t**)pout = handle;
134 handle->avcodec = avcodec_find_by_fcc((uint32_t) pin);
135 if (!handle->avcodec)
137 destroy_handle(handle);
138 (private_handle_t**)pout = NULL;
139 return -1;// better error
145 // eventually close all allocated space if this was last
147 destroy_handle(handle);
159 case AVC_GET_VERSION: