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.

484 lines
12KB

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