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.

969 lines
26KB

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