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.

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