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.

963 lines
25KB

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