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.

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