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.

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