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.

940 lines
24KB

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