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.

472 lines
12KB

  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. if (*got_picture_ptr)
  106. avctx->frame_number++;
  107. return ret;
  108. }
  109. /* decode an audio frame. return -1 if error, otherwise return the
  110. *number of bytes used. If no frame could be decompressed,
  111. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  112. *size in BYTES. */
  113. int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
  114. int *frame_size_ptr,
  115. UINT8 *buf, int buf_size)
  116. {
  117. int ret;
  118. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  119. buf, buf_size);
  120. avctx->frame_number++;
  121. return ret;
  122. }
  123. int avcodec_close(AVCodecContext *avctx)
  124. {
  125. if (avctx->codec->close)
  126. avctx->codec->close(avctx);
  127. free(avctx->priv_data);
  128. avctx->priv_data = NULL;
  129. avctx->codec = NULL;
  130. return 0;
  131. }
  132. AVCodec *avcodec_find_encoder(enum CodecID id)
  133. {
  134. AVCodec *p;
  135. p = first_avcodec;
  136. while (p) {
  137. if (p->encode != NULL && p->id == id)
  138. return p;
  139. p = p->next;
  140. }
  141. return NULL;
  142. }
  143. AVCodec *avcodec_find_encoder_by_name(const char *name)
  144. {
  145. AVCodec *p;
  146. p = first_avcodec;
  147. while (p) {
  148. if (p->encode != NULL && strcmp(name,p->name) == 0)
  149. return p;
  150. p = p->next;
  151. }
  152. return NULL;
  153. }
  154. AVCodec *avcodec_find_decoder(enum CodecID id)
  155. {
  156. AVCodec *p;
  157. p = first_avcodec;
  158. while (p) {
  159. if (p->decode != NULL && p->id == id)
  160. return p;
  161. p = p->next;
  162. }
  163. return NULL;
  164. }
  165. AVCodec *avcodec_find_decoder_by_name(const char *name)
  166. {
  167. AVCodec *p;
  168. p = first_avcodec;
  169. while (p) {
  170. if (p->decode != NULL && strcmp(name,p->name) == 0)
  171. return p;
  172. p = p->next;
  173. }
  174. return NULL;
  175. }
  176. AVCodec *avcodec_find(enum CodecID id)
  177. {
  178. AVCodec *p;
  179. p = first_avcodec;
  180. while (p) {
  181. if (p->id == id)
  182. return p;
  183. p = p->next;
  184. }
  185. return NULL;
  186. }
  187. const char *pix_fmt_str[] = {
  188. "yuv420p",
  189. "yuv422",
  190. "rgb24",
  191. "bgr24",
  192. "yuv422p",
  193. "yuv444p",
  194. };
  195. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  196. {
  197. const char *codec_name;
  198. AVCodec *p;
  199. char buf1[32];
  200. int bitrate;
  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. bitrate = enc->bit_rate;
  239. break;
  240. case CODEC_TYPE_AUDIO:
  241. snprintf(buf, buf_size,
  242. "Audio: %s",
  243. codec_name);
  244. if (enc->sample_rate) {
  245. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  246. ", %d Hz, %s",
  247. enc->sample_rate,
  248. enc->channels == 2 ? "stereo" : "mono");
  249. }
  250. /* for PCM codecs, compute bitrate directly */
  251. switch(enc->codec_id) {
  252. case CODEC_ID_PCM_S16LE:
  253. case CODEC_ID_PCM_S16BE:
  254. case CODEC_ID_PCM_U16LE:
  255. case CODEC_ID_PCM_U16BE:
  256. bitrate = enc->sample_rate * enc->channels * 16;
  257. break;
  258. case CODEC_ID_PCM_S8:
  259. case CODEC_ID_PCM_U8:
  260. case CODEC_ID_PCM_ALAW:
  261. case CODEC_ID_PCM_MULAW:
  262. bitrate = enc->sample_rate * enc->channels * 8;
  263. break;
  264. default:
  265. bitrate = enc->bit_rate;
  266. break;
  267. }
  268. break;
  269. default:
  270. abort();
  271. }
  272. if (bitrate != 0) {
  273. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  274. ", %d kb/s", bitrate / 1000);
  275. }
  276. }
  277. /* Picture field are filled with 'ptr' addresses */
  278. void avpicture_fill(AVPicture *picture, UINT8 *ptr,
  279. 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. picture->data[0] = ptr;
  286. picture->data[1] = picture->data[0] + size;
  287. picture->data[2] = picture->data[1] + size / 4;
  288. picture->linesize[0] = width;
  289. picture->linesize[1] = width / 2;
  290. picture->linesize[2] = width / 2;
  291. break;
  292. case PIX_FMT_YUV422P:
  293. picture->data[0] = ptr;
  294. picture->data[1] = picture->data[0] + size;
  295. picture->data[2] = picture->data[1] + size / 2;
  296. picture->linesize[0] = width;
  297. picture->linesize[1] = width / 2;
  298. picture->linesize[2] = width / 2;
  299. break;
  300. case PIX_FMT_YUV444P:
  301. picture->data[0] = ptr;
  302. picture->data[1] = picture->data[0] + size;
  303. picture->data[2] = picture->data[1] + size;
  304. picture->linesize[0] = width;
  305. picture->linesize[1] = width;
  306. picture->linesize[2] = width;
  307. break;
  308. case PIX_FMT_RGB24:
  309. case PIX_FMT_BGR24:
  310. picture->data[0] = ptr;
  311. picture->data[1] = NULL;
  312. picture->data[2] = NULL;
  313. picture->linesize[0] = width * 3;
  314. break;
  315. case PIX_FMT_YUV422:
  316. picture->data[0] = ptr;
  317. picture->data[1] = NULL;
  318. picture->data[2] = NULL;
  319. picture->linesize[0] = width * 2;
  320. break;
  321. default:
  322. picture->data[0] = NULL;
  323. picture->data[1] = NULL;
  324. picture->data[2] = NULL;
  325. break;
  326. }
  327. }
  328. int avpicture_get_size(int pix_fmt, int width, int height)
  329. {
  330. int size;
  331. size = width * height;
  332. switch(pix_fmt) {
  333. case PIX_FMT_YUV420P:
  334. size = (size * 3) / 2;
  335. break;
  336. case PIX_FMT_YUV422P:
  337. size = (size * 2);
  338. break;
  339. case PIX_FMT_YUV444P:
  340. size = (size * 3);
  341. break;
  342. case PIX_FMT_RGB24:
  343. case PIX_FMT_BGR24:
  344. size = (size * 3);
  345. break;
  346. case PIX_FMT_YUV422:
  347. size = (size * 2);
  348. break;
  349. default:
  350. size = -1;
  351. break;
  352. }
  353. return size;
  354. }
  355. /* must be called before any other functions */
  356. void avcodec_init(void)
  357. {
  358. dsputil_init();
  359. }
  360. /* simple call to use all the codecs */
  361. void avcodec_register_all(void)
  362. {
  363. /* encoders */
  364. #ifdef CONFIG_ENCODERS
  365. register_avcodec(&ac3_encoder);
  366. register_avcodec(&mp2_encoder);
  367. #ifdef CONFIG_MP3LAME
  368. register_avcodec(&mp3lame_encoder);
  369. #endif
  370. register_avcodec(&mpeg1video_encoder);
  371. register_avcodec(&h263_encoder);
  372. register_avcodec(&h263p_encoder);
  373. register_avcodec(&rv10_encoder);
  374. register_avcodec(&mjpeg_encoder);
  375. register_avcodec(&mpeg4_encoder);
  376. register_avcodec(&msmpeg4_encoder);
  377. #endif /* CONFIG_ENCODERS */
  378. register_avcodec(&rawvideo_codec);
  379. /* decoders */
  380. #ifdef CONFIG_DECODERS
  381. register_avcodec(&h263_decoder);
  382. register_avcodec(&mpeg4_decoder);
  383. register_avcodec(&msmpeg4_decoder);
  384. register_avcodec(&mpeg_decoder);
  385. register_avcodec(&h263i_decoder);
  386. register_avcodec(&rv10_decoder);
  387. register_avcodec(&mjpeg_decoder);
  388. register_avcodec(&mp3_decoder);
  389. #ifdef CONFIG_AC3
  390. register_avcodec(&ac3_decoder);
  391. #endif
  392. #endif /* CONFIG_DECODERS */
  393. /* pcm codecs */
  394. #define PCM_CODEC(id, name) \
  395. register_avcodec(& name ## _encoder); \
  396. register_avcodec(& name ## _decoder); \
  397. PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le);
  398. PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be);
  399. PCM_CODEC(CODEC_ID_PCM_U16LE, pcm_u16le);
  400. PCM_CODEC(CODEC_ID_PCM_U16BE, pcm_u16be);
  401. PCM_CODEC(CODEC_ID_PCM_S8, pcm_s8);
  402. PCM_CODEC(CODEC_ID_PCM_U8, pcm_u8);
  403. PCM_CODEC(CODEC_ID_PCM_ALAW, pcm_alaw);
  404. PCM_CODEC(CODEC_ID_PCM_MULAW, pcm_mulaw);
  405. #undef PCM_CODEC
  406. }
  407. static int encode_init(AVCodecContext *s)
  408. {
  409. return 0;
  410. }
  411. static int decode_frame(AVCodecContext *avctx,
  412. void *data, int *data_size,
  413. UINT8 *buf, int buf_size)
  414. {
  415. return -1;
  416. }
  417. static int encode_frame(AVCodecContext *avctx,
  418. unsigned char *frame, int buf_size, void *data)
  419. {
  420. return -1;
  421. }
  422. AVCodec rawvideo_codec = {
  423. "rawvideo",
  424. CODEC_TYPE_VIDEO,
  425. CODEC_ID_RAWVIDEO,
  426. 0,
  427. encode_init,
  428. encode_frame,
  429. NULL,
  430. decode_frame,
  431. };