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.

889 lines
22KB

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