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.

506 lines
13KB

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