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.

510 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. #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. "??",
  189. "yuv420p",
  190. "yuv422",
  191. "rgb24",
  192. "bgr24",
  193. "yuv422p",
  194. "yuv444p",
  195. };
  196. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  197. {
  198. const char *codec_name;
  199. AVCodec *p;
  200. char buf1[32];
  201. char *channels_str=NULL;
  202. int bitrate;
  203. if (encode)
  204. p = avcodec_find_encoder(enc->codec_id);
  205. else
  206. p = avcodec_find_decoder(enc->codec_id);
  207. if (p) {
  208. codec_name = p->name;
  209. } else if (enc->codec_name[0] != '\0') {
  210. codec_name = enc->codec_name;
  211. } else {
  212. /* output avi tags */
  213. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  214. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  215. enc->codec_tag & 0xff,
  216. (enc->codec_tag >> 8) & 0xff,
  217. (enc->codec_tag >> 16) & 0xff,
  218. (enc->codec_tag >> 24) & 0xff);
  219. } else {
  220. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  221. }
  222. codec_name = buf1;
  223. }
  224. switch(enc->codec_type) {
  225. case CODEC_TYPE_VIDEO:
  226. snprintf(buf, buf_size,
  227. "Video: %s%s",
  228. codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
  229. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  230. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  231. ", %s",
  232. pix_fmt_str[enc->pix_fmt]);
  233. }
  234. if (enc->width) {
  235. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  236. ", %dx%d, %0.2f fps",
  237. enc->width, enc->height,
  238. (float)enc->frame_rate / FRAME_RATE_BASE);
  239. }
  240. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  241. ", q=%d-%d", enc->qmin, enc->qmax);
  242. bitrate = enc->bit_rate;
  243. break;
  244. case CODEC_TYPE_AUDIO:
  245. snprintf(buf, buf_size,
  246. "Audio: %s",
  247. codec_name);
  248. switch (enc->channels) {
  249. case 1:
  250. channels_str = "mono";
  251. break;
  252. case 2:
  253. channels_str = "stereo";
  254. break;
  255. case 6:
  256. channels_str = "5:1";
  257. break;
  258. default:
  259. sprintf(channels_str, "%d channels", enc->channels);
  260. break;
  261. }
  262. if (enc->sample_rate) {
  263. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  264. ", %d Hz, %s",
  265. enc->sample_rate,
  266. channels_str);
  267. }
  268. /* for PCM codecs, compute bitrate directly */
  269. switch(enc->codec_id) {
  270. case CODEC_ID_PCM_S16LE:
  271. case CODEC_ID_PCM_S16BE:
  272. case CODEC_ID_PCM_U16LE:
  273. case CODEC_ID_PCM_U16BE:
  274. bitrate = enc->sample_rate * enc->channels * 16;
  275. break;
  276. case CODEC_ID_PCM_S8:
  277. case CODEC_ID_PCM_U8:
  278. case CODEC_ID_PCM_ALAW:
  279. case CODEC_ID_PCM_MULAW:
  280. bitrate = enc->sample_rate * enc->channels * 8;
  281. break;
  282. default:
  283. bitrate = enc->bit_rate;
  284. break;
  285. }
  286. break;
  287. default:
  288. abort();
  289. }
  290. if (bitrate != 0) {
  291. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  292. ", %d kb/s", bitrate / 1000);
  293. }
  294. }
  295. /* Picture field are filled with 'ptr' addresses */
  296. void avpicture_fill(AVPicture *picture, UINT8 *ptr,
  297. int pix_fmt, int width, int height)
  298. {
  299. int size;
  300. size = width * height;
  301. switch(pix_fmt) {
  302. case PIX_FMT_YUV420P:
  303. picture->data[0] = ptr;
  304. picture->data[1] = picture->data[0] + size;
  305. picture->data[2] = picture->data[1] + size / 4;
  306. picture->linesize[0] = width;
  307. picture->linesize[1] = width / 2;
  308. picture->linesize[2] = width / 2;
  309. break;
  310. case PIX_FMT_YUV422P:
  311. picture->data[0] = ptr;
  312. picture->data[1] = picture->data[0] + size;
  313. picture->data[2] = picture->data[1] + size / 2;
  314. picture->linesize[0] = width;
  315. picture->linesize[1] = width / 2;
  316. picture->linesize[2] = width / 2;
  317. break;
  318. case PIX_FMT_YUV444P:
  319. picture->data[0] = ptr;
  320. picture->data[1] = picture->data[0] + size;
  321. picture->data[2] = picture->data[1] + size;
  322. picture->linesize[0] = width;
  323. picture->linesize[1] = width;
  324. picture->linesize[2] = width;
  325. break;
  326. case PIX_FMT_RGB24:
  327. case PIX_FMT_BGR24:
  328. picture->data[0] = ptr;
  329. picture->data[1] = NULL;
  330. picture->data[2] = NULL;
  331. picture->linesize[0] = width * 3;
  332. break;
  333. case PIX_FMT_YUV422:
  334. picture->data[0] = ptr;
  335. picture->data[1] = NULL;
  336. picture->data[2] = NULL;
  337. picture->linesize[0] = width * 2;
  338. break;
  339. default:
  340. picture->data[0] = NULL;
  341. picture->data[1] = NULL;
  342. picture->data[2] = NULL;
  343. break;
  344. }
  345. }
  346. int avpicture_get_size(int pix_fmt, int width, int height)
  347. {
  348. int size;
  349. size = width * height;
  350. switch(pix_fmt) {
  351. case PIX_FMT_YUV420P:
  352. size = (size * 3) / 2;
  353. break;
  354. case PIX_FMT_YUV422P:
  355. size = (size * 2);
  356. break;
  357. case PIX_FMT_YUV444P:
  358. size = (size * 3);
  359. break;
  360. case PIX_FMT_RGB24:
  361. case PIX_FMT_BGR24:
  362. size = (size * 3);
  363. break;
  364. case PIX_FMT_YUV422:
  365. size = (size * 2);
  366. break;
  367. default:
  368. size = -1;
  369. break;
  370. }
  371. return size;
  372. }
  373. /* must be called before any other functions */
  374. void avcodec_init(void)
  375. {
  376. static int inited = 0;
  377. if (inited != 0)
  378. return;
  379. inited = 1;
  380. dsputil_init();
  381. }
  382. /* simple call to use all the codecs */
  383. void avcodec_register_all(void)
  384. {
  385. static int inited = 0;
  386. if (inited != 0)
  387. return;
  388. inited = 1;
  389. /* encoders */
  390. #ifdef CONFIG_ENCODERS
  391. register_avcodec(&ac3_encoder);
  392. register_avcodec(&mp2_encoder);
  393. #ifdef CONFIG_MP3LAME
  394. register_avcodec(&mp3lame_encoder);
  395. #endif
  396. register_avcodec(&mpeg1video_encoder);
  397. register_avcodec(&h263_encoder);
  398. register_avcodec(&h263p_encoder);
  399. register_avcodec(&rv10_encoder);
  400. register_avcodec(&mjpeg_encoder);
  401. register_avcodec(&mpeg4_encoder);
  402. register_avcodec(&msmpeg4v1_encoder);
  403. register_avcodec(&msmpeg4v2_encoder);
  404. register_avcodec(&msmpeg4v3_encoder);
  405. #endif /* CONFIG_ENCODERS */
  406. register_avcodec(&rawvideo_codec);
  407. /* decoders */
  408. #ifdef CONFIG_DECODERS
  409. register_avcodec(&h263_decoder);
  410. register_avcodec(&mpeg4_decoder);
  411. register_avcodec(&msmpeg4v1_decoder);
  412. register_avcodec(&msmpeg4v2_decoder);
  413. register_avcodec(&msmpeg4v3_decoder);
  414. register_avcodec(&wmv1_decoder);
  415. register_avcodec(&mpeg_decoder);
  416. register_avcodec(&h263i_decoder);
  417. register_avcodec(&rv10_decoder);
  418. register_avcodec(&mjpeg_decoder);
  419. register_avcodec(&mp2_decoder);
  420. register_avcodec(&mp3_decoder);
  421. #ifdef CONFIG_AC3
  422. register_avcodec(&ac3_decoder);
  423. #endif
  424. #endif /* CONFIG_DECODERS */
  425. /* pcm codecs */
  426. #define PCM_CODEC(id, name) \
  427. register_avcodec(& name ## _encoder); \
  428. register_avcodec(& name ## _decoder); \
  429. PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le);
  430. PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be);
  431. PCM_CODEC(CODEC_ID_PCM_U16LE, pcm_u16le);
  432. PCM_CODEC(CODEC_ID_PCM_U16BE, pcm_u16be);
  433. PCM_CODEC(CODEC_ID_PCM_S8, pcm_s8);
  434. PCM_CODEC(CODEC_ID_PCM_U8, pcm_u8);
  435. PCM_CODEC(CODEC_ID_PCM_ALAW, pcm_alaw);
  436. PCM_CODEC(CODEC_ID_PCM_MULAW, pcm_mulaw);
  437. #undef PCM_CODEC
  438. }
  439. static int encode_init(AVCodecContext *s)
  440. {
  441. return 0;
  442. }
  443. static int decode_frame(AVCodecContext *avctx,
  444. void *data, int *data_size,
  445. UINT8 *buf, int buf_size)
  446. {
  447. return -1;
  448. }
  449. static int encode_frame(AVCodecContext *avctx,
  450. unsigned char *frame, int buf_size, void *data)
  451. {
  452. return -1;
  453. }
  454. AVCodec rawvideo_codec = {
  455. "rawvideo",
  456. CODEC_TYPE_VIDEO,
  457. CODEC_ID_RAWVIDEO,
  458. 0,
  459. encode_init,
  460. encode_frame,
  461. NULL,
  462. decode_frame,
  463. };