Mercurial > libavcodec.hg
annotate avcodec.c @ 2227:bf414a07af2e libavcodec
AVC (H264 in mp4 files, fourcc avc1) support
| author | rtognimp |
|---|---|
| date | Mon, 13 Sep 2004 21:20:55 +0000 |
| parents | ef54decf5624 |
| children |
| rev | line source |
|---|---|
| 1106 | 1 /** |
| 2 * @file avcodec.c | |
| 3 * avcodec. | |
| 4 */ | |
| 5 | |
| 382 | 6 #include "errno.h" |
| 7 #include "avcodec.h" | |
| 8 | |
| 9 #ifndef MKTAG | |
| 10 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24)) | |
| 11 #endif | |
| 12 | |
| 13 // private structure used to hide all internal memory allocations | |
| 14 // and structures used for de/encoding - end user should | |
| 15 // never see any complicated structure | |
| 852 | 16 typedef struct private_handle |
| 382 | 17 { |
| 18 AVCodec* avcodec; | |
| 19 AVCodecContext avcontext; | |
| 852 | 20 struct private_handle* next; |
| 21 struct private_handle* prev; | |
| 382 | 22 } private_handle_t; |
| 23 | |
| 852 | 24 static private_handle_t* handle_first = 0; |
| 25 | |
| 382 | 26 static AVCodec* avcodec_find_by_fcc(uint32_t fcc) |
| 27 { | |
| 28 // translation table | |
| 29 static const struct fcc_to_avcodecid { | |
| 30 enum CodecID codec; | |
| 31 uint32_t list[4]; // maybe we could map more fcc to same codec | |
| 32 } lc[] = { | |
| 33 { CODEC_ID_H263, { MKTAG('U', '2', '6', '3'), 0 } }, | |
| 34 { CODEC_ID_H263I, { MKTAG('I', '2', '6', '3'), 0 } }, | |
| 852 | 35 { CODEC_ID_MSMPEG4V3, { MKTAG('D', 'I', 'V', '3'), 0 } }, |
| 382 | 36 { CODEC_ID_MPEG4, { MKTAG('D', 'I', 'V', 'X'), MKTAG('D', 'X', '5', '0'), 0 } }, |
| 37 { CODEC_ID_MSMPEG4V2, { MKTAG('M', 'P', '4', '2'), 0 } }, | |
| 38 { CODEC_ID_MJPEG, { MKTAG('M', 'J', 'P', 'G'), 0 } }, | |
| 39 { CODEC_ID_MPEG1VIDEO, { MKTAG('P', 'I', 'M', '1'), 0 } }, | |
| 40 { CODEC_ID_AC3, { 0x2000, 0 } }, | |
|
2123
ef54decf5624
libdts support by (Benjamin Zores <ben at geexbox dot org>)
michael
parents:
1353
diff
changeset
|
41 { CODEC_ID_DTS, { 0x10, 0 } }, |
| 382 | 42 { CODEC_ID_MP2, { 0x50, 0x55, 0 } }, |
|
1353
cfc80b3a4ada
flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
michaelni
parents:
1106
diff
changeset
|
43 { CODEC_ID_FLV1, { MKTAG('F', 'L', 'V', '1'), 0 } }, |
| 382 | 44 |
| 45 { CODEC_ID_NONE, {0}} | |
| 46 }; | |
| 47 const struct fcc_to_avcodecid* c; | |
| 48 | |
| 49 for (c = lc; c->codec != CODEC_ID_NONE; c++) | |
| 50 { | |
| 51 int i = 0; | |
| 52 while (c->list[i] != 0) | |
| 53 if (c->list[i++] == fcc) | |
| 54 return avcodec_find_decoder(c->codec); | |
| 55 } | |
| 56 | |
| 57 return NULL; | |
| 58 } | |
| 59 | |
| 1059 | 60 static private_handle_t* create_handle(void) |
| 382 | 61 { |
| 1059 | 62 private_handle_t* t = av_malloc(sizeof(private_handle_t)); |
| 382 | 63 if (!t) |
| 64 return NULL; | |
| 852 | 65 memset(t, 0, sizeof(*t)); |
| 382 | 66 |
| 67 // register and fill | |
| 852 | 68 if (!handle_first) |
| 69 { | |
| 70 avcodec_init(); | |
| 71 avcodec_register_all(); | |
| 72 handle_first = t; | |
| 73 } | |
| 74 else | |
| 75 { | |
| 76 t->prev = handle_first->next; | |
| 77 handle_first->next = t; | |
| 78 t->next = handle_first; | |
| 79 } | |
| 80 | |
| 382 | 81 return t; |
| 82 } | |
| 83 | |
| 84 static void destroy_handle(private_handle_t* handle) | |
| 85 { | |
| 86 if (handle) | |
| 87 { | |
| 88 if (handle->avcodec) | |
| 89 { | |
| 90 avcodec_close(&handle->avcontext); | |
| 91 } | |
| 1059 | 92 av_free(handle); |
| 382 | 93 |
| 94 // count referencies | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 int avcodec(void* handle, avc_cmd_t cmd, void* pin, void* pout) | |
| 99 { | |
| 100 AVCodecContext* ctx = handle; | |
| 101 switch (cmd) | |
| 102 { | |
| 103 case AVC_OPEN_BY_NAME: | |
| 104 { | |
| 105 // pin char* codec name | |
| 1059 | 106 private_handle_t* h = create_handle(); |
| 107 (private_handle_t**)pout = h; | |
| 108 if (!h) | |
| 382 | 109 return -ENOMEM; |
| 1059 | 110 if (!h->avcodec) |
| 382 | 111 { |
| 1059 | 112 destroy_handle(h); |
| 382 | 113 (private_handle_t**)pout = NULL; |
| 114 return -1;// better error | |
| 115 } | |
| 116 return 0; | |
| 117 } | |
| 118 case AVC_OPEN_BY_CODEC_ID: | |
| 119 { | |
| 120 // pin uint32_t codec fourcc | |
| 1059 | 121 private_handle_t* h = create_handle(); |
| 122 (private_handle_t**)pout = h; | |
| 123 if (!h) | |
| 382 | 124 return -ENOMEM; |
| 125 | |
| 1059 | 126 if (!h->avcodec) |
| 382 | 127 { |
| 1059 | 128 destroy_handle(h); |
| 382 | 129 (private_handle_t**)pout = NULL; |
| 130 return -1;// better error | |
| 131 } | |
| 132 return 0; | |
| 133 } | |
| 134 case AVC_OPEN_BY_FOURCC: | |
| 135 { | |
| 136 // pin uint32_t codec fourcc | |
| 1059 | 137 private_handle_t* h = create_handle(); |
| 138 (private_handle_t**)pout = h; | |
| 139 if (!h) | |
| 382 | 140 return -ENOMEM; |
| 1059 | 141 h->avcodec = avcodec_find_by_fcc((uint32_t) pin); |
| 142 if (!h->avcodec) | |
| 382 | 143 { |
| 1059 | 144 destroy_handle(h); |
| 382 | 145 (private_handle_t**)pout = NULL; |
| 146 return -1;// better error | |
| 147 } | |
| 148 return 0; | |
| 149 } | |
| 150 case AVC_CLOSE: | |
| 151 // uninit part | |
| 152 // eventually close all allocated space if this was last | |
| 153 // instance | |
| 154 destroy_handle(handle); | |
| 155 break; | |
| 156 | |
| 157 case AVC_FLUSH: | |
| 158 break; | |
| 159 | |
| 160 case AVC_DECODE: | |
| 161 break; | |
| 162 | |
| 163 case AVC_ENCODE: | |
| 164 break; | |
| 165 | |
| 166 case AVC_GET_VERSION: | |
| 167 (int*) pout = 500; | |
| 168 default: | |
| 169 return -1; | |
| 170 | |
| 171 } | |
| 172 return 0; | |
| 173 } |
