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.

820 lines
20KB

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