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.

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