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.

542 lines
14KB

  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 "avcodec.h"
  20. #include "dsputil.h"
  21. #include "mpegvideo.h"
  22. #ifdef HAVE_MALLOC_H
  23. #include <malloc.h>
  24. #endif
  25. /* memory alloc */
  26. void *av_malloc(int size)
  27. {
  28. void *ptr;
  29. #if defined ( ARCH_X86 ) && defined ( HAVE_MEMALIGN )
  30. ptr = memalign(64,size);
  31. /* Why 64?
  32. Indeed, we should align it:
  33. on 4 for 386
  34. on 16 for 486
  35. on 32 for 586, PPro - k6-III
  36. on 64 for K7 (maybe for P3 too).
  37. Because L1 and L2 caches are aligned on those values.
  38. But I don't want to code such logic here!
  39. */
  40. #else
  41. ptr = malloc(size);
  42. #endif
  43. if (!ptr)
  44. return NULL;
  45. memset(ptr, 0, size);
  46. return ptr;
  47. }
  48. void *av_mallocz(int size)
  49. {
  50. void *ptr;
  51. ptr = av_malloc(size);
  52. if (!ptr)
  53. return NULL;
  54. memset(ptr, 0, size);
  55. return ptr;
  56. }
  57. /* NOTE: ptr = NULL is explicetly allowed */
  58. void av_free(void *ptr)
  59. {
  60. /* XXX: this test should not be needed on most libcs */
  61. if (ptr)
  62. free(ptr);
  63. }
  64. /* encoder management */
  65. AVCodec *first_avcodec;
  66. void register_avcodec(AVCodec *format)
  67. {
  68. AVCodec **p;
  69. p = &first_avcodec;
  70. while (*p != NULL) p = &(*p)->next;
  71. *p = format;
  72. format->next = NULL;
  73. }
  74. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  75. {
  76. int ret;
  77. avctx->codec = codec;
  78. avctx->frame_number = 0;
  79. if (codec->priv_data_size > 0) {
  80. avctx->priv_data = av_mallocz(codec->priv_data_size);
  81. if (!avctx->priv_data)
  82. return -ENOMEM;
  83. } else {
  84. avctx->priv_data = NULL;
  85. }
  86. ret = avctx->codec->init(avctx);
  87. if (ret < 0) {
  88. av_freep(&avctx->priv_data);
  89. return ret;
  90. }
  91. return 0;
  92. }
  93. int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  94. const short *samples)
  95. {
  96. int ret;
  97. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  98. avctx->frame_number++;
  99. return ret;
  100. }
  101. int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  102. const AVPicture *pict)
  103. {
  104. int ret;
  105. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  106. avctx->frame_number++;
  107. return ret;
  108. }
  109. /* decode a frame. return -1 if error, otherwise return the number of
  110. bytes used. If no frame could be decompressed, *got_picture_ptr is
  111. zero. Otherwise, it is non zero */
  112. int avcodec_decode_video(AVCodecContext *avctx, AVPicture *picture,
  113. int *got_picture_ptr,
  114. UINT8 *buf, int buf_size)
  115. {
  116. int ret;
  117. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  118. buf, buf_size);
  119. if (*got_picture_ptr)
  120. avctx->frame_number++;
  121. return ret;
  122. }
  123. /* decode an audio frame. return -1 if error, otherwise return the
  124. *number of bytes used. If no frame could be decompressed,
  125. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  126. *size in BYTES. */
  127. int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
  128. int *frame_size_ptr,
  129. UINT8 *buf, int buf_size)
  130. {
  131. int ret;
  132. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  133. buf, buf_size);
  134. avctx->frame_number++;
  135. return ret;
  136. }
  137. int avcodec_close(AVCodecContext *avctx)
  138. {
  139. if (avctx->codec->close)
  140. avctx->codec->close(avctx);
  141. av_freep(&avctx->priv_data);
  142. avctx->codec = NULL;
  143. return 0;
  144. }
  145. AVCodec *avcodec_find_encoder(enum CodecID id)
  146. {
  147. AVCodec *p;
  148. p = first_avcodec;
  149. while (p) {
  150. if (p->encode != NULL && p->id == id)
  151. return p;
  152. p = p->next;
  153. }
  154. return NULL;
  155. }
  156. AVCodec *avcodec_find_encoder_by_name(const char *name)
  157. {
  158. AVCodec *p;
  159. p = first_avcodec;
  160. while (p) {
  161. if (p->encode != NULL && strcmp(name,p->name) == 0)
  162. return p;
  163. p = p->next;
  164. }
  165. return NULL;
  166. }
  167. AVCodec *avcodec_find_decoder(enum CodecID id)
  168. {
  169. AVCodec *p;
  170. p = first_avcodec;
  171. while (p) {
  172. if (p->decode != NULL && p->id == id)
  173. return p;
  174. p = p->next;
  175. }
  176. return NULL;
  177. }
  178. AVCodec *avcodec_find_decoder_by_name(const char *name)
  179. {
  180. AVCodec *p;
  181. p = first_avcodec;
  182. while (p) {
  183. if (p->decode != NULL && strcmp(name,p->name) == 0)
  184. return p;
  185. p = p->next;
  186. }
  187. return NULL;
  188. }
  189. AVCodec *avcodec_find(enum CodecID id)
  190. {
  191. AVCodec *p;
  192. p = first_avcodec;
  193. while (p) {
  194. if (p->id == id)
  195. return p;
  196. p = p->next;
  197. }
  198. return NULL;
  199. }
  200. const char *pix_fmt_str[] = {
  201. "??",
  202. "yuv420p",
  203. "yuv422",
  204. "rgb24",
  205. "bgr24",
  206. "yuv422p",
  207. "yuv444p",
  208. };
  209. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  210. {
  211. const char *codec_name;
  212. AVCodec *p;
  213. char buf1[32];
  214. char channels_str[100];
  215. int bitrate;
  216. if (encode)
  217. p = avcodec_find_encoder(enc->codec_id);
  218. else
  219. p = avcodec_find_decoder(enc->codec_id);
  220. if (p) {
  221. codec_name = p->name;
  222. } else if (enc->codec_name[0] != '\0') {
  223. codec_name = enc->codec_name;
  224. } else {
  225. /* output avi tags */
  226. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  227. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  228. enc->codec_tag & 0xff,
  229. (enc->codec_tag >> 8) & 0xff,
  230. (enc->codec_tag >> 16) & 0xff,
  231. (enc->codec_tag >> 24) & 0xff);
  232. } else {
  233. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  234. }
  235. codec_name = buf1;
  236. }
  237. switch(enc->codec_type) {
  238. case CODEC_TYPE_VIDEO:
  239. snprintf(buf, buf_size,
  240. "Video: %s%s",
  241. codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
  242. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  243. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  244. ", %s",
  245. pix_fmt_str[enc->pix_fmt]);
  246. }
  247. if (enc->width) {
  248. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  249. ", %dx%d, %0.2f fps",
  250. enc->width, enc->height,
  251. (float)enc->frame_rate / FRAME_RATE_BASE);
  252. }
  253. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  254. ", q=%d-%d", enc->qmin, enc->qmax);
  255. bitrate = enc->bit_rate;
  256. break;
  257. case CODEC_TYPE_AUDIO:
  258. snprintf(buf, buf_size,
  259. "Audio: %s",
  260. codec_name);
  261. switch (enc->channels) {
  262. case 1:
  263. strcpy(channels_str, "mono");
  264. break;
  265. case 2:
  266. strcpy(channels_str, "stereo");
  267. break;
  268. case 6:
  269. strcpy(channels_str, "5:1");
  270. break;
  271. default:
  272. sprintf(channels_str, "%d channels", enc->channels);
  273. break;
  274. }
  275. if (enc->sample_rate) {
  276. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  277. ", %d Hz, %s",
  278. enc->sample_rate,
  279. channels_str);
  280. }
  281. /* for PCM codecs, compute bitrate directly */
  282. switch(enc->codec_id) {
  283. case CODEC_ID_PCM_S16LE:
  284. case CODEC_ID_PCM_S16BE:
  285. case CODEC_ID_PCM_U16LE:
  286. case CODEC_ID_PCM_U16BE:
  287. bitrate = enc->sample_rate * enc->channels * 16;
  288. break;
  289. case CODEC_ID_PCM_S8:
  290. case CODEC_ID_PCM_U8:
  291. case CODEC_ID_PCM_ALAW:
  292. case CODEC_ID_PCM_MULAW:
  293. bitrate = enc->sample_rate * enc->channels * 8;
  294. break;
  295. default:
  296. bitrate = enc->bit_rate;
  297. break;
  298. }
  299. break;
  300. default:
  301. abort();
  302. }
  303. if (bitrate != 0) {
  304. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  305. ", %d kb/s", bitrate / 1000);
  306. }
  307. }
  308. /* Picture field are filled with 'ptr' addresses */
  309. void avpicture_fill(AVPicture *picture, UINT8 *ptr,
  310. int pix_fmt, int width, int height)
  311. {
  312. int size;
  313. size = width * height;
  314. switch(pix_fmt) {
  315. case PIX_FMT_YUV420P:
  316. picture->data[0] = ptr;
  317. picture->data[1] = picture->data[0] + size;
  318. picture->data[2] = picture->data[1] + size / 4;
  319. picture->linesize[0] = width;
  320. picture->linesize[1] = width / 2;
  321. picture->linesize[2] = width / 2;
  322. break;
  323. case PIX_FMT_YUV422P:
  324. picture->data[0] = ptr;
  325. picture->data[1] = picture->data[0] + size;
  326. picture->data[2] = picture->data[1] + size / 2;
  327. picture->linesize[0] = width;
  328. picture->linesize[1] = width / 2;
  329. picture->linesize[2] = width / 2;
  330. break;
  331. case PIX_FMT_YUV444P:
  332. picture->data[0] = ptr;
  333. picture->data[1] = picture->data[0] + size;
  334. picture->data[2] = picture->data[1] + size;
  335. picture->linesize[0] = width;
  336. picture->linesize[1] = width;
  337. picture->linesize[2] = width;
  338. break;
  339. case PIX_FMT_RGB24:
  340. case PIX_FMT_BGR24:
  341. picture->data[0] = ptr;
  342. picture->data[1] = NULL;
  343. picture->data[2] = NULL;
  344. picture->linesize[0] = width * 3;
  345. break;
  346. case PIX_FMT_YUV422:
  347. picture->data[0] = ptr;
  348. picture->data[1] = NULL;
  349. picture->data[2] = NULL;
  350. picture->linesize[0] = width * 2;
  351. break;
  352. default:
  353. picture->data[0] = NULL;
  354. picture->data[1] = NULL;
  355. picture->data[2] = NULL;
  356. break;
  357. }
  358. }
  359. int avpicture_get_size(int pix_fmt, int width, int height)
  360. {
  361. int size;
  362. size = width * height;
  363. switch(pix_fmt) {
  364. case PIX_FMT_YUV420P:
  365. size = (size * 3) / 2;
  366. break;
  367. case PIX_FMT_YUV422P:
  368. size = (size * 2);
  369. break;
  370. case PIX_FMT_YUV444P:
  371. size = (size * 3);
  372. break;
  373. case PIX_FMT_RGB24:
  374. case PIX_FMT_BGR24:
  375. size = (size * 3);
  376. break;
  377. case PIX_FMT_YUV422:
  378. size = (size * 2);
  379. break;
  380. default:
  381. size = -1;
  382. break;
  383. }
  384. return size;
  385. }
  386. unsigned avcodec_version( void )
  387. {
  388. return LIBAVCODEC_VERSION_INT;
  389. }
  390. unsigned avcodec_build( void )
  391. {
  392. return LIBAVCODEC_BUILD;
  393. }
  394. /* must be called before any other functions */
  395. void avcodec_init(void)
  396. {
  397. static int inited = 0;
  398. if (inited != 0)
  399. return;
  400. inited = 1;
  401. dsputil_init();
  402. }
  403. /* simple call to use all the codecs */
  404. void avcodec_register_all(void)
  405. {
  406. static int inited = 0;
  407. if (inited != 0)
  408. return;
  409. inited = 1;
  410. /* encoders */
  411. #ifdef CONFIG_ENCODERS
  412. register_avcodec(&ac3_encoder);
  413. register_avcodec(&mp2_encoder);
  414. #ifdef CONFIG_MP3LAME
  415. register_avcodec(&mp3lame_encoder);
  416. #endif
  417. register_avcodec(&mpeg1video_encoder);
  418. register_avcodec(&h263_encoder);
  419. register_avcodec(&h263p_encoder);
  420. register_avcodec(&rv10_encoder);
  421. register_avcodec(&mjpeg_encoder);
  422. register_avcodec(&mpeg4_encoder);
  423. register_avcodec(&msmpeg4v1_encoder);
  424. register_avcodec(&msmpeg4v2_encoder);
  425. register_avcodec(&msmpeg4v3_encoder);
  426. #endif /* CONFIG_ENCODERS */
  427. register_avcodec(&rawvideo_codec);
  428. /* decoders */
  429. #ifdef CONFIG_DECODERS
  430. register_avcodec(&h263_decoder);
  431. register_avcodec(&mpeg4_decoder);
  432. register_avcodec(&msmpeg4v1_decoder);
  433. register_avcodec(&msmpeg4v2_decoder);
  434. register_avcodec(&msmpeg4v3_decoder);
  435. register_avcodec(&wmv1_decoder);
  436. register_avcodec(&mpeg_decoder);
  437. register_avcodec(&h263i_decoder);
  438. register_avcodec(&rv10_decoder);
  439. register_avcodec(&mjpeg_decoder);
  440. register_avcodec(&mp2_decoder);
  441. register_avcodec(&mp3_decoder);
  442. #ifdef CONFIG_AC3
  443. register_avcodec(&ac3_decoder);
  444. #endif
  445. #endif /* CONFIG_DECODERS */
  446. /* pcm codecs */
  447. #define PCM_CODEC(id, name) \
  448. register_avcodec(& name ## _encoder); \
  449. register_avcodec(& name ## _decoder); \
  450. PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le);
  451. PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be);
  452. PCM_CODEC(CODEC_ID_PCM_U16LE, pcm_u16le);
  453. PCM_CODEC(CODEC_ID_PCM_U16BE, pcm_u16be);
  454. PCM_CODEC(CODEC_ID_PCM_S8, pcm_s8);
  455. PCM_CODEC(CODEC_ID_PCM_U8, pcm_u8);
  456. PCM_CODEC(CODEC_ID_PCM_ALAW, pcm_alaw);
  457. PCM_CODEC(CODEC_ID_PCM_MULAW, pcm_mulaw);
  458. #undef PCM_CODEC
  459. }
  460. /* this should be called after seeking and before trying to decode the next frame */
  461. void avcodec_flush_buffers(AVCodecContext *avctx)
  462. {
  463. MpegEncContext *s = avctx->priv_data;
  464. s->num_available_buffers=0;
  465. }
  466. static int encode_init(AVCodecContext *s)
  467. {
  468. return 0;
  469. }
  470. static int decode_frame(AVCodecContext *avctx,
  471. void *data, int *data_size,
  472. UINT8 *buf, int buf_size)
  473. {
  474. return -1;
  475. }
  476. static int encode_frame(AVCodecContext *avctx,
  477. unsigned char *frame, int buf_size, void *data)
  478. {
  479. return -1;
  480. }
  481. AVCodec rawvideo_codec = {
  482. "rawvideo",
  483. CODEC_TYPE_VIDEO,
  484. CODEC_ID_RAWVIDEO,
  485. 0,
  486. encode_init,
  487. encode_frame,
  488. NULL,
  489. decode_frame,
  490. };