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.

769 lines
19KB

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