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.

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