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.

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