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.

999 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_mallocz((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. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  503. buf, buf_size);
  504. emms_c(); //needed to avoid a emms_c() call before every return;
  505. if (*got_picture_ptr)
  506. avctx->frame_number++;
  507. return ret;
  508. }
  509. /* decode an audio frame. return -1 if error, otherwise return the
  510. *number of bytes used. If no frame could be decompressed,
  511. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  512. *size in BYTES. */
  513. int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,
  514. int *frame_size_ptr,
  515. uint8_t *buf, int buf_size)
  516. {
  517. int ret;
  518. *frame_size_ptr= 0;
  519. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  520. buf, buf_size);
  521. avctx->frame_number++;
  522. return ret;
  523. }
  524. int avcodec_close(AVCodecContext *avctx)
  525. {
  526. if (avctx->codec->close)
  527. avctx->codec->close(avctx);
  528. avcodec_default_free_buffers(avctx);
  529. av_freep(&avctx->priv_data);
  530. avctx->codec = NULL;
  531. return 0;
  532. }
  533. AVCodec *avcodec_find_encoder(enum CodecID id)
  534. {
  535. AVCodec *p;
  536. p = first_avcodec;
  537. while (p) {
  538. if (p->encode != NULL && p->id == id)
  539. return p;
  540. p = p->next;
  541. }
  542. return NULL;
  543. }
  544. AVCodec *avcodec_find_encoder_by_name(const char *name)
  545. {
  546. AVCodec *p;
  547. p = first_avcodec;
  548. while (p) {
  549. if (p->encode != NULL && strcmp(name,p->name) == 0)
  550. return p;
  551. p = p->next;
  552. }
  553. return NULL;
  554. }
  555. AVCodec *avcodec_find_decoder(enum CodecID id)
  556. {
  557. AVCodec *p;
  558. p = first_avcodec;
  559. while (p) {
  560. if (p->decode != NULL && p->id == id)
  561. return p;
  562. p = p->next;
  563. }
  564. return NULL;
  565. }
  566. AVCodec *avcodec_find_decoder_by_name(const char *name)
  567. {
  568. AVCodec *p;
  569. p = first_avcodec;
  570. while (p) {
  571. if (p->decode != NULL && strcmp(name,p->name) == 0)
  572. return p;
  573. p = p->next;
  574. }
  575. return NULL;
  576. }
  577. static AVCodec *avcodec_find(enum CodecID id)
  578. {
  579. AVCodec *p;
  580. p = first_avcodec;
  581. while (p) {
  582. if (p->id == id)
  583. return p;
  584. p = p->next;
  585. }
  586. return NULL;
  587. }
  588. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  589. {
  590. const char *codec_name;
  591. AVCodec *p;
  592. char buf1[32];
  593. char channels_str[100];
  594. int bitrate;
  595. if (encode)
  596. p = avcodec_find_encoder(enc->codec_id);
  597. else
  598. p = avcodec_find_decoder(enc->codec_id);
  599. if (p) {
  600. codec_name = p->name;
  601. if (!encode && enc->codec_id == CODEC_ID_MP3) {
  602. if (enc->sub_id == 2)
  603. codec_name = "mp2";
  604. else if (enc->sub_id == 1)
  605. codec_name = "mp1";
  606. }
  607. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  608. /* fake mpeg2 transport stream codec (currently not
  609. registered) */
  610. codec_name = "mpeg2ts";
  611. } else if (enc->codec_name[0] != '\0') {
  612. codec_name = enc->codec_name;
  613. } else {
  614. /* output avi tags */
  615. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  616. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  617. enc->codec_tag & 0xff,
  618. (enc->codec_tag >> 8) & 0xff,
  619. (enc->codec_tag >> 16) & 0xff,
  620. (enc->codec_tag >> 24) & 0xff);
  621. } else {
  622. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  623. }
  624. codec_name = buf1;
  625. }
  626. switch(enc->codec_type) {
  627. case CODEC_TYPE_VIDEO:
  628. snprintf(buf, buf_size,
  629. "Video: %s%s",
  630. codec_name, enc->mb_decision ? " (hq)" : "");
  631. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  632. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  633. ", %s",
  634. avcodec_get_pix_fmt_name(enc->pix_fmt));
  635. }
  636. if (enc->width) {
  637. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  638. ", %dx%d, %0.2f fps",
  639. enc->width, enc->height,
  640. (float)enc->frame_rate / enc->frame_rate_base);
  641. }
  642. if (encode) {
  643. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  644. ", q=%d-%d", enc->qmin, enc->qmax);
  645. }
  646. bitrate = enc->bit_rate;
  647. break;
  648. case CODEC_TYPE_AUDIO:
  649. snprintf(buf, buf_size,
  650. "Audio: %s",
  651. codec_name);
  652. switch (enc->channels) {
  653. case 1:
  654. strcpy(channels_str, "mono");
  655. break;
  656. case 2:
  657. strcpy(channels_str, "stereo");
  658. break;
  659. case 6:
  660. strcpy(channels_str, "5:1");
  661. break;
  662. default:
  663. sprintf(channels_str, "%d channels", enc->channels);
  664. break;
  665. }
  666. if (enc->sample_rate) {
  667. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  668. ", %d Hz, %s",
  669. enc->sample_rate,
  670. channels_str);
  671. }
  672. /* for PCM codecs, compute bitrate directly */
  673. switch(enc->codec_id) {
  674. case CODEC_ID_PCM_S16LE:
  675. case CODEC_ID_PCM_S16BE:
  676. case CODEC_ID_PCM_U16LE:
  677. case CODEC_ID_PCM_U16BE:
  678. bitrate = enc->sample_rate * enc->channels * 16;
  679. break;
  680. case CODEC_ID_PCM_S8:
  681. case CODEC_ID_PCM_U8:
  682. case CODEC_ID_PCM_ALAW:
  683. case CODEC_ID_PCM_MULAW:
  684. bitrate = enc->sample_rate * enc->channels * 8;
  685. break;
  686. default:
  687. bitrate = enc->bit_rate;
  688. break;
  689. }
  690. break;
  691. case CODEC_TYPE_DATA:
  692. snprintf(buf, buf_size, "Data: %s", codec_name);
  693. bitrate = enc->bit_rate;
  694. break;
  695. default:
  696. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  697. return;
  698. }
  699. if (encode) {
  700. if (enc->flags & CODEC_FLAG_PASS1)
  701. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  702. ", pass 1");
  703. if (enc->flags & CODEC_FLAG_PASS2)
  704. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  705. ", pass 2");
  706. }
  707. if (bitrate != 0) {
  708. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  709. ", %d kb/s", bitrate / 1000);
  710. }
  711. }
  712. unsigned avcodec_version( void )
  713. {
  714. return LIBAVCODEC_VERSION_INT;
  715. }
  716. unsigned avcodec_build( void )
  717. {
  718. return LIBAVCODEC_BUILD;
  719. }
  720. /* must be called before any other functions */
  721. void avcodec_init(void)
  722. {
  723. static int inited = 0;
  724. if (inited != 0)
  725. return;
  726. inited = 1;
  727. dsputil_static_init();
  728. }
  729. /**
  730. * Flush buffers, should be called when seeking or when swicthing to a different stream.
  731. */
  732. void avcodec_flush_buffers(AVCodecContext *avctx)
  733. {
  734. if(avctx->codec->flush)
  735. avctx->codec->flush(avctx);
  736. }
  737. void avcodec_default_free_buffers(AVCodecContext *s){
  738. int i, j;
  739. if(s->internal_buffer==NULL) return;
  740. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  741. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  742. for(j=0; j<4; j++){
  743. av_freep(&buf->base[j]);
  744. buf->data[j]= NULL;
  745. }
  746. }
  747. av_freep(&s->internal_buffer);
  748. s->internal_buffer_count=0;
  749. }
  750. char av_get_pict_type_char(int pict_type){
  751. switch(pict_type){
  752. case I_TYPE: return 'I';
  753. case P_TYPE: return 'P';
  754. case B_TYPE: return 'B';
  755. case S_TYPE: return 'S';
  756. case SI_TYPE:return 'i';
  757. case SP_TYPE:return 'p';
  758. default: return '?';
  759. }
  760. }
  761. int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
  762. AVRational a0={0,1}, a1={1,0};
  763. int sign= (nom<0) ^ (den<0);
  764. int64_t gcd= ff_gcd(ABS(nom), ABS(den));
  765. nom = ABS(nom)/gcd;
  766. den = ABS(den)/gcd;
  767. if(nom<=max && den<=max){
  768. a1= (AVRational){nom, den};
  769. den=0;
  770. }
  771. while(den){
  772. int64_t x = nom / den;
  773. int64_t next_den= nom - den*x;
  774. int64_t a2n= x*a1.num + a0.num;
  775. int64_t a2d= x*a1.den + a0.den;
  776. if(a2n > max || a2d > max) break;
  777. a0= a1;
  778. a1= (AVRational){a2n, a2d};
  779. nom= den;
  780. den= next_den;
  781. }
  782. assert(ff_gcd(a1.num, a1.den) == 1);
  783. *dst_nom = sign ? -a1.num : a1.num;
  784. *dst_den = a1.den;
  785. return den==0;
  786. }
  787. int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd){
  788. AVInteger ai;
  789. int64_t r=0;
  790. assert(c > 0);
  791. assert(b >=0);
  792. assert(rnd >=0 && rnd<=5 && rnd!=4);
  793. if(a<0 && a != INT64_MIN) return -av_rescale_rnd(-a, b, c, rnd ^ ((rnd>>1)&1));
  794. if(rnd==AV_ROUND_NEAR_INF) r= c/2;
  795. else if(rnd&1) r= c-1;
  796. if(b<=INT_MAX && c<=INT_MAX){
  797. if(a<=INT_MAX)
  798. return (a * b + r)/c;
  799. else
  800. return a/c*b + (a%c*b + r)/c;
  801. }
  802. ai= av_mul_i(av_int2i(a), av_int2i(b));
  803. ai= av_add_i(ai, av_int2i(r));
  804. return av_i2int(av_div_i(ai, av_int2i(c)));
  805. }
  806. int64_t av_rescale(int64_t a, int64_t b, int64_t c){
  807. return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
  808. }
  809. int64_t ff_gcd(int64_t a, int64_t b){
  810. if(b) return ff_gcd(b, a%b);
  811. else return a;
  812. }
  813. /* av_log API */
  814. static int av_log_level = AV_LOG_DEBUG;
  815. static void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
  816. {
  817. static int print_prefix=1;
  818. AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
  819. if(level>av_log_level)
  820. return;
  821. #undef fprintf
  822. if(print_prefix && avc) {
  823. fprintf(stderr, "[%s @ %p]", avc->item_name(ptr), avc);
  824. }
  825. #define fprintf please_use_av_log
  826. print_prefix= strstr(fmt, "\n") != NULL;
  827. vfprintf(stderr, fmt, vl);
  828. }
  829. static void (*av_log_callback)(void*, int, const char*, va_list) = av_log_default_callback;
  830. void av_log(void* avcl, int level, const char *fmt, ...)
  831. {
  832. va_list vl;
  833. va_start(vl, fmt);
  834. av_vlog(avcl, level, fmt, vl);
  835. va_end(vl);
  836. }
  837. void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
  838. {
  839. av_log_callback(avcl, level, fmt, vl);
  840. }
  841. int av_log_get_level(void)
  842. {
  843. return av_log_level;
  844. }
  845. void av_log_set_level(int level)
  846. {
  847. av_log_level = level;
  848. }
  849. void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
  850. {
  851. av_log_callback = callback;
  852. }
  853. #if !defined(HAVE_THREADS)
  854. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  855. return -1;
  856. }
  857. #endif