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.

448 lines
11KB

  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 <limits.h> /* __GLIBC__ and __GLIBC_MINOR__ are defined here */
  24. #if __GLIBC__ >=2 && __GLIBC_MINOR__ >= 1 /* Fixme about glibc-2.0 */
  25. #define HAVE_MEMALIGN 1
  26. #include <malloc.h>
  27. #endif
  28. #include "common.h"
  29. #include "dsputil.h"
  30. #include "avcodec.h"
  31. /* memory alloc */
  32. void *av_mallocz(int size)
  33. {
  34. void *ptr;
  35. #if defined ( ARCH_X86 ) && defined ( HAVE_MEMALIGN )
  36. /*
  37. From glibc-2.1.x manuals:
  38. -------------------------
  39. The address of a block returned by `malloc' or `realloc' in the GNU
  40. system is always a multiple of eight (or sixteen on 64-bit systems).
  41. If you need a block whose address is a multiple of a higher power of
  42. two than that, use `memalign' or `valloc'. These functions are
  43. declared in `stdlib.h'.
  44. With the GNU library, you can use `free' to free the blocks that
  45. `memalign' and `valloc' return. That does not work in BSD,
  46. however--BSD does not provide any way to free such blocks.
  47. */
  48. ptr = memalign(64,size);
  49. /* Why 64?
  50. Indeed, we should align it:
  51. on 4 for 386
  52. on 16 for 486
  53. on 32 for 586, PPro - k6-III
  54. on 64 for K7 (maybe for P3 too).
  55. Because L1 and L2 caches are aligned on those values.
  56. But I don't want to code such logic here!
  57. */
  58. #else
  59. ptr = malloc(size);
  60. #endif
  61. if (!ptr)
  62. return NULL;
  63. memset(ptr, 0, size);
  64. return ptr;
  65. }
  66. /* encoder management */
  67. AVCodec *first_avcodec;
  68. void register_avcodec(AVCodec *format)
  69. {
  70. AVCodec **p;
  71. p = &first_avcodec;
  72. while (*p != NULL) p = &(*p)->next;
  73. *p = format;
  74. format->next = NULL;
  75. }
  76. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  77. {
  78. int ret;
  79. avctx->codec = codec;
  80. avctx->frame_number = 0;
  81. avctx->priv_data = av_mallocz(codec->priv_data_size);
  82. if (!avctx->priv_data)
  83. return -ENOMEM;
  84. ret = avctx->codec->init(avctx);
  85. if (ret < 0) {
  86. free(avctx->priv_data);
  87. avctx->priv_data = NULL;
  88. return ret;
  89. }
  90. return 0;
  91. }
  92. int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  93. const short *samples)
  94. {
  95. int ret;
  96. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  97. avctx->frame_number++;
  98. return ret;
  99. }
  100. int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  101. const AVPicture *pict)
  102. {
  103. int ret;
  104. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  105. avctx->frame_number++;
  106. return ret;
  107. }
  108. /* decode a frame. return -1 if error, otherwise return the number of
  109. bytes used. If no frame could be decompressed, *got_picture_ptr is
  110. zero. Otherwise, it is non zero */
  111. int avcodec_decode_video(AVCodecContext *avctx, AVPicture *picture,
  112. int *got_picture_ptr,
  113. UINT8 *buf, int buf_size)
  114. {
  115. int ret;
  116. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  117. buf, buf_size);
  118. avctx->frame_number++;
  119. return ret;
  120. }
  121. /* decode an audio frame. return -1 if error, otherwise return the
  122. *number of bytes used. If no frame could be decompressed,
  123. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  124. *size in BYTES. */
  125. int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
  126. int *frame_size_ptr,
  127. UINT8 *buf, int buf_size)
  128. {
  129. int ret;
  130. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  131. buf, buf_size);
  132. avctx->frame_number++;
  133. return ret;
  134. }
  135. int avcodec_close(AVCodecContext *avctx)
  136. {
  137. if (avctx->codec->close)
  138. avctx->codec->close(avctx);
  139. free(avctx->priv_data);
  140. avctx->priv_data = NULL;
  141. avctx->codec = NULL;
  142. return 0;
  143. }
  144. AVCodec *avcodec_find_encoder(enum CodecID id)
  145. {
  146. AVCodec *p;
  147. p = first_avcodec;
  148. while (p) {
  149. if (p->encode != NULL && p->id == id)
  150. return p;
  151. p = p->next;
  152. }
  153. return NULL;
  154. }
  155. AVCodec *avcodec_find_decoder(enum CodecID id)
  156. {
  157. AVCodec *p;
  158. p = first_avcodec;
  159. while (p) {
  160. if (p->decode != NULL && p->id == id)
  161. return p;
  162. p = p->next;
  163. }
  164. return NULL;
  165. }
  166. AVCodec *avcodec_find_decoder_by_name(const char *name)
  167. {
  168. AVCodec *p;
  169. p = first_avcodec;
  170. while (p) {
  171. if (p->decode != NULL && strcmp(name,p->name) == 0)
  172. return p;
  173. p = p->next;
  174. }
  175. return NULL;
  176. }
  177. AVCodec *avcodec_find(enum CodecID id)
  178. {
  179. AVCodec *p;
  180. p = first_avcodec;
  181. while (p) {
  182. if (p->id == id)
  183. return p;
  184. p = p->next;
  185. }
  186. return NULL;
  187. }
  188. const char *pix_fmt_str[] = {
  189. "yuv420p",
  190. "yuv422",
  191. "rgb24",
  192. "bgr24",
  193. "yuv422p",
  194. "yuv444p",
  195. };
  196. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  197. {
  198. const char *codec_name;
  199. AVCodec *p;
  200. char buf1[32];
  201. if (encode)
  202. p = avcodec_find_encoder(enc->codec_id);
  203. else
  204. p = avcodec_find_decoder(enc->codec_id);
  205. if (p) {
  206. codec_name = p->name;
  207. } else if (enc->codec_name[0] != '\0') {
  208. codec_name = enc->codec_name;
  209. } else {
  210. /* output avi tags */
  211. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  212. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  213. enc->codec_tag & 0xff,
  214. (enc->codec_tag >> 8) & 0xff,
  215. (enc->codec_tag >> 16) & 0xff,
  216. (enc->codec_tag >> 24) & 0xff);
  217. } else {
  218. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  219. }
  220. codec_name = buf1;
  221. }
  222. switch(enc->codec_type) {
  223. case CODEC_TYPE_VIDEO:
  224. snprintf(buf, buf_size,
  225. "Video: %s%s",
  226. codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
  227. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  228. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  229. ", %s",
  230. pix_fmt_str[enc->pix_fmt]);
  231. }
  232. if (enc->width) {
  233. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  234. ", %dx%d, %0.2f fps",
  235. enc->width, enc->height,
  236. (float)enc->frame_rate / FRAME_RATE_BASE);
  237. }
  238. break;
  239. case CODEC_TYPE_AUDIO:
  240. snprintf(buf, buf_size,
  241. "Audio: %s",
  242. codec_name);
  243. if (enc->sample_rate) {
  244. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  245. ", %d Hz, %s",
  246. enc->sample_rate,
  247. enc->channels == 2 ? "stereo" : "mono");
  248. }
  249. break;
  250. default:
  251. abort();
  252. }
  253. if (enc->bit_rate != 0) {
  254. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  255. ", %d kb/s", enc->bit_rate / 1000);
  256. }
  257. }
  258. /* Picture field are filled with 'ptr' addresses */
  259. void avpicture_fill(AVPicture *picture, UINT8 *ptr,
  260. int pix_fmt, int width, int height)
  261. {
  262. int size;
  263. size = width * height;
  264. switch(pix_fmt) {
  265. case PIX_FMT_YUV420P:
  266. picture->data[0] = ptr;
  267. picture->data[1] = picture->data[0] + size;
  268. picture->data[2] = picture->data[1] + size / 4;
  269. picture->linesize[0] = width;
  270. picture->linesize[1] = width / 2;
  271. picture->linesize[2] = width / 2;
  272. break;
  273. case PIX_FMT_YUV422P:
  274. picture->data[0] = ptr;
  275. picture->data[1] = picture->data[0] + size;
  276. picture->data[2] = picture->data[1] + size / 2;
  277. picture->linesize[0] = width;
  278. picture->linesize[1] = width / 2;
  279. picture->linesize[2] = width / 2;
  280. break;
  281. case PIX_FMT_YUV444P:
  282. picture->data[0] = ptr;
  283. picture->data[1] = picture->data[0] + size;
  284. picture->data[2] = picture->data[1] + size;
  285. picture->linesize[0] = width;
  286. picture->linesize[1] = width;
  287. picture->linesize[2] = width;
  288. break;
  289. case PIX_FMT_RGB24:
  290. case PIX_FMT_BGR24:
  291. picture->data[0] = ptr;
  292. picture->data[1] = NULL;
  293. picture->data[2] = NULL;
  294. picture->linesize[0] = width * 3;
  295. break;
  296. case PIX_FMT_YUV422:
  297. picture->data[0] = ptr;
  298. picture->data[1] = NULL;
  299. picture->data[2] = NULL;
  300. picture->linesize[0] = width * 2;
  301. break;
  302. default:
  303. picture->data[0] = NULL;
  304. picture->data[1] = NULL;
  305. picture->data[2] = NULL;
  306. break;
  307. }
  308. }
  309. int avpicture_get_size(int pix_fmt, int width, int height)
  310. {
  311. int size;
  312. size = width * height;
  313. switch(pix_fmt) {
  314. case PIX_FMT_YUV420P:
  315. size = (size * 3) / 2;
  316. break;
  317. case PIX_FMT_YUV422P:
  318. size = (size * 2);
  319. break;
  320. case PIX_FMT_YUV444P:
  321. size = (size * 3);
  322. break;
  323. case PIX_FMT_RGB24:
  324. case PIX_FMT_BGR24:
  325. size = (size * 3);
  326. break;
  327. case PIX_FMT_YUV422:
  328. size = (size * 2);
  329. break;
  330. default:
  331. size = -1;
  332. break;
  333. }
  334. return size;
  335. }
  336. /* must be called before any other functions */
  337. void avcodec_init(void)
  338. {
  339. dsputil_init();
  340. }
  341. /* simple call to use all the codecs */
  342. void avcodec_register_all(void)
  343. {
  344. /* encoders */
  345. #ifdef CONFIG_ENCODERS
  346. register_avcodec(&ac3_encoder);
  347. register_avcodec(&mp2_encoder);
  348. register_avcodec(&mpeg1video_encoder);
  349. register_avcodec(&h263_encoder);
  350. register_avcodec(&h263p_encoder);
  351. register_avcodec(&rv10_encoder);
  352. register_avcodec(&mjpeg_encoder);
  353. register_avcodec(&mpeg4_encoder);
  354. register_avcodec(&msmpeg4_encoder);
  355. #endif /* CONFIG_ENCODERS */
  356. register_avcodec(&pcm_codec);
  357. register_avcodec(&rawvideo_codec);
  358. /* decoders */
  359. #ifdef CONFIG_DECODERS
  360. register_avcodec(&h263_decoder);
  361. register_avcodec(&mpeg4_decoder);
  362. register_avcodec(&msmpeg4_decoder);
  363. register_avcodec(&mpeg_decoder);
  364. register_avcodec(&h263i_decoder);
  365. register_avcodec(&rv10_decoder);
  366. register_avcodec(&mjpeg_decoder);
  367. #ifdef CONFIG_MPGLIB
  368. register_avcodec(&mp3_decoder);
  369. #endif
  370. #ifdef CONFIG_AC3
  371. register_avcodec(&ac3_decoder);
  372. #endif
  373. #endif /* CONFIG_DECODERS */
  374. }
  375. static int encode_init(AVCodecContext *s)
  376. {
  377. return 0;
  378. }
  379. static int decode_frame(AVCodecContext *avctx,
  380. void *data, int *data_size,
  381. UINT8 *buf, int buf_size)
  382. {
  383. return -1;
  384. }
  385. static int encode_frame(AVCodecContext *avctx,
  386. unsigned char *frame, int buf_size, void *data)
  387. {
  388. return -1;
  389. }
  390. /* dummy pcm codec */
  391. AVCodec pcm_codec = {
  392. "pcm",
  393. CODEC_TYPE_AUDIO,
  394. CODEC_ID_PCM,
  395. 0,
  396. encode_init,
  397. encode_frame,
  398. NULL,
  399. decode_frame,
  400. };
  401. AVCodec rawvideo_codec = {
  402. "rawvideo",
  403. CODEC_TYPE_VIDEO,
  404. CODEC_ID_RAWVIDEO,
  405. 0,
  406. encode_init,
  407. encode_frame,
  408. NULL,
  409. decode_frame,
  410. };