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.

902 lines
23KB

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