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.

528 lines
13KB

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