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.

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