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.

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