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.

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