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.

900 lines
23KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. * Copyright (c) 2003 Michel Bardiaux for the av_log API
  5. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /**
  22. * @file utils.c
  23. * utils.
  24. */
  25. #include "avcodec.h"
  26. #include "dsputil.h"
  27. #include "mpegvideo.h"
  28. #include "integer.h"
  29. #include <stdarg.h>
  30. #include <limits.h>
  31. static void avcodec_default_free_buffers(AVCodecContext *s);
  32. void *av_mallocz(unsigned int size)
  33. {
  34. void *ptr;
  35. ptr = av_malloc(size);
  36. if (!ptr)
  37. return NULL;
  38. memset(ptr, 0, size);
  39. return ptr;
  40. }
  41. char *av_strdup(const char *s)
  42. {
  43. char *ptr;
  44. int len;
  45. len = strlen(s) + 1;
  46. ptr = av_malloc(len);
  47. if (!ptr)
  48. return NULL;
  49. memcpy(ptr, s, len);
  50. return ptr;
  51. }
  52. /**
  53. * realloc which does nothing if the block is large enough
  54. */
  55. void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
  56. {
  57. if(min_size < *size)
  58. return ptr;
  59. *size= 17*min_size/16 + 32;
  60. return av_realloc(ptr, *size);
  61. }
  62. static unsigned int last_static = 0;
  63. static unsigned int allocated_static = 0;
  64. static void** array_static = NULL;
  65. /**
  66. * allocation of static arrays - do not use for normal allocation.
  67. */
  68. void *av_mallocz_static(unsigned int size)
  69. {
  70. void *ptr = av_mallocz(size);
  71. if(ptr){
  72. array_static =av_fast_realloc(array_static, &allocated_static, sizeof(void*)*(last_static+1));
  73. array_static[last_static++] = ptr;
  74. }
  75. return ptr;
  76. }
  77. /**
  78. * free all static arrays and reset pointers to 0.
  79. */
  80. void av_free_static(void)
  81. {
  82. while(last_static){
  83. av_freep(&array_static[--last_static]);
  84. }
  85. av_freep(&array_static);
  86. }
  87. /**
  88. * Frees memory and sets the pointer to NULL.
  89. * @param arg pointer to the pointer which should be freed
  90. */
  91. void av_freep(void *arg)
  92. {
  93. void **ptr= (void**)arg;
  94. av_free(*ptr);
  95. *ptr = NULL;
  96. }
  97. /* encoder management */
  98. AVCodec *first_avcodec;
  99. void register_avcodec(AVCodec *format)
  100. {
  101. AVCodec **p;
  102. p = &first_avcodec;
  103. while (*p != NULL) p = &(*p)->next;
  104. *p = format;
  105. format->next = NULL;
  106. }
  107. typedef struct InternalBuffer{
  108. int last_pic_num;
  109. uint8_t *base[4];
  110. uint8_t *data[4];
  111. int linesize[4];
  112. }InternalBuffer;
  113. #define INTERNAL_BUFFER_SIZE 32
  114. #define ALIGN(x, a) (((x)+(a)-1)&~((a)-1))
  115. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  116. int w_align= 1;
  117. int h_align= 1;
  118. switch(s->pix_fmt){
  119. case PIX_FMT_YUV420P:
  120. case PIX_FMT_YUV422:
  121. case PIX_FMT_YUV422P:
  122. case PIX_FMT_YUV444P:
  123. case PIX_FMT_GRAY8:
  124. case PIX_FMT_YUVJ420P:
  125. case PIX_FMT_YUVJ422P:
  126. case PIX_FMT_YUVJ444P:
  127. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  128. h_align= 16;
  129. break;
  130. case PIX_FMT_YUV411P:
  131. w_align=32;
  132. h_align=8;
  133. break;
  134. case PIX_FMT_YUV410P:
  135. if(s->codec_id == CODEC_ID_SVQ1){
  136. w_align=64;
  137. h_align=64;
  138. }
  139. break;
  140. default:
  141. w_align= 1;
  142. h_align= 1;
  143. break;
  144. }
  145. *width = ALIGN(*width , w_align);
  146. *height= ALIGN(*height, h_align);
  147. }
  148. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  149. int i;
  150. int w= s->width;
  151. int h= s->height;
  152. InternalBuffer *buf;
  153. int *picture_number;
  154. assert(pic->data[0]==NULL);
  155. assert(INTERNAL_BUFFER_SIZE > s->internal_buffer_count);
  156. if(s->internal_buffer==NULL){
  157. s->internal_buffer= av_mallocz(INTERNAL_BUFFER_SIZE*sizeof(InternalBuffer));
  158. }
  159. #if 0
  160. s->internal_buffer= av_fast_realloc(
  161. s->internal_buffer,
  162. &s->internal_buffer_size,
  163. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  164. );
  165. #endif
  166. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  167. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE-1]).last_pic_num; //FIXME ugly hack
  168. (*picture_number)++;
  169. if(buf->base[0]){
  170. pic->age= *picture_number - buf->last_pic_num;
  171. buf->last_pic_num= *picture_number;
  172. }else{
  173. int h_chroma_shift, v_chroma_shift;
  174. int s_align, pixel_size;
  175. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  176. switch(s->pix_fmt){
  177. case PIX_FMT_RGB555:
  178. case PIX_FMT_RGB565:
  179. case PIX_FMT_YUV422:
  180. pixel_size=2;
  181. break;
  182. case PIX_FMT_RGB24:
  183. case PIX_FMT_BGR24:
  184. pixel_size=3;
  185. break;
  186. case PIX_FMT_RGBA32:
  187. pixel_size=4;
  188. break;
  189. default:
  190. pixel_size=1;
  191. }
  192. avcodec_align_dimensions(s, &w, &h);
  193. #if defined(ARCH_POWERPC) || defined(HAVE_MMI) //FIXME some cleaner check
  194. s_align= 16;
  195. #else
  196. s_align= 8;
  197. #endif
  198. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  199. w+= EDGE_WIDTH*2;
  200. h+= EDGE_WIDTH*2;
  201. }
  202. buf->last_pic_num= -256*256*256*64;
  203. for(i=0; i<3; i++){
  204. const int h_shift= i==0 ? 0 : h_chroma_shift;
  205. const int v_shift= i==0 ? 0 : v_chroma_shift;
  206. //FIXME next ensures that linesize= 2^x uvlinesize, thats needed because some MC code assumes it
  207. buf->linesize[i]= ALIGN(pixel_size*w>>h_shift, s_align<<(h_chroma_shift-h_shift));
  208. buf->base[i]= av_mallocz((buf->linesize[i]*h>>v_shift)+16); //FIXME 16
  209. if(buf->base[i]==NULL) return -1;
  210. memset(buf->base[i], 128, buf->linesize[i]*h>>v_shift);
  211. if(s->flags&CODEC_FLAG_EMU_EDGE)
  212. buf->data[i] = buf->base[i];
  213. else
  214. buf->data[i] = buf->base[i] + ALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), s_align);
  215. }
  216. pic->age= 256*256*256*64;
  217. }
  218. pic->type= FF_BUFFER_TYPE_INTERNAL;
  219. for(i=0; i<4; i++){
  220. pic->base[i]= buf->base[i];
  221. pic->data[i]= buf->data[i];
  222. pic->linesize[i]= buf->linesize[i];
  223. }
  224. s->internal_buffer_count++;
  225. return 0;
  226. }
  227. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  228. int i;
  229. InternalBuffer *buf, *last, temp;
  230. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  231. assert(s->internal_buffer_count);
  232. buf = NULL; /* avoids warning */
  233. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  234. buf= &((InternalBuffer*)s->internal_buffer)[i];
  235. if(buf->data[0] == pic->data[0])
  236. break;
  237. }
  238. assert(i < s->internal_buffer_count);
  239. s->internal_buffer_count--;
  240. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  241. temp= *buf;
  242. *buf= *last;
  243. *last= temp;
  244. for(i=0; i<3; i++){
  245. pic->data[i]=NULL;
  246. // pic->base[i]=NULL;
  247. }
  248. //printf("R%X\n", pic->opaque);
  249. }
  250. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  251. AVFrame temp_pic;
  252. int i;
  253. /* If no picture return a new buffer */
  254. if(pic->data[0] == NULL) {
  255. /* We will copy from buffer, so must be readable */
  256. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  257. return s->get_buffer(s, pic);
  258. }
  259. /* If internal buffer type return the same buffer */
  260. if(pic->type == FF_BUFFER_TYPE_INTERNAL)
  261. return 0;
  262. /*
  263. * Not internal type and reget_buffer not overridden, emulate cr buffer
  264. */
  265. temp_pic = *pic;
  266. for(i = 0; i < 4; i++)
  267. pic->data[i] = pic->base[i] = NULL;
  268. pic->opaque = NULL;
  269. /* Allocate new frame */
  270. if (s->get_buffer(s, pic))
  271. return -1;
  272. /* Copy image data from old buffer to new buffer */
  273. img_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  274. s->height);
  275. s->release_buffer(s, &temp_pic); // Release old frame
  276. return 0;
  277. }
  278. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){
  279. int i;
  280. for(i=0; i<count; i++){
  281. int r= func(c, arg[i]);
  282. if(ret) ret[i]= r;
  283. }
  284. return 0;
  285. }
  286. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat * fmt){
  287. return fmt[0];
  288. }
  289. static const char* context_to_name(void* ptr) {
  290. AVCodecContext *avc= ptr;
  291. if(avc && avc->codec && avc->codec->name)
  292. return avc->codec->name;
  293. else
  294. return "NULL";
  295. }
  296. static AVClass av_codec_context_class = { "AVCodecContext", context_to_name };
  297. void avcodec_get_context_defaults(AVCodecContext *s){
  298. memset(s, 0, sizeof(AVCodecContext));
  299. s->av_class= &av_codec_context_class;
  300. s->bit_rate= 800*1000;
  301. s->bit_rate_tolerance= s->bit_rate*10;
  302. s->qmin= 2;
  303. s->qmax= 31;
  304. s->mb_qmin= 2;
  305. s->mb_qmax= 31;
  306. s->rc_eq= "tex^qComp";
  307. s->qcompress= 0.5;
  308. s->max_qdiff= 3;
  309. s->b_quant_factor=1.25;
  310. s->b_quant_offset=1.25;
  311. s->i_quant_factor=-0.8;
  312. s->i_quant_offset=0.0;
  313. s->error_concealment= 3;
  314. s->error_resilience= 1;
  315. s->workaround_bugs= FF_BUG_AUTODETECT;
  316. s->frame_rate_base= 1;
  317. s->frame_rate = 25;
  318. s->gop_size= 50;
  319. s->me_method= ME_EPZS;
  320. s->get_buffer= avcodec_default_get_buffer;
  321. s->release_buffer= avcodec_default_release_buffer;
  322. s->get_format= avcodec_default_get_format;
  323. s->execute= avcodec_default_execute;
  324. s->thread_count=1;
  325. s->me_subpel_quality=8;
  326. s->lmin= FF_QP2LAMBDA * s->qmin;
  327. s->lmax= FF_QP2LAMBDA * s->qmax;
  328. s->sample_aspect_ratio= (AVRational){0,1};
  329. s->ildct_cmp= FF_CMP_VSAD;
  330. s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS;
  331. s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS;
  332. s->palctrl = NULL;
  333. s->reget_buffer= avcodec_default_reget_buffer;
  334. }
  335. /**
  336. * allocates a AVCodecContext and set it to defaults.
  337. * this can be deallocated by simply calling free()
  338. */
  339. AVCodecContext *avcodec_alloc_context(void){
  340. AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
  341. if(avctx==NULL) return NULL;
  342. avcodec_get_context_defaults(avctx);
  343. return avctx;
  344. }
  345. void avcodec_get_frame_defaults(AVFrame *pic){
  346. memset(pic, 0, sizeof(AVFrame));
  347. pic->pts= AV_NOPTS_VALUE;
  348. }
  349. /**
  350. * allocates a AVPFrame and set it to defaults.
  351. * this can be deallocated by simply calling free()
  352. */
  353. AVFrame *avcodec_alloc_frame(void){
  354. AVFrame *pic= av_malloc(sizeof(AVFrame));
  355. if(pic==NULL) return NULL;
  356. avcodec_get_frame_defaults(pic);
  357. return pic;
  358. }
  359. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  360. {
  361. int ret;
  362. if(avctx->codec)
  363. return -1;
  364. avctx->codec = codec;
  365. avctx->codec_id = codec->id;
  366. avctx->frame_number = 0;
  367. if (codec->priv_data_size > 0) {
  368. avctx->priv_data = av_mallocz(codec->priv_data_size);
  369. if (!avctx->priv_data)
  370. return -ENOMEM;
  371. } else {
  372. avctx->priv_data = NULL;
  373. }
  374. ret = avctx->codec->init(avctx);
  375. if (ret < 0) {
  376. av_freep(&avctx->priv_data);
  377. return ret;
  378. }
  379. return 0;
  380. }
  381. int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  382. const short *samples)
  383. {
  384. int ret;
  385. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  386. avctx->frame_number++;
  387. return ret;
  388. }
  389. int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  390. const AVFrame *pict)
  391. {
  392. int ret;
  393. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  394. emms_c(); //needed to avoid a emms_c() call before every return;
  395. avctx->frame_number++;
  396. return ret;
  397. }
  398. /**
  399. * decode a frame.
  400. * @param buf bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE larger then the actual read bytes
  401. * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  402. * @param buf_size the size of the buffer in bytes
  403. * @param got_picture_ptr zero if no frame could be decompressed, Otherwise, it is non zero
  404. * @return -1 if error, otherwise return the number of
  405. * bytes used.
  406. */
  407. int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  408. int *got_picture_ptr,
  409. uint8_t *buf, int buf_size)
  410. {
  411. int ret;
  412. *got_picture_ptr= 0;
  413. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  414. buf, buf_size);
  415. emms_c(); //needed to avoid a emms_c() call before every return;
  416. if (*got_picture_ptr)
  417. avctx->frame_number++;
  418. return ret;
  419. }
  420. /* decode an audio frame. return -1 if error, otherwise return the
  421. *number of bytes used. If no frame could be decompressed,
  422. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  423. *size in BYTES. */
  424. int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,
  425. int *frame_size_ptr,
  426. uint8_t *buf, int buf_size)
  427. {
  428. int ret;
  429. *frame_size_ptr= 0;
  430. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  431. buf, buf_size);
  432. avctx->frame_number++;
  433. return ret;
  434. }
  435. int avcodec_close(AVCodecContext *avctx)
  436. {
  437. if (avctx->codec->close)
  438. avctx->codec->close(avctx);
  439. avcodec_default_free_buffers(avctx);
  440. av_freep(&avctx->priv_data);
  441. avctx->codec = NULL;
  442. return 0;
  443. }
  444. AVCodec *avcodec_find_encoder(enum CodecID id)
  445. {
  446. AVCodec *p;
  447. p = first_avcodec;
  448. while (p) {
  449. if (p->encode != NULL && p->id == id)
  450. return p;
  451. p = p->next;
  452. }
  453. return NULL;
  454. }
  455. AVCodec *avcodec_find_encoder_by_name(const char *name)
  456. {
  457. AVCodec *p;
  458. p = first_avcodec;
  459. while (p) {
  460. if (p->encode != NULL && strcmp(name,p->name) == 0)
  461. return p;
  462. p = p->next;
  463. }
  464. return NULL;
  465. }
  466. AVCodec *avcodec_find_decoder(enum CodecID id)
  467. {
  468. AVCodec *p;
  469. p = first_avcodec;
  470. while (p) {
  471. if (p->decode != NULL && p->id == id)
  472. return p;
  473. p = p->next;
  474. }
  475. return NULL;
  476. }
  477. AVCodec *avcodec_find_decoder_by_name(const char *name)
  478. {
  479. AVCodec *p;
  480. p = first_avcodec;
  481. while (p) {
  482. if (p->decode != NULL && strcmp(name,p->name) == 0)
  483. return p;
  484. p = p->next;
  485. }
  486. return NULL;
  487. }
  488. static AVCodec *avcodec_find(enum CodecID id)
  489. {
  490. AVCodec *p;
  491. p = first_avcodec;
  492. while (p) {
  493. if (p->id == id)
  494. return p;
  495. p = p->next;
  496. }
  497. return NULL;
  498. }
  499. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  500. {
  501. const char *codec_name;
  502. AVCodec *p;
  503. char buf1[32];
  504. char channels_str[100];
  505. int bitrate;
  506. if (encode)
  507. p = avcodec_find_encoder(enc->codec_id);
  508. else
  509. p = avcodec_find_decoder(enc->codec_id);
  510. if (p) {
  511. codec_name = p->name;
  512. if (!encode && enc->codec_id == CODEC_ID_MP3) {
  513. if (enc->sub_id == 2)
  514. codec_name = "mp2";
  515. else if (enc->sub_id == 1)
  516. codec_name = "mp1";
  517. }
  518. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  519. /* fake mpeg2 transport stream codec (currently not
  520. registered) */
  521. codec_name = "mpeg2ts";
  522. } else if (enc->codec_name[0] != '\0') {
  523. codec_name = enc->codec_name;
  524. } else {
  525. /* output avi tags */
  526. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  527. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  528. enc->codec_tag & 0xff,
  529. (enc->codec_tag >> 8) & 0xff,
  530. (enc->codec_tag >> 16) & 0xff,
  531. (enc->codec_tag >> 24) & 0xff);
  532. } else {
  533. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  534. }
  535. codec_name = buf1;
  536. }
  537. switch(enc->codec_type) {
  538. case CODEC_TYPE_VIDEO:
  539. snprintf(buf, buf_size,
  540. "Video: %s%s",
  541. codec_name, enc->mb_decision ? " (hq)" : "");
  542. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  543. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  544. ", %s",
  545. avcodec_get_pix_fmt_name(enc->pix_fmt));
  546. }
  547. if (enc->width) {
  548. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  549. ", %dx%d, %0.2f fps",
  550. enc->width, enc->height,
  551. (float)enc->frame_rate / enc->frame_rate_base);
  552. }
  553. if (encode) {
  554. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  555. ", q=%d-%d", enc->qmin, enc->qmax);
  556. }
  557. bitrate = enc->bit_rate;
  558. break;
  559. case CODEC_TYPE_AUDIO:
  560. snprintf(buf, buf_size,
  561. "Audio: %s",
  562. codec_name);
  563. switch (enc->channels) {
  564. case 1:
  565. strcpy(channels_str, "mono");
  566. break;
  567. case 2:
  568. strcpy(channels_str, "stereo");
  569. break;
  570. case 6:
  571. strcpy(channels_str, "5:1");
  572. break;
  573. default:
  574. sprintf(channels_str, "%d channels", enc->channels);
  575. break;
  576. }
  577. if (enc->sample_rate) {
  578. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  579. ", %d Hz, %s",
  580. enc->sample_rate,
  581. channels_str);
  582. }
  583. /* for PCM codecs, compute bitrate directly */
  584. switch(enc->codec_id) {
  585. case CODEC_ID_PCM_S16LE:
  586. case CODEC_ID_PCM_S16BE:
  587. case CODEC_ID_PCM_U16LE:
  588. case CODEC_ID_PCM_U16BE:
  589. bitrate = enc->sample_rate * enc->channels * 16;
  590. break;
  591. case CODEC_ID_PCM_S8:
  592. case CODEC_ID_PCM_U8:
  593. case CODEC_ID_PCM_ALAW:
  594. case CODEC_ID_PCM_MULAW:
  595. bitrate = enc->sample_rate * enc->channels * 8;
  596. break;
  597. default:
  598. bitrate = enc->bit_rate;
  599. break;
  600. }
  601. break;
  602. case CODEC_TYPE_DATA:
  603. snprintf(buf, buf_size, "Data: %s", codec_name);
  604. bitrate = enc->bit_rate;
  605. break;
  606. default:
  607. av_abort();
  608. }
  609. if (encode) {
  610. if (enc->flags & CODEC_FLAG_PASS1)
  611. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  612. ", pass 1");
  613. if (enc->flags & CODEC_FLAG_PASS2)
  614. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  615. ", pass 2");
  616. }
  617. if (bitrate != 0) {
  618. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  619. ", %d kb/s", bitrate / 1000);
  620. }
  621. }
  622. unsigned avcodec_version( void )
  623. {
  624. return LIBAVCODEC_VERSION_INT;
  625. }
  626. unsigned avcodec_build( void )
  627. {
  628. return LIBAVCODEC_BUILD;
  629. }
  630. /* must be called before any other functions */
  631. void avcodec_init(void)
  632. {
  633. static int inited = 0;
  634. if (inited != 0)
  635. return;
  636. inited = 1;
  637. dsputil_static_init();
  638. }
  639. /**
  640. * Flush buffers, should be called when seeking or when swicthing to a different stream.
  641. */
  642. void avcodec_flush_buffers(AVCodecContext *avctx)
  643. {
  644. if(avctx->codec->flush)
  645. avctx->codec->flush(avctx);
  646. }
  647. static void avcodec_default_free_buffers(AVCodecContext *s){
  648. int i, j;
  649. if(s->internal_buffer==NULL) return;
  650. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  651. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  652. for(j=0; j<4; j++){
  653. av_freep(&buf->base[j]);
  654. buf->data[j]= NULL;
  655. }
  656. }
  657. av_freep(&s->internal_buffer);
  658. s->internal_buffer_count=0;
  659. }
  660. char av_get_pict_type_char(int pict_type){
  661. switch(pict_type){
  662. case I_TYPE: return 'I';
  663. case P_TYPE: return 'P';
  664. case B_TYPE: return 'B';
  665. case S_TYPE: return 'S';
  666. case SI_TYPE:return 'i';
  667. case SP_TYPE:return 'p';
  668. default: return '?';
  669. }
  670. }
  671. int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
  672. int exact=1, sign=0;
  673. int64_t gcd;
  674. assert(den != 0);
  675. if(den < 0)
  676. return av_reduce(dst_nom, dst_den, -nom, -den, max);
  677. sign= nom < 0;
  678. nom= ABS(nom);
  679. gcd = ff_gcd(nom, den);
  680. nom /= gcd;
  681. den /= gcd;
  682. if(nom > max || den > max){
  683. AVRational a0={0,1}, a1={1,0};
  684. exact=0;
  685. for(;;){
  686. int64_t x= nom / den;
  687. int64_t a2n= x*a1.num + a0.num;
  688. int64_t a2d= x*a1.den + a0.den;
  689. if(a2n > max || a2d > max) break;
  690. nom %= den;
  691. a0= a1;
  692. a1= (AVRational){a2n, a2d};
  693. if(nom==0) break;
  694. x= nom; nom=den; den=x;
  695. }
  696. nom= a1.num;
  697. den= a1.den;
  698. }
  699. assert(ff_gcd(nom, den) == 1);
  700. *dst_nom = sign ? -nom : nom;
  701. *dst_den = den;
  702. return exact;
  703. }
  704. int64_t av_rescale(int64_t a, int64_t b, int64_t c){
  705. AVInteger ai, ci;
  706. assert(c > 0);
  707. assert(b >=0);
  708. if(a<0) return -av_rescale(-a, b, c);
  709. if(b<=INT_MAX && c<=INT_MAX){
  710. if(a<=INT_MAX)
  711. return (a * b + c/2)/c;
  712. else
  713. return a/c*b + (a%c*b + c/2)/c;
  714. }
  715. ai= av_mul_i(av_int2i(a), av_int2i(b));
  716. ci= av_int2i(c);
  717. ai= av_add_i(ai, av_shr_i(ci,1));
  718. return av_i2int(av_div_i(ai, ci));
  719. }
  720. /* av_log API */
  721. static int av_log_level = AV_LOG_DEBUG;
  722. static void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
  723. {
  724. static int print_prefix=1;
  725. AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
  726. if(level>av_log_level)
  727. return;
  728. #undef fprintf
  729. if(print_prefix && avc) {
  730. fprintf(stderr, "[%s @ %p]", avc->item_name(ptr), avc);
  731. }
  732. #define fprintf please_use_av_log
  733. print_prefix= strstr(fmt, "\n") != NULL;
  734. vfprintf(stderr, fmt, vl);
  735. }
  736. static void (*av_log_callback)(void*, int, const char*, va_list) = av_log_default_callback;
  737. void av_log(void* avcl, int level, const char *fmt, ...)
  738. {
  739. va_list vl;
  740. va_start(vl, fmt);
  741. av_vlog(avcl, level, fmt, vl);
  742. va_end(vl);
  743. }
  744. void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
  745. {
  746. av_log_callback(avcl, level, fmt, vl);
  747. }
  748. int av_log_get_level(void)
  749. {
  750. return av_log_level;
  751. }
  752. void av_log_set_level(int level)
  753. {
  754. av_log_level = level;
  755. }
  756. void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
  757. {
  758. av_log_callback = callback;
  759. }
  760. #if !defined(HAVE_PTHREADS) && !defined(HAVE_W32THREADS)
  761. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  762. return -1;
  763. }
  764. #endif