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.

826 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. s->cr_available = 0;
  286. }
  287. /**
  288. * allocates a AVCodecContext and set it to defaults.
  289. * this can be deallocated by simply calling free()
  290. */
  291. AVCodecContext *avcodec_alloc_context(void){
  292. AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
  293. if(avctx==NULL) return NULL;
  294. avcodec_get_context_defaults(avctx);
  295. return avctx;
  296. }
  297. /**
  298. * allocates a AVPFrame and set it to defaults.
  299. * this can be deallocated by simply calling free()
  300. */
  301. AVFrame *avcodec_alloc_frame(void){
  302. AVFrame *pic= av_mallocz(sizeof(AVFrame));
  303. return pic;
  304. }
  305. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  306. {
  307. int ret;
  308. if(avctx->codec)
  309. return -1;
  310. avctx->codec = codec;
  311. avctx->codec_id = codec->id;
  312. avctx->frame_number = 0;
  313. if (codec->priv_data_size > 0) {
  314. avctx->priv_data = av_mallocz(codec->priv_data_size);
  315. if (!avctx->priv_data)
  316. return -ENOMEM;
  317. } else {
  318. avctx->priv_data = NULL;
  319. }
  320. ret = avctx->codec->init(avctx);
  321. if (ret < 0) {
  322. av_freep(&avctx->priv_data);
  323. return ret;
  324. }
  325. return 0;
  326. }
  327. int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  328. const short *samples)
  329. {
  330. int ret;
  331. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  332. avctx->frame_number++;
  333. return ret;
  334. }
  335. int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  336. const AVFrame *pict)
  337. {
  338. int ret;
  339. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  340. emms_c(); //needed to avoid a emms_c() call before every return;
  341. avctx->frame_number++;
  342. return ret;
  343. }
  344. /**
  345. * decode a frame.
  346. * @param buf bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE larger then the actual read bytes
  347. * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  348. * @param buf_size the size of the buffer in bytes
  349. * @param got_picture_ptr zero if no frame could be decompressed, Otherwise, it is non zero
  350. * @return -1 if error, otherwise return the number of
  351. * bytes used.
  352. */
  353. int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  354. int *got_picture_ptr,
  355. uint8_t *buf, int buf_size)
  356. {
  357. int ret;
  358. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  359. buf, buf_size);
  360. emms_c(); //needed to avoid a emms_c() call before every return;
  361. if (*got_picture_ptr)
  362. avctx->frame_number++;
  363. return ret;
  364. }
  365. /* decode an audio frame. return -1 if error, otherwise return the
  366. *number of bytes used. If no frame could be decompressed,
  367. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  368. *size in BYTES. */
  369. int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,
  370. int *frame_size_ptr,
  371. uint8_t *buf, int buf_size)
  372. {
  373. int ret;
  374. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  375. buf, buf_size);
  376. avctx->frame_number++;
  377. return ret;
  378. }
  379. int avcodec_close(AVCodecContext *avctx)
  380. {
  381. if (avctx->codec->close)
  382. avctx->codec->close(avctx);
  383. av_freep(&avctx->priv_data);
  384. avctx->codec = NULL;
  385. return 0;
  386. }
  387. AVCodec *avcodec_find_encoder(enum CodecID id)
  388. {
  389. AVCodec *p;
  390. p = first_avcodec;
  391. while (p) {
  392. if (p->encode != NULL && p->id == id)
  393. return p;
  394. p = p->next;
  395. }
  396. return NULL;
  397. }
  398. AVCodec *avcodec_find_encoder_by_name(const char *name)
  399. {
  400. AVCodec *p;
  401. p = first_avcodec;
  402. while (p) {
  403. if (p->encode != NULL && strcmp(name,p->name) == 0)
  404. return p;
  405. p = p->next;
  406. }
  407. return NULL;
  408. }
  409. AVCodec *avcodec_find_decoder(enum CodecID id)
  410. {
  411. AVCodec *p;
  412. p = first_avcodec;
  413. while (p) {
  414. if (p->decode != NULL && p->id == id)
  415. return p;
  416. p = p->next;
  417. }
  418. return NULL;
  419. }
  420. AVCodec *avcodec_find_decoder_by_name(const char *name)
  421. {
  422. AVCodec *p;
  423. p = first_avcodec;
  424. while (p) {
  425. if (p->decode != NULL && strcmp(name,p->name) == 0)
  426. return p;
  427. p = p->next;
  428. }
  429. return NULL;
  430. }
  431. AVCodec *avcodec_find(enum CodecID id)
  432. {
  433. AVCodec *p;
  434. p = first_avcodec;
  435. while (p) {
  436. if (p->id == id)
  437. return p;
  438. p = p->next;
  439. }
  440. return NULL;
  441. }
  442. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  443. {
  444. const char *codec_name;
  445. AVCodec *p;
  446. char buf1[32];
  447. char channels_str[100];
  448. int bitrate;
  449. if (encode)
  450. p = avcodec_find_encoder(enc->codec_id);
  451. else
  452. p = avcodec_find_decoder(enc->codec_id);
  453. if (p) {
  454. codec_name = p->name;
  455. if (!encode && enc->codec_id == CODEC_ID_MP3) {
  456. if (enc->sub_id == 2)
  457. codec_name = "mp2";
  458. else if (enc->sub_id == 1)
  459. codec_name = "mp1";
  460. }
  461. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  462. /* fake mpeg2 transport stream codec (currently not
  463. registered) */
  464. codec_name = "mpeg2ts";
  465. } else if (enc->codec_name[0] != '\0') {
  466. codec_name = enc->codec_name;
  467. } else {
  468. /* output avi tags */
  469. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  470. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  471. enc->codec_tag & 0xff,
  472. (enc->codec_tag >> 8) & 0xff,
  473. (enc->codec_tag >> 16) & 0xff,
  474. (enc->codec_tag >> 24) & 0xff);
  475. } else {
  476. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  477. }
  478. codec_name = buf1;
  479. }
  480. switch(enc->codec_type) {
  481. case CODEC_TYPE_VIDEO:
  482. snprintf(buf, buf_size,
  483. "Video: %s%s",
  484. codec_name, enc->mb_decision ? " (hq)" : "");
  485. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  486. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  487. ", %s",
  488. avcodec_get_pix_fmt_name(enc->pix_fmt));
  489. }
  490. if (enc->width) {
  491. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  492. ", %dx%d, %0.2f fps",
  493. enc->width, enc->height,
  494. (float)enc->frame_rate / enc->frame_rate_base);
  495. }
  496. if (encode) {
  497. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  498. ", q=%d-%d", enc->qmin, enc->qmax);
  499. }
  500. bitrate = enc->bit_rate;
  501. break;
  502. case CODEC_TYPE_AUDIO:
  503. snprintf(buf, buf_size,
  504. "Audio: %s",
  505. codec_name);
  506. switch (enc->channels) {
  507. case 1:
  508. strcpy(channels_str, "mono");
  509. break;
  510. case 2:
  511. strcpy(channels_str, "stereo");
  512. break;
  513. case 6:
  514. strcpy(channels_str, "5:1");
  515. break;
  516. default:
  517. sprintf(channels_str, "%d channels", enc->channels);
  518. break;
  519. }
  520. if (enc->sample_rate) {
  521. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  522. ", %d Hz, %s",
  523. enc->sample_rate,
  524. channels_str);
  525. }
  526. /* for PCM codecs, compute bitrate directly */
  527. switch(enc->codec_id) {
  528. case CODEC_ID_PCM_S16LE:
  529. case CODEC_ID_PCM_S16BE:
  530. case CODEC_ID_PCM_U16LE:
  531. case CODEC_ID_PCM_U16BE:
  532. bitrate = enc->sample_rate * enc->channels * 16;
  533. break;
  534. case CODEC_ID_PCM_S8:
  535. case CODEC_ID_PCM_U8:
  536. case CODEC_ID_PCM_ALAW:
  537. case CODEC_ID_PCM_MULAW:
  538. bitrate = enc->sample_rate * enc->channels * 8;
  539. break;
  540. default:
  541. bitrate = enc->bit_rate;
  542. break;
  543. }
  544. break;
  545. case CODEC_TYPE_DATA:
  546. snprintf(buf, buf_size, "Data: %s", codec_name);
  547. bitrate = enc->bit_rate;
  548. break;
  549. default:
  550. av_abort();
  551. }
  552. if (encode) {
  553. if (enc->flags & CODEC_FLAG_PASS1)
  554. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  555. ", pass 1");
  556. if (enc->flags & CODEC_FLAG_PASS2)
  557. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  558. ", pass 2");
  559. }
  560. if (bitrate != 0) {
  561. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  562. ", %d kb/s", bitrate / 1000);
  563. }
  564. }
  565. unsigned avcodec_version( void )
  566. {
  567. return LIBAVCODEC_VERSION_INT;
  568. }
  569. unsigned avcodec_build( void )
  570. {
  571. return LIBAVCODEC_BUILD;
  572. }
  573. /* must be called before any other functions */
  574. void avcodec_init(void)
  575. {
  576. static int inited = 0;
  577. if (inited != 0)
  578. return;
  579. inited = 1;
  580. dsputil_static_init();
  581. }
  582. /**
  583. * Flush buffers, should be called when seeking or when swicthing to a different stream.
  584. */
  585. void avcodec_flush_buffers(AVCodecContext *avctx)
  586. {
  587. if(avctx->codec->flush)
  588. avctx->codec->flush(avctx);
  589. }
  590. void avcodec_default_free_buffers(AVCodecContext *s){
  591. int i, j;
  592. if(s->internal_buffer==NULL) return;
  593. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  594. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  595. for(j=0; j<4; j++){
  596. av_freep(&buf->base[j]);
  597. buf->data[j]= NULL;
  598. }
  599. }
  600. av_freep(&s->internal_buffer);
  601. s->internal_buffer_count=0;
  602. }
  603. char av_get_pict_type_char(int pict_type){
  604. switch(pict_type){
  605. case I_TYPE: return 'I';
  606. case P_TYPE: return 'P';
  607. case B_TYPE: return 'B';
  608. case S_TYPE: return 'S';
  609. case SI_TYPE:return 'i';
  610. case SP_TYPE:return 'p';
  611. default: return '?';
  612. }
  613. }
  614. int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
  615. int exact=1, sign=0;
  616. int64_t gcd;
  617. assert(den != 0);
  618. if(den < 0){
  619. den= -den;
  620. nom= -nom;
  621. }
  622. if(nom < 0){
  623. nom= -nom;
  624. sign= 1;
  625. }
  626. gcd = ff_gcd(nom, den);
  627. nom /= gcd;
  628. den /= gcd;
  629. if(nom > max || den > max){
  630. AVRational a0={0,1}, a1={1,0};
  631. exact=0;
  632. for(;;){
  633. int64_t x= nom / den;
  634. int64_t a2n= x*a1.num + a0.num;
  635. int64_t a2d= x*a1.den + a0.den;
  636. if(a2n > max || a2d > max) break;
  637. nom %= den;
  638. a0= a1;
  639. a1= (AVRational){a2n, a2d};
  640. if(nom==0) break;
  641. x= nom; nom=den; den=x;
  642. }
  643. nom= a1.num;
  644. den= a1.den;
  645. }
  646. assert(ff_gcd(nom, den) == 1);
  647. if(sign) nom= -nom;
  648. *dst_nom = nom;
  649. *dst_den = den;
  650. return exact;
  651. }
  652. int64_t av_rescale(int64_t a, int b, int c){
  653. uint64_t h, l;
  654. assert(c > 0);
  655. assert(b >=0);
  656. if(a<0) return -av_rescale(-a, b, c);
  657. h= a>>32;
  658. if(h==0) return a*b/c;
  659. l= a&0xFFFFFFFF;
  660. l *= b;
  661. h *= b;
  662. l += (h%c)<<32;
  663. return ((h/c)<<32) + l/c;
  664. }
  665. /* av_log API */
  666. #ifdef AV_LOG_TRAP_PRINTF
  667. #undef stderr
  668. #undef fprintf
  669. #endif
  670. static int av_log_level = AV_LOG_DEBUG;
  671. static void av_log_default_callback(AVCodecContext* avctx, int level, const char* fmt, va_list vl)
  672. {
  673. static int print_prefix=1;
  674. if(level>av_log_level)
  675. return;
  676. if(avctx && print_prefix)
  677. fprintf(stderr, "[%s @ %p]", avctx->codec->name, avctx);
  678. print_prefix= (int)strstr(fmt, "\n");
  679. vfprintf(stderr, fmt, vl);
  680. }
  681. static void (*av_log_callback)(AVCodecContext*, int, const char*, va_list) = av_log_default_callback;
  682. void av_log(AVCodecContext* avctx, int level, const char *fmt, ...)
  683. {
  684. va_list vl;
  685. va_start(vl, fmt);
  686. av_vlog(avctx, level, fmt, vl);
  687. va_end(vl);
  688. }
  689. void av_vlog(AVCodecContext* avctx, int level, const char *fmt, va_list vl)
  690. {
  691. av_log_callback(avctx, level, fmt, vl);
  692. }
  693. int av_log_get_level(void)
  694. {
  695. return av_log_level;
  696. }
  697. void av_log_set_level(int level)
  698. {
  699. av_log_level = level;
  700. }
  701. void av_log_set_callback(void (*callback)(AVCodecContext*, int, const char*, va_list))
  702. {
  703. av_log_callback = callback;
  704. }