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.

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