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.

549 lines
14KB

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