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.

450 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. "yuv410p"
  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. char channels_str[100];
  189. int bitrate;
  190. if (encode)
  191. p = avcodec_find_encoder(enc->codec_id);
  192. else
  193. p = avcodec_find_decoder(enc->codec_id);
  194. if (p) {
  195. codec_name = p->name;
  196. } else if (enc->codec_name[0] != '\0') {
  197. codec_name = enc->codec_name;
  198. } else {
  199. /* output avi tags */
  200. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  201. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  202. enc->codec_tag & 0xff,
  203. (enc->codec_tag >> 8) & 0xff,
  204. (enc->codec_tag >> 16) & 0xff,
  205. (enc->codec_tag >> 24) & 0xff);
  206. } else {
  207. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  208. }
  209. codec_name = buf1;
  210. }
  211. switch(enc->codec_type) {
  212. case CODEC_TYPE_VIDEO:
  213. snprintf(buf, buf_size,
  214. "Video: %s%s",
  215. codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
  216. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  217. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  218. ", %s",
  219. pix_fmt_str[enc->pix_fmt]);
  220. }
  221. if (enc->width) {
  222. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  223. ", %dx%d, %0.2f fps",
  224. enc->width, enc->height,
  225. (float)enc->frame_rate / FRAME_RATE_BASE);
  226. }
  227. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  228. ", q=%d-%d", enc->qmin, enc->qmax);
  229. bitrate = enc->bit_rate;
  230. break;
  231. case CODEC_TYPE_AUDIO:
  232. snprintf(buf, buf_size,
  233. "Audio: %s",
  234. codec_name);
  235. switch (enc->channels) {
  236. case 1:
  237. strcpy(channels_str, "mono");
  238. break;
  239. case 2:
  240. strcpy(channels_str, "stereo");
  241. break;
  242. case 6:
  243. strcpy(channels_str, "5:1");
  244. break;
  245. default:
  246. sprintf(channels_str, "%d channels", enc->channels);
  247. break;
  248. }
  249. if (enc->sample_rate) {
  250. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  251. ", %d Hz, %s",
  252. enc->sample_rate,
  253. channels_str);
  254. }
  255. /* for PCM codecs, compute bitrate directly */
  256. switch(enc->codec_id) {
  257. case CODEC_ID_PCM_S16LE:
  258. case CODEC_ID_PCM_S16BE:
  259. case CODEC_ID_PCM_U16LE:
  260. case CODEC_ID_PCM_U16BE:
  261. bitrate = enc->sample_rate * enc->channels * 16;
  262. break;
  263. case CODEC_ID_PCM_S8:
  264. case CODEC_ID_PCM_U8:
  265. case CODEC_ID_PCM_ALAW:
  266. case CODEC_ID_PCM_MULAW:
  267. bitrate = enc->sample_rate * enc->channels * 8;
  268. break;
  269. default:
  270. bitrate = enc->bit_rate;
  271. break;
  272. }
  273. break;
  274. default:
  275. abort();
  276. }
  277. if (bitrate != 0) {
  278. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  279. ", %d kb/s", bitrate / 1000);
  280. }
  281. }
  282. /* Picture field are filled with 'ptr' addresses */
  283. void avpicture_fill(AVPicture *picture, UINT8 *ptr,
  284. int pix_fmt, int width, int height)
  285. {
  286. int size;
  287. size = width * height;
  288. switch(pix_fmt) {
  289. case PIX_FMT_YUV420P:
  290. picture->data[0] = ptr;
  291. picture->data[1] = picture->data[0] + size;
  292. picture->data[2] = picture->data[1] + size / 4;
  293. picture->linesize[0] = width;
  294. picture->linesize[1] = width / 2;
  295. picture->linesize[2] = width / 2;
  296. break;
  297. case PIX_FMT_YUV422P:
  298. picture->data[0] = ptr;
  299. picture->data[1] = picture->data[0] + size;
  300. picture->data[2] = picture->data[1] + size / 2;
  301. picture->linesize[0] = width;
  302. picture->linesize[1] = width / 2;
  303. picture->linesize[2] = width / 2;
  304. break;
  305. case PIX_FMT_YUV444P:
  306. picture->data[0] = ptr;
  307. picture->data[1] = picture->data[0] + size;
  308. picture->data[2] = picture->data[1] + size;
  309. picture->linesize[0] = width;
  310. picture->linesize[1] = width;
  311. picture->linesize[2] = width;
  312. break;
  313. case PIX_FMT_RGB24:
  314. case PIX_FMT_BGR24:
  315. picture->data[0] = ptr;
  316. picture->data[1] = NULL;
  317. picture->data[2] = NULL;
  318. picture->linesize[0] = width * 3;
  319. break;
  320. case PIX_FMT_YUV422:
  321. picture->data[0] = ptr;
  322. picture->data[1] = NULL;
  323. picture->data[2] = NULL;
  324. picture->linesize[0] = width * 2;
  325. break;
  326. default:
  327. picture->data[0] = NULL;
  328. picture->data[1] = NULL;
  329. picture->data[2] = NULL;
  330. break;
  331. }
  332. }
  333. int avpicture_get_size(int pix_fmt, int width, int height)
  334. {
  335. int size;
  336. size = width * height;
  337. switch(pix_fmt) {
  338. case PIX_FMT_YUV420P:
  339. size = (size * 3) / 2;
  340. break;
  341. case PIX_FMT_YUV422P:
  342. size = (size * 2);
  343. break;
  344. case PIX_FMT_YUV444P:
  345. size = (size * 3);
  346. break;
  347. case PIX_FMT_RGB24:
  348. case PIX_FMT_BGR24:
  349. size = (size * 3);
  350. break;
  351. case PIX_FMT_YUV422:
  352. size = (size * 2);
  353. break;
  354. default:
  355. size = -1;
  356. break;
  357. }
  358. return size;
  359. }
  360. unsigned avcodec_version( void )
  361. {
  362. return LIBAVCODEC_VERSION_INT;
  363. }
  364. unsigned avcodec_build( void )
  365. {
  366. return LIBAVCODEC_BUILD;
  367. }
  368. /* must be called before any other functions */
  369. void avcodec_init(void)
  370. {
  371. static int inited = 0;
  372. if (inited != 0)
  373. return;
  374. inited = 1;
  375. dsputil_init();
  376. }
  377. /* this should be called after seeking and before trying to decode the next frame */
  378. void avcodec_flush_buffers(AVCodecContext *avctx)
  379. {
  380. MpegEncContext *s = avctx->priv_data;
  381. s->num_available_buffers=0;
  382. }
  383. static int raw_encode_init(AVCodecContext *s)
  384. {
  385. return 0;
  386. }
  387. static int raw_decode_frame(AVCodecContext *avctx,
  388. void *data, int *data_size,
  389. UINT8 *buf, int buf_size)
  390. {
  391. return -1;
  392. }
  393. static int raw_encode_frame(AVCodecContext *avctx,
  394. unsigned char *frame, int buf_size, void *data)
  395. {
  396. return -1;
  397. }
  398. AVCodec rawvideo_codec = {
  399. "rawvideo",
  400. CODEC_TYPE_VIDEO,
  401. CODEC_ID_RAWVIDEO,
  402. 0,
  403. raw_encode_init,
  404. raw_encode_frame,
  405. NULL,
  406. raw_decode_frame,
  407. };