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.

463 lines
11KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library 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 GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. #include "dsputil.h"
  21. #include "mpegvideo.h"
  22. void *av_mallocz(int size)
  23. {
  24. void *ptr;
  25. ptr = av_malloc(size);
  26. if (!ptr)
  27. return NULL;
  28. memset(ptr, 0, size);
  29. return ptr;
  30. }
  31. /* cannot call it directly because of 'void **' casting is not automatic */
  32. void __av_freep(void **ptr)
  33. {
  34. av_free(*ptr);
  35. *ptr = NULL;
  36. }
  37. /* encoder management */
  38. AVCodec *first_avcodec;
  39. void register_avcodec(AVCodec *format)
  40. {
  41. AVCodec **p;
  42. p = &first_avcodec;
  43. while (*p != NULL) p = &(*p)->next;
  44. *p = format;
  45. format->next = NULL;
  46. }
  47. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  48. {
  49. int ret;
  50. avctx->codec = codec;
  51. avctx->frame_number = 0;
  52. if (codec->priv_data_size > 0) {
  53. avctx->priv_data = av_mallocz(codec->priv_data_size);
  54. if (!avctx->priv_data)
  55. return -ENOMEM;
  56. } else {
  57. avctx->priv_data = NULL;
  58. }
  59. ret = avctx->codec->init(avctx);
  60. if (ret < 0) {
  61. av_freep(&avctx->priv_data);
  62. return ret;
  63. }
  64. return 0;
  65. }
  66. int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  67. const short *samples)
  68. {
  69. int ret;
  70. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  71. avctx->frame_number++;
  72. return ret;
  73. }
  74. int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  75. const AVPicture *pict)
  76. {
  77. int ret;
  78. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  79. avctx->frame_number++;
  80. return ret;
  81. }
  82. /* decode a frame. return -1 if error, otherwise return the number of
  83. bytes used. If no frame could be decompressed, *got_picture_ptr is
  84. zero. Otherwise, it is non zero */
  85. int avcodec_decode_video(AVCodecContext *avctx, AVPicture *picture,
  86. int *got_picture_ptr,
  87. UINT8 *buf, int buf_size)
  88. {
  89. int ret;
  90. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  91. buf, buf_size);
  92. if (*got_picture_ptr)
  93. avctx->frame_number++;
  94. return ret;
  95. }
  96. /* decode an audio frame. return -1 if error, otherwise return the
  97. *number of bytes used. If no frame could be decompressed,
  98. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  99. *size in BYTES. */
  100. int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
  101. int *frame_size_ptr,
  102. UINT8 *buf, int buf_size)
  103. {
  104. int ret;
  105. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  106. buf, buf_size);
  107. avctx->frame_number++;
  108. return ret;
  109. }
  110. int avcodec_close(AVCodecContext *avctx)
  111. {
  112. if (avctx->codec->close)
  113. avctx->codec->close(avctx);
  114. av_freep(&avctx->priv_data);
  115. avctx->codec = NULL;
  116. return 0;
  117. }
  118. AVCodec *avcodec_find_encoder(enum CodecID id)
  119. {
  120. AVCodec *p;
  121. p = first_avcodec;
  122. while (p) {
  123. if (p->encode != NULL && p->id == id)
  124. return p;
  125. p = p->next;
  126. }
  127. return NULL;
  128. }
  129. AVCodec *avcodec_find_encoder_by_name(const char *name)
  130. {
  131. AVCodec *p;
  132. p = first_avcodec;
  133. while (p) {
  134. if (p->encode != NULL && strcmp(name,p->name) == 0)
  135. return p;
  136. p = p->next;
  137. }
  138. return NULL;
  139. }
  140. AVCodec *avcodec_find_decoder(enum CodecID id)
  141. {
  142. AVCodec *p;
  143. p = first_avcodec;
  144. while (p) {
  145. if (p->decode != NULL && p->id == id)
  146. return p;
  147. p = p->next;
  148. }
  149. return NULL;
  150. }
  151. AVCodec *avcodec_find_decoder_by_name(const char *name)
  152. {
  153. AVCodec *p;
  154. p = first_avcodec;
  155. while (p) {
  156. if (p->decode != NULL && strcmp(name,p->name) == 0)
  157. return p;
  158. p = p->next;
  159. }
  160. return NULL;
  161. }
  162. AVCodec *avcodec_find(enum CodecID id)
  163. {
  164. AVCodec *p;
  165. p = first_avcodec;
  166. while (p) {
  167. if (p->id == id)
  168. return p;
  169. p = p->next;
  170. }
  171. return NULL;
  172. }
  173. const char *pix_fmt_str[] = {
  174. "??",
  175. "yuv420p",
  176. "yuv422",
  177. "rgb24",
  178. "bgr24",
  179. "yuv422p",
  180. "yuv444p",
  181. "rgba32",
  182. "bgra32",
  183. "yuv410p"
  184. };
  185. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  186. {
  187. const char *codec_name;
  188. AVCodec *p;
  189. char buf1[32];
  190. char channels_str[100];
  191. int bitrate;
  192. if (encode)
  193. p = avcodec_find_encoder(enc->codec_id);
  194. else
  195. p = avcodec_find_decoder(enc->codec_id);
  196. if (p) {
  197. codec_name = p->name;
  198. } else if (enc->codec_name[0] != '\0') {
  199. codec_name = enc->codec_name;
  200. } else {
  201. /* output avi tags */
  202. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  203. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  204. enc->codec_tag & 0xff,
  205. (enc->codec_tag >> 8) & 0xff,
  206. (enc->codec_tag >> 16) & 0xff,
  207. (enc->codec_tag >> 24) & 0xff);
  208. } else {
  209. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  210. }
  211. codec_name = buf1;
  212. }
  213. switch(enc->codec_type) {
  214. case CODEC_TYPE_VIDEO:
  215. snprintf(buf, buf_size,
  216. "Video: %s%s",
  217. codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
  218. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  219. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  220. ", %s",
  221. pix_fmt_str[enc->pix_fmt]);
  222. }
  223. if (enc->width) {
  224. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  225. ", %dx%d, %0.2f fps",
  226. enc->width, enc->height,
  227. (float)enc->frame_rate / FRAME_RATE_BASE);
  228. }
  229. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  230. ", q=%d-%d", enc->qmin, enc->qmax);
  231. bitrate = enc->bit_rate;
  232. break;
  233. case CODEC_TYPE_AUDIO:
  234. snprintf(buf, buf_size,
  235. "Audio: %s",
  236. codec_name);
  237. switch (enc->channels) {
  238. case 1:
  239. strcpy(channels_str, "mono");
  240. break;
  241. case 2:
  242. strcpy(channels_str, "stereo");
  243. break;
  244. case 6:
  245. strcpy(channels_str, "5:1");
  246. break;
  247. default:
  248. sprintf(channels_str, "%d channels", enc->channels);
  249. break;
  250. }
  251. if (enc->sample_rate) {
  252. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  253. ", %d Hz, %s",
  254. enc->sample_rate,
  255. channels_str);
  256. }
  257. /* for PCM codecs, compute bitrate directly */
  258. switch(enc->codec_id) {
  259. case CODEC_ID_PCM_S16LE:
  260. case CODEC_ID_PCM_S16BE:
  261. case CODEC_ID_PCM_U16LE:
  262. case CODEC_ID_PCM_U16BE:
  263. bitrate = enc->sample_rate * enc->channels * 16;
  264. break;
  265. case CODEC_ID_PCM_S8:
  266. case CODEC_ID_PCM_U8:
  267. case CODEC_ID_PCM_ALAW:
  268. case CODEC_ID_PCM_MULAW:
  269. bitrate = enc->sample_rate * enc->channels * 8;
  270. break;
  271. default:
  272. bitrate = enc->bit_rate;
  273. break;
  274. }
  275. break;
  276. default:
  277. av_abort();
  278. }
  279. if (bitrate != 0) {
  280. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  281. ", %d kb/s", bitrate / 1000);
  282. }
  283. }
  284. /* Picture field are filled with 'ptr' addresses */
  285. void avpicture_fill(AVPicture *picture, UINT8 *ptr,
  286. int pix_fmt, int width, int height)
  287. {
  288. int size;
  289. size = width * height;
  290. switch(pix_fmt) {
  291. case PIX_FMT_YUV420P:
  292. picture->data[0] = ptr;
  293. picture->data[1] = picture->data[0] + size;
  294. picture->data[2] = picture->data[1] + size / 4;
  295. picture->linesize[0] = width;
  296. picture->linesize[1] = width / 2;
  297. picture->linesize[2] = width / 2;
  298. break;
  299. case PIX_FMT_YUV422P:
  300. picture->data[0] = ptr;
  301. picture->data[1] = picture->data[0] + size;
  302. picture->data[2] = picture->data[1] + size / 2;
  303. picture->linesize[0] = width;
  304. picture->linesize[1] = width / 2;
  305. picture->linesize[2] = width / 2;
  306. break;
  307. case PIX_FMT_YUV444P:
  308. picture->data[0] = ptr;
  309. picture->data[1] = picture->data[0] + size;
  310. picture->data[2] = picture->data[1] + size;
  311. picture->linesize[0] = width;
  312. picture->linesize[1] = width;
  313. picture->linesize[2] = width;
  314. break;
  315. case PIX_FMT_RGB24:
  316. case PIX_FMT_BGR24:
  317. picture->data[0] = ptr;
  318. picture->data[1] = NULL;
  319. picture->data[2] = NULL;
  320. picture->linesize[0] = width * 3;
  321. break;
  322. case PIX_FMT_RGBA32:
  323. case PIX_FMT_BGRA32:
  324. picture->data[0] = ptr;
  325. picture->data[1] = NULL;
  326. picture->data[2] = NULL;
  327. picture->linesize[0] = width * 4;
  328. break;
  329. case PIX_FMT_YUV422:
  330. picture->data[0] = ptr;
  331. picture->data[1] = NULL;
  332. picture->data[2] = NULL;
  333. picture->linesize[0] = width * 2;
  334. break;
  335. default:
  336. picture->data[0] = NULL;
  337. picture->data[1] = NULL;
  338. picture->data[2] = NULL;
  339. break;
  340. }
  341. }
  342. int avpicture_get_size(int pix_fmt, int width, int height)
  343. {
  344. int size;
  345. size = width * height;
  346. switch(pix_fmt) {
  347. case PIX_FMT_YUV420P:
  348. size = (size * 3) / 2;
  349. break;
  350. case PIX_FMT_YUV422P:
  351. size = (size * 2);
  352. break;
  353. case PIX_FMT_YUV444P:
  354. size = (size * 3);
  355. break;
  356. case PIX_FMT_RGB24:
  357. case PIX_FMT_BGR24:
  358. size = (size * 3);
  359. break;
  360. case PIX_FMT_RGBA32:
  361. case PIX_FMT_BGRA32:
  362. size = (size * 4);
  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. unsigned avcodec_version( void )
  374. {
  375. return LIBAVCODEC_VERSION_INT;
  376. }
  377. unsigned avcodec_build( void )
  378. {
  379. return LIBAVCODEC_BUILD;
  380. }
  381. /* must be called before any other functions */
  382. void avcodec_init(void)
  383. {
  384. static int inited = 0;
  385. if (inited != 0)
  386. return;
  387. inited = 1;
  388. dsputil_init();
  389. }
  390. /* this should be called after seeking and before trying to decode the next frame */
  391. void avcodec_flush_buffers(AVCodecContext *avctx)
  392. {
  393. MpegEncContext *s = avctx->priv_data;
  394. s->num_available_buffers=0;
  395. }
  396. static int raw_encode_init(AVCodecContext *s)
  397. {
  398. return 0;
  399. }
  400. static int raw_decode_frame(AVCodecContext *avctx,
  401. void *data, int *data_size,
  402. UINT8 *buf, int buf_size)
  403. {
  404. return -1;
  405. }
  406. static int raw_encode_frame(AVCodecContext *avctx,
  407. unsigned char *frame, int buf_size, void *data)
  408. {
  409. return -1;
  410. }
  411. AVCodec rawvideo_codec = {
  412. "rawvideo",
  413. CODEC_TYPE_VIDEO,
  414. CODEC_ID_RAWVIDEO,
  415. 0,
  416. raw_encode_init,
  417. raw_encode_frame,
  418. NULL,
  419. raw_decode_frame,
  420. };