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.

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