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.

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