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.

1003 lines
27KB

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