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.

887 lines
22KB

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