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.

493 lines
12KB

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