You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

320 lines
7.9KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include "common.h"
  24. #include "dsputil.h"
  25. #include "avcodec.h"
  26. /* memory alloc */
  27. void *av_mallocz(int size)
  28. {
  29. void *ptr;
  30. ptr = malloc(size);
  31. if (!ptr)
  32. return NULL;
  33. memset(ptr, 0, size);
  34. return ptr;
  35. }
  36. /* encoder management */
  37. AVCodec *first_avcodec;
  38. void register_avcodec(AVCodec *format)
  39. {
  40. AVCodec **p;
  41. p = &first_avcodec;
  42. while (*p != NULL) p = &(*p)->next;
  43. *p = format;
  44. format->next = NULL;
  45. }
  46. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  47. {
  48. int ret;
  49. avctx->codec = codec;
  50. avctx->frame_number = 0;
  51. avctx->priv_data = av_mallocz(codec->priv_data_size);
  52. if (!avctx->priv_data)
  53. return -ENOMEM;
  54. ret = avctx->codec->init(avctx);
  55. if (ret < 0) {
  56. free(avctx->priv_data);
  57. avctx->priv_data = NULL;
  58. return ret;
  59. }
  60. return 0;
  61. }
  62. int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  63. const short *samples)
  64. {
  65. int ret;
  66. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  67. avctx->frame_number++;
  68. return ret;
  69. }
  70. int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  71. const AVPicture *pict)
  72. {
  73. int ret;
  74. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  75. avctx->frame_number++;
  76. return ret;
  77. }
  78. /* decode a frame. return -1 if error, otherwise return the number of
  79. bytes used. If no frame could be decompressed, *got_picture_ptr is
  80. zero. Otherwise, it is non zero */
  81. int avcodec_decode_video(AVCodecContext *avctx, AVPicture *picture,
  82. int *got_picture_ptr,
  83. UINT8 *buf, int buf_size)
  84. {
  85. int ret;
  86. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  87. buf, buf_size);
  88. avctx->frame_number++;
  89. return ret;
  90. }
  91. /* decode an audio frame. return -1 if error, otherwise return the
  92. *number of bytes used. If no frame could be decompressed,
  93. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  94. *size in BYTES. */
  95. int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
  96. int *frame_size_ptr,
  97. UINT8 *buf, int buf_size)
  98. {
  99. int ret;
  100. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  101. buf, buf_size);
  102. avctx->frame_number++;
  103. return ret;
  104. }
  105. int avcodec_close(AVCodecContext *avctx)
  106. {
  107. if (avctx->codec->close)
  108. avctx->codec->close(avctx);
  109. free(avctx->priv_data);
  110. avctx->priv_data = NULL;
  111. avctx->codec = NULL;
  112. return 0;
  113. }
  114. AVCodec *avcodec_find_encoder(enum CodecID id)
  115. {
  116. AVCodec *p;
  117. p = first_avcodec;
  118. while (p) {
  119. if (p->encode != NULL && p->id == id)
  120. return p;
  121. p = p->next;
  122. }
  123. return NULL;
  124. }
  125. AVCodec *avcodec_find_decoder(enum CodecID id)
  126. {
  127. AVCodec *p;
  128. p = first_avcodec;
  129. while (p) {
  130. if (p->decode != NULL && p->id == id)
  131. return p;
  132. p = p->next;
  133. }
  134. return NULL;
  135. }
  136. AVCodec *avcodec_find_decoder_by_name(const char *name)
  137. {
  138. AVCodec *p;
  139. p = first_avcodec;
  140. while (p) {
  141. if (p->decode != NULL && strcmp(name,p->name) == 0)
  142. return p;
  143. p = p->next;
  144. }
  145. return NULL;
  146. }
  147. AVCodec *avcodec_find(enum CodecID id)
  148. {
  149. AVCodec *p;
  150. p = first_avcodec;
  151. while (p) {
  152. if (p->id == id)
  153. return p;
  154. p = p->next;
  155. }
  156. return NULL;
  157. }
  158. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  159. {
  160. const char *codec_name;
  161. AVCodec *p;
  162. char buf1[32];
  163. if (encode)
  164. p = avcodec_find_encoder(enc->codec_id);
  165. else
  166. p = avcodec_find_decoder(enc->codec_id);
  167. if (p) {
  168. codec_name = p->name;
  169. } else if (enc->codec_name[0] != '\0') {
  170. codec_name = enc->codec_name;
  171. } else {
  172. /* output avi tags */
  173. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  174. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  175. enc->codec_tag & 0xff,
  176. (enc->codec_tag >> 8) & 0xff,
  177. (enc->codec_tag >> 16) & 0xff,
  178. (enc->codec_tag >> 24) & 0xff);
  179. } else {
  180. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  181. }
  182. codec_name = buf1;
  183. }
  184. switch(enc->codec_type) {
  185. case CODEC_TYPE_VIDEO:
  186. snprintf(buf, buf_size,
  187. "Video: %s%s",
  188. codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
  189. if (enc->width) {
  190. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  191. ", %dx%d, %0.2f fps",
  192. enc->width, enc->height,
  193. (float)enc->frame_rate / FRAME_RATE_BASE);
  194. }
  195. break;
  196. case CODEC_TYPE_AUDIO:
  197. snprintf(buf, buf_size,
  198. "Audio: %s",
  199. codec_name);
  200. if (enc->sample_rate) {
  201. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  202. ", %d Hz, %s",
  203. enc->sample_rate,
  204. enc->channels == 2 ? "stereo" : "mono");
  205. }
  206. break;
  207. default:
  208. abort();
  209. }
  210. if (enc->bit_rate != 0) {
  211. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  212. ", %d kb/s", enc->bit_rate / 1000);
  213. }
  214. }
  215. /* must be called before any other functions */
  216. void avcodec_init(void)
  217. {
  218. dsputil_init();
  219. }
  220. /* simple call to use all the codecs */
  221. void avcodec_register_all(void)
  222. {
  223. /* encoders */
  224. #ifdef CONFIG_ENCODERS
  225. register_avcodec(&ac3_encoder);
  226. register_avcodec(&mp2_encoder);
  227. register_avcodec(&mpeg1video_encoder);
  228. register_avcodec(&h263_encoder);
  229. register_avcodec(&h263p_encoder);
  230. register_avcodec(&rv10_encoder);
  231. register_avcodec(&mjpeg_encoder);
  232. register_avcodec(&opendivx_encoder);
  233. register_avcodec(&msmpeg4_encoder);
  234. #endif /* CONFIG_ENCODERS */
  235. register_avcodec(&pcm_codec);
  236. register_avcodec(&rawvideo_codec);
  237. /* decoders */
  238. #ifdef CONFIG_DECODERS
  239. register_avcodec(&h263_decoder);
  240. register_avcodec(&opendivx_decoder);
  241. register_avcodec(&msmpeg4_decoder);
  242. register_avcodec(&mpeg_decoder);
  243. register_avcodec(&h263i_decoder);
  244. register_avcodec(&rv10_decoder);
  245. register_avcodec(&mjpeg_decoder);
  246. #ifdef CONFIG_MPGLIB
  247. register_avcodec(&mp3_decoder);
  248. #endif
  249. #ifdef CONFIG_AC3
  250. register_avcodec(&ac3_decoder);
  251. #endif
  252. #endif /* CONFIG_DECODERS */
  253. }
  254. static int encode_init(AVCodecContext *s)
  255. {
  256. return 0;
  257. }
  258. static int decode_frame(AVCodecContext *avctx,
  259. void *data, int *data_size,
  260. UINT8 *buf, int buf_size)
  261. {
  262. return -1;
  263. }
  264. static int encode_frame(AVCodecContext *avctx,
  265. unsigned char *frame, int buf_size, void *data)
  266. {
  267. return -1;
  268. }
  269. /* dummy pcm codec */
  270. AVCodec pcm_codec = {
  271. "pcm",
  272. CODEC_TYPE_AUDIO,
  273. CODEC_ID_PCM,
  274. 0,
  275. encode_init,
  276. encode_frame,
  277. NULL,
  278. decode_frame,
  279. };
  280. AVCodec rawvideo_codec = {
  281. "rawvideo",
  282. CODEC_TYPE_VIDEO,
  283. CODEC_ID_RAWVIDEO,
  284. 0,
  285. encode_init,
  286. encode_frame,
  287. NULL,
  288. decode_frame,
  289. };