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.

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