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.

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