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.

417 lines
10KB

  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. const char *pix_fmt_str[] = {
  159. "yuv420p",
  160. "yuv422",
  161. "rgb24",
  162. "bgr24",
  163. "yuv422p",
  164. "yuv444p",
  165. };
  166. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  167. {
  168. const char *codec_name;
  169. AVCodec *p;
  170. char buf1[32];
  171. if (encode)
  172. p = avcodec_find_encoder(enc->codec_id);
  173. else
  174. p = avcodec_find_decoder(enc->codec_id);
  175. if (p) {
  176. codec_name = p->name;
  177. } else if (enc->codec_name[0] != '\0') {
  178. codec_name = enc->codec_name;
  179. } else {
  180. /* output avi tags */
  181. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  182. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  183. enc->codec_tag & 0xff,
  184. (enc->codec_tag >> 8) & 0xff,
  185. (enc->codec_tag >> 16) & 0xff,
  186. (enc->codec_tag >> 24) & 0xff);
  187. } else {
  188. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  189. }
  190. codec_name = buf1;
  191. }
  192. switch(enc->codec_type) {
  193. case CODEC_TYPE_VIDEO:
  194. snprintf(buf, buf_size,
  195. "Video: %s%s",
  196. codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
  197. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  198. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  199. ", %s",
  200. pix_fmt_str[enc->pix_fmt]);
  201. }
  202. if (enc->width) {
  203. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  204. ", %dx%d, %0.2f fps",
  205. enc->width, enc->height,
  206. (float)enc->frame_rate / FRAME_RATE_BASE);
  207. }
  208. break;
  209. case CODEC_TYPE_AUDIO:
  210. snprintf(buf, buf_size,
  211. "Audio: %s",
  212. codec_name);
  213. if (enc->sample_rate) {
  214. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  215. ", %d Hz, %s",
  216. enc->sample_rate,
  217. enc->channels == 2 ? "stereo" : "mono");
  218. }
  219. break;
  220. default:
  221. abort();
  222. }
  223. if (enc->bit_rate != 0) {
  224. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  225. ", %d kb/s", enc->bit_rate / 1000);
  226. }
  227. }
  228. /* Picture field are filled with 'ptr' addresses */
  229. void avpicture_fill(AVPicture *picture, UINT8 *ptr,
  230. int pix_fmt, int width, int height)
  231. {
  232. int size;
  233. size = width * height;
  234. switch(pix_fmt) {
  235. case PIX_FMT_YUV420P:
  236. picture->data[0] = ptr;
  237. picture->data[1] = picture->data[0] + size;
  238. picture->data[2] = picture->data[1] + size / 4;
  239. picture->linesize[0] = width;
  240. picture->linesize[1] = width / 2;
  241. picture->linesize[2] = width / 2;
  242. break;
  243. case PIX_FMT_YUV422P:
  244. picture->data[0] = ptr;
  245. picture->data[1] = picture->data[0] + size;
  246. picture->data[2] = picture->data[1] + size / 2;
  247. picture->linesize[0] = width;
  248. picture->linesize[1] = width / 2;
  249. picture->linesize[2] = width / 2;
  250. break;
  251. case PIX_FMT_YUV444P:
  252. picture->data[0] = ptr;
  253. picture->data[1] = picture->data[0] + size;
  254. picture->data[2] = picture->data[1] + size;
  255. picture->linesize[0] = width;
  256. picture->linesize[1] = width;
  257. picture->linesize[2] = width;
  258. break;
  259. case PIX_FMT_RGB24:
  260. case PIX_FMT_BGR24:
  261. picture->data[0] = ptr;
  262. picture->data[1] = NULL;
  263. picture->data[2] = NULL;
  264. picture->linesize[0] = width * 3;
  265. break;
  266. case PIX_FMT_YUV422:
  267. picture->data[0] = ptr;
  268. picture->data[1] = NULL;
  269. picture->data[2] = NULL;
  270. picture->linesize[0] = width * 2;
  271. break;
  272. default:
  273. picture->data[0] = NULL;
  274. picture->data[1] = NULL;
  275. picture->data[2] = NULL;
  276. break;
  277. }
  278. }
  279. int avpicture_get_size(int pix_fmt, int width, int height)
  280. {
  281. int size;
  282. size = width * height;
  283. switch(pix_fmt) {
  284. case PIX_FMT_YUV420P:
  285. size = (size * 3) / 2;
  286. break;
  287. case PIX_FMT_YUV422P:
  288. size = (size * 2);
  289. break;
  290. case PIX_FMT_YUV444P:
  291. size = (size * 3);
  292. break;
  293. case PIX_FMT_RGB24:
  294. case PIX_FMT_BGR24:
  295. size = (size * 3);
  296. break;
  297. case PIX_FMT_YUV422:
  298. size = (size * 2);
  299. break;
  300. default:
  301. size = -1;
  302. break;
  303. }
  304. return size;
  305. }
  306. /* must be called before any other functions */
  307. void avcodec_init(void)
  308. {
  309. dsputil_init();
  310. }
  311. /* simple call to use all the codecs */
  312. void avcodec_register_all(void)
  313. {
  314. /* encoders */
  315. #ifdef CONFIG_ENCODERS
  316. register_avcodec(&ac3_encoder);
  317. register_avcodec(&mp2_encoder);
  318. register_avcodec(&mpeg1video_encoder);
  319. register_avcodec(&h263_encoder);
  320. register_avcodec(&h263p_encoder);
  321. register_avcodec(&rv10_encoder);
  322. register_avcodec(&mjpeg_encoder);
  323. register_avcodec(&mpeg4_encoder);
  324. register_avcodec(&msmpeg4_encoder);
  325. #endif /* CONFIG_ENCODERS */
  326. register_avcodec(&pcm_codec);
  327. register_avcodec(&rawvideo_codec);
  328. /* decoders */
  329. #ifdef CONFIG_DECODERS
  330. register_avcodec(&h263_decoder);
  331. register_avcodec(&mpeg4_decoder);
  332. register_avcodec(&msmpeg4_decoder);
  333. register_avcodec(&mpeg_decoder);
  334. register_avcodec(&h263i_decoder);
  335. register_avcodec(&rv10_decoder);
  336. register_avcodec(&mjpeg_decoder);
  337. #ifdef CONFIG_MPGLIB
  338. register_avcodec(&mp3_decoder);
  339. #endif
  340. #ifdef CONFIG_AC3
  341. register_avcodec(&ac3_decoder);
  342. #endif
  343. #endif /* CONFIG_DECODERS */
  344. }
  345. static int encode_init(AVCodecContext *s)
  346. {
  347. return 0;
  348. }
  349. static int decode_frame(AVCodecContext *avctx,
  350. void *data, int *data_size,
  351. UINT8 *buf, int buf_size)
  352. {
  353. return -1;
  354. }
  355. static int encode_frame(AVCodecContext *avctx,
  356. unsigned char *frame, int buf_size, void *data)
  357. {
  358. return -1;
  359. }
  360. /* dummy pcm codec */
  361. AVCodec pcm_codec = {
  362. "pcm",
  363. CODEC_TYPE_AUDIO,
  364. CODEC_ID_PCM,
  365. 0,
  366. encode_init,
  367. encode_frame,
  368. NULL,
  369. decode_frame,
  370. };
  371. AVCodec rawvideo_codec = {
  372. "rawvideo",
  373. CODEC_TYPE_VIDEO,
  374. CODEC_ID_RAWVIDEO,
  375. 0,
  376. encode_init,
  377. encode_frame,
  378. NULL,
  379. decode_frame,
  380. };