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.

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