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.

860 lines
21KB

  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. buf->linesize[i]= ALIGN(pixel_size*w>>h_shift, s_align);
  210. buf->base[i]= av_mallocz((buf->linesize[i]*h>>v_shift)+16); //FIXME 16
  211. if(buf->base[i]==NULL) return -1;
  212. memset(buf->base[i], 128, buf->linesize[i]*h>>v_shift);
  213. if(s->flags&CODEC_FLAG_EMU_EDGE)
  214. buf->data[i] = buf->base[i];
  215. else
  216. buf->data[i] = buf->base[i] + ALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), s_align);
  217. }
  218. pic->age= 256*256*256*64;
  219. }
  220. pic->type= FF_BUFFER_TYPE_INTERNAL;
  221. for(i=0; i<4; i++){
  222. pic->base[i]= buf->base[i];
  223. pic->data[i]= buf->data[i];
  224. pic->linesize[i]= buf->linesize[i];
  225. }
  226. s->internal_buffer_count++;
  227. return 0;
  228. }
  229. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  230. int i;
  231. InternalBuffer *buf, *last, temp;
  232. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  233. assert(s->internal_buffer_count);
  234. buf = NULL; /* avoids warning */
  235. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  236. buf= &((InternalBuffer*)s->internal_buffer)[i];
  237. if(buf->data[0] == pic->data[0])
  238. break;
  239. }
  240. assert(i < s->internal_buffer_count);
  241. s->internal_buffer_count--;
  242. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  243. temp= *buf;
  244. *buf= *last;
  245. *last= temp;
  246. for(i=0; i<3; i++){
  247. pic->data[i]=NULL;
  248. // pic->base[i]=NULL;
  249. }
  250. //printf("R%X\n", pic->opaque);
  251. }
  252. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  253. AVFrame temp_pic;
  254. int i;
  255. /* If no picture return a new buffer */
  256. if(pic->data[0] == NULL) {
  257. /* We will copy from buffer, so must be readable */
  258. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  259. return s->get_buffer(s, pic);
  260. }
  261. /* If internal buffer type return the same buffer */
  262. if(pic->type == FF_BUFFER_TYPE_INTERNAL)
  263. return 0;
  264. /*
  265. * Not internal type and reget_buffer not overridden, emulate cr buffer
  266. */
  267. temp_pic = *pic;
  268. for(i = 0; i < 4; i++)
  269. pic->data[i] = pic->base[i] = NULL;
  270. pic->opaque = NULL;
  271. /* Allocate new frame */
  272. if (s->get_buffer(s, pic))
  273. return -1;
  274. /* Copy image data from old buffer to new buffer */
  275. img_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  276. s->height);
  277. s->release_buffer(s, &temp_pic); // Release old frame
  278. return 0;
  279. }
  280. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, enum PixelFormat * fmt){
  281. return fmt[0];
  282. }
  283. void avcodec_get_context_defaults(AVCodecContext *s){
  284. s->bit_rate= 800*1000;
  285. s->bit_rate_tolerance= s->bit_rate*10;
  286. s->qmin= 2;
  287. s->qmax= 31;
  288. s->mb_qmin= 2;
  289. s->mb_qmax= 31;
  290. s->rc_eq= "tex^qComp";
  291. s->qcompress= 0.5;
  292. s->max_qdiff= 3;
  293. s->b_quant_factor=1.25;
  294. s->b_quant_offset=1.25;
  295. s->i_quant_factor=-0.8;
  296. s->i_quant_offset=0.0;
  297. s->error_concealment= 3;
  298. s->error_resilience= 1;
  299. s->workaround_bugs= FF_BUG_AUTODETECT;
  300. s->frame_rate_base= 1;
  301. s->frame_rate = 25;
  302. s->gop_size= 50;
  303. s->me_method= ME_EPZS;
  304. s->get_buffer= avcodec_default_get_buffer;
  305. s->release_buffer= avcodec_default_release_buffer;
  306. s->get_format= avcodec_default_get_format;
  307. s->me_subpel_quality=8;
  308. s->lmin= FF_QP2LAMBDA * s->qmin;
  309. s->lmax= FF_QP2LAMBDA * s->qmax;
  310. s->sample_aspect_ratio= (AVRational){0,1};
  311. s->ildct_cmp= FF_CMP_VSAD;
  312. s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS;
  313. s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS;
  314. s->palctrl = NULL;
  315. s->reget_buffer= avcodec_default_reget_buffer;
  316. }
  317. /**
  318. * allocates a AVCodecContext and set it to defaults.
  319. * this can be deallocated by simply calling free()
  320. */
  321. AVCodecContext *avcodec_alloc_context(void){
  322. AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
  323. if(avctx==NULL) return NULL;
  324. avcodec_get_context_defaults(avctx);
  325. return avctx;
  326. }
  327. /**
  328. * allocates a AVPFrame and set it to defaults.
  329. * this can be deallocated by simply calling free()
  330. */
  331. AVFrame *avcodec_alloc_frame(void){
  332. AVFrame *pic= av_mallocz(sizeof(AVFrame));
  333. return pic;
  334. }
  335. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  336. {
  337. int ret;
  338. if(avctx->codec)
  339. return -1;
  340. avctx->codec = codec;
  341. avctx->codec_id = codec->id;
  342. avctx->frame_number = 0;
  343. if (codec->priv_data_size > 0) {
  344. avctx->priv_data = av_mallocz(codec->priv_data_size);
  345. if (!avctx->priv_data)
  346. return -ENOMEM;
  347. } else {
  348. avctx->priv_data = NULL;
  349. }
  350. ret = avctx->codec->init(avctx);
  351. if (ret < 0) {
  352. av_freep(&avctx->priv_data);
  353. return ret;
  354. }
  355. return 0;
  356. }
  357. int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  358. const short *samples)
  359. {
  360. int ret;
  361. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  362. avctx->frame_number++;
  363. return ret;
  364. }
  365. int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  366. const AVFrame *pict)
  367. {
  368. int ret;
  369. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  370. emms_c(); //needed to avoid a emms_c() call before every return;
  371. avctx->frame_number++;
  372. return ret;
  373. }
  374. /**
  375. * decode a frame.
  376. * @param buf bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE larger then the actual read bytes
  377. * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  378. * @param buf_size the size of the buffer in bytes
  379. * @param got_picture_ptr zero if no frame could be decompressed, Otherwise, it is non zero
  380. * @return -1 if error, otherwise return the number of
  381. * bytes used.
  382. */
  383. int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  384. int *got_picture_ptr,
  385. uint8_t *buf, int buf_size)
  386. {
  387. int ret;
  388. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  389. buf, buf_size);
  390. emms_c(); //needed to avoid a emms_c() call before every return;
  391. if (*got_picture_ptr)
  392. avctx->frame_number++;
  393. return ret;
  394. }
  395. /* decode an audio frame. return -1 if error, otherwise return the
  396. *number of bytes used. If no frame could be decompressed,
  397. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  398. *size in BYTES. */
  399. int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,
  400. int *frame_size_ptr,
  401. uint8_t *buf, int buf_size)
  402. {
  403. int ret;
  404. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  405. buf, buf_size);
  406. avctx->frame_number++;
  407. return ret;
  408. }
  409. int avcodec_close(AVCodecContext *avctx)
  410. {
  411. if (avctx->codec->close)
  412. avctx->codec->close(avctx);
  413. av_freep(&avctx->priv_data);
  414. avctx->codec = NULL;
  415. return 0;
  416. }
  417. AVCodec *avcodec_find_encoder(enum CodecID id)
  418. {
  419. AVCodec *p;
  420. p = first_avcodec;
  421. while (p) {
  422. if (p->encode != NULL && p->id == id)
  423. return p;
  424. p = p->next;
  425. }
  426. return NULL;
  427. }
  428. AVCodec *avcodec_find_encoder_by_name(const char *name)
  429. {
  430. AVCodec *p;
  431. p = first_avcodec;
  432. while (p) {
  433. if (p->encode != NULL && strcmp(name,p->name) == 0)
  434. return p;
  435. p = p->next;
  436. }
  437. return NULL;
  438. }
  439. AVCodec *avcodec_find_decoder(enum CodecID id)
  440. {
  441. AVCodec *p;
  442. p = first_avcodec;
  443. while (p) {
  444. if (p->decode != NULL && p->id == id)
  445. return p;
  446. p = p->next;
  447. }
  448. return NULL;
  449. }
  450. AVCodec *avcodec_find_decoder_by_name(const char *name)
  451. {
  452. AVCodec *p;
  453. p = first_avcodec;
  454. while (p) {
  455. if (p->decode != NULL && strcmp(name,p->name) == 0)
  456. return p;
  457. p = p->next;
  458. }
  459. return NULL;
  460. }
  461. AVCodec *avcodec_find(enum CodecID id)
  462. {
  463. AVCodec *p;
  464. p = first_avcodec;
  465. while (p) {
  466. if (p->id == id)
  467. return p;
  468. p = p->next;
  469. }
  470. return NULL;
  471. }
  472. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  473. {
  474. const char *codec_name;
  475. AVCodec *p;
  476. char buf1[32];
  477. char channels_str[100];
  478. int bitrate;
  479. if (encode)
  480. p = avcodec_find_encoder(enc->codec_id);
  481. else
  482. p = avcodec_find_decoder(enc->codec_id);
  483. if (p) {
  484. codec_name = p->name;
  485. if (!encode && enc->codec_id == CODEC_ID_MP3) {
  486. if (enc->sub_id == 2)
  487. codec_name = "mp2";
  488. else if (enc->sub_id == 1)
  489. codec_name = "mp1";
  490. }
  491. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  492. /* fake mpeg2 transport stream codec (currently not
  493. registered) */
  494. codec_name = "mpeg2ts";
  495. } else if (enc->codec_name[0] != '\0') {
  496. codec_name = enc->codec_name;
  497. } else {
  498. /* output avi tags */
  499. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  500. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  501. enc->codec_tag & 0xff,
  502. (enc->codec_tag >> 8) & 0xff,
  503. (enc->codec_tag >> 16) & 0xff,
  504. (enc->codec_tag >> 24) & 0xff);
  505. } else {
  506. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  507. }
  508. codec_name = buf1;
  509. }
  510. switch(enc->codec_type) {
  511. case CODEC_TYPE_VIDEO:
  512. snprintf(buf, buf_size,
  513. "Video: %s%s",
  514. codec_name, enc->mb_decision ? " (hq)" : "");
  515. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  516. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  517. ", %s",
  518. avcodec_get_pix_fmt_name(enc->pix_fmt));
  519. }
  520. if (enc->width) {
  521. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  522. ", %dx%d, %0.2f fps",
  523. enc->width, enc->height,
  524. (float)enc->frame_rate / enc->frame_rate_base);
  525. }
  526. if (encode) {
  527. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  528. ", q=%d-%d", enc->qmin, enc->qmax);
  529. }
  530. bitrate = enc->bit_rate;
  531. break;
  532. case CODEC_TYPE_AUDIO:
  533. snprintf(buf, buf_size,
  534. "Audio: %s",
  535. codec_name);
  536. switch (enc->channels) {
  537. case 1:
  538. strcpy(channels_str, "mono");
  539. break;
  540. case 2:
  541. strcpy(channels_str, "stereo");
  542. break;
  543. case 6:
  544. strcpy(channels_str, "5:1");
  545. break;
  546. default:
  547. sprintf(channels_str, "%d channels", enc->channels);
  548. break;
  549. }
  550. if (enc->sample_rate) {
  551. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  552. ", %d Hz, %s",
  553. enc->sample_rate,
  554. channels_str);
  555. }
  556. /* for PCM codecs, compute bitrate directly */
  557. switch(enc->codec_id) {
  558. case CODEC_ID_PCM_S16LE:
  559. case CODEC_ID_PCM_S16BE:
  560. case CODEC_ID_PCM_U16LE:
  561. case CODEC_ID_PCM_U16BE:
  562. bitrate = enc->sample_rate * enc->channels * 16;
  563. break;
  564. case CODEC_ID_PCM_S8:
  565. case CODEC_ID_PCM_U8:
  566. case CODEC_ID_PCM_ALAW:
  567. case CODEC_ID_PCM_MULAW:
  568. bitrate = enc->sample_rate * enc->channels * 8;
  569. break;
  570. default:
  571. bitrate = enc->bit_rate;
  572. break;
  573. }
  574. break;
  575. case CODEC_TYPE_DATA:
  576. snprintf(buf, buf_size, "Data: %s", codec_name);
  577. bitrate = enc->bit_rate;
  578. break;
  579. default:
  580. av_abort();
  581. }
  582. if (encode) {
  583. if (enc->flags & CODEC_FLAG_PASS1)
  584. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  585. ", pass 1");
  586. if (enc->flags & CODEC_FLAG_PASS2)
  587. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  588. ", pass 2");
  589. }
  590. if (bitrate != 0) {
  591. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  592. ", %d kb/s", bitrate / 1000);
  593. }
  594. }
  595. unsigned avcodec_version( void )
  596. {
  597. return LIBAVCODEC_VERSION_INT;
  598. }
  599. unsigned avcodec_build( void )
  600. {
  601. return LIBAVCODEC_BUILD;
  602. }
  603. /* must be called before any other functions */
  604. void avcodec_init(void)
  605. {
  606. static int inited = 0;
  607. if (inited != 0)
  608. return;
  609. inited = 1;
  610. dsputil_static_init();
  611. }
  612. /**
  613. * Flush buffers, should be called when seeking or when swicthing to a different stream.
  614. */
  615. void avcodec_flush_buffers(AVCodecContext *avctx)
  616. {
  617. if(avctx->codec->flush)
  618. avctx->codec->flush(avctx);
  619. }
  620. void avcodec_default_free_buffers(AVCodecContext *s){
  621. int i, j;
  622. if(s->internal_buffer==NULL) return;
  623. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  624. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  625. for(j=0; j<4; j++){
  626. av_freep(&buf->base[j]);
  627. buf->data[j]= NULL;
  628. }
  629. }
  630. av_freep(&s->internal_buffer);
  631. s->internal_buffer_count=0;
  632. }
  633. char av_get_pict_type_char(int pict_type){
  634. switch(pict_type){
  635. case I_TYPE: return 'I';
  636. case P_TYPE: return 'P';
  637. case B_TYPE: return 'B';
  638. case S_TYPE: return 'S';
  639. case SI_TYPE:return 'i';
  640. case SP_TYPE:return 'p';
  641. default: return '?';
  642. }
  643. }
  644. int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
  645. int exact=1, sign=0;
  646. int64_t gcd;
  647. assert(den != 0);
  648. if(den < 0){
  649. den= -den;
  650. nom= -nom;
  651. }
  652. if(nom < 0){
  653. nom= -nom;
  654. sign= 1;
  655. }
  656. gcd = ff_gcd(nom, den);
  657. nom /= gcd;
  658. den /= gcd;
  659. if(nom > max || den > max){
  660. AVRational a0={0,1}, a1={1,0};
  661. exact=0;
  662. for(;;){
  663. int64_t x= nom / den;
  664. int64_t a2n= x*a1.num + a0.num;
  665. int64_t a2d= x*a1.den + a0.den;
  666. if(a2n > max || a2d > max) break;
  667. nom %= den;
  668. a0= a1;
  669. a1= (AVRational){a2n, a2d};
  670. if(nom==0) break;
  671. x= nom; nom=den; den=x;
  672. }
  673. nom= a1.num;
  674. den= a1.den;
  675. }
  676. assert(ff_gcd(nom, den) == 1);
  677. if(sign) nom= -nom;
  678. *dst_nom = nom;
  679. *dst_den = den;
  680. return exact;
  681. }
  682. int64_t av_rescale(int64_t a, int b, int c){
  683. uint64_t h, l;
  684. assert(c > 0);
  685. assert(b >=0);
  686. if(a<0) return -av_rescale(-a, b, c);
  687. h= a>>32;
  688. if(h==0) return a*b/c;
  689. l= a&0xFFFFFFFF;
  690. l *= b;
  691. h *= b;
  692. l += (h%c)<<32;
  693. return ((h/c)<<32) + l/c;
  694. }
  695. /* av_log API */
  696. #ifdef AV_LOG_TRAP_PRINTF
  697. #undef stderr
  698. #undef fprintf
  699. #endif
  700. static int av_log_level = AV_LOG_DEBUG;
  701. static void av_log_default_callback(AVCodecContext* avctx, int level, const char* fmt, va_list vl)
  702. {
  703. static int print_prefix=1;
  704. if(level>av_log_level)
  705. return;
  706. if(avctx && print_prefix)
  707. fprintf(stderr, "[%s @ %p]", avctx->codec->name, avctx);
  708. print_prefix= (int)strstr(fmt, "\n");
  709. vfprintf(stderr, fmt, vl);
  710. }
  711. static void (*av_log_callback)(AVCodecContext*, int, const char*, va_list) = av_log_default_callback;
  712. void av_log(AVCodecContext* avctx, int level, const char *fmt, ...)
  713. {
  714. va_list vl;
  715. va_start(vl, fmt);
  716. av_vlog(avctx, level, fmt, vl);
  717. va_end(vl);
  718. }
  719. void av_vlog(AVCodecContext* avctx, int level, const char *fmt, va_list vl)
  720. {
  721. av_log_callback(avctx, level, fmt, vl);
  722. }
  723. int av_log_get_level(void)
  724. {
  725. return av_log_level;
  726. }
  727. void av_log_set_level(int level)
  728. {
  729. av_log_level = level;
  730. }
  731. void av_log_set_callback(void (*callback)(AVCodecContext*, int, const char*, va_list))
  732. {
  733. av_log_callback = callback;
  734. }