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.

993 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_lmin= FF_QP2LAMBDA * 2;
  368. s->mb_lmax= FF_QP2LAMBDA * 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->me_penalty_compensation= 256;
  396. s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS;
  397. s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS;
  398. s->palctrl = NULL;
  399. s->reget_buffer= avcodec_default_reget_buffer;
  400. }
  401. /**
  402. * allocates a AVCodecContext and set it to defaults.
  403. * this can be deallocated by simply calling free()
  404. */
  405. AVCodecContext *avcodec_alloc_context(void){
  406. AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
  407. if(avctx==NULL) return NULL;
  408. avcodec_get_context_defaults(avctx);
  409. return avctx;
  410. }
  411. void avcodec_get_frame_defaults(AVFrame *pic){
  412. memset(pic, 0, sizeof(AVFrame));
  413. pic->pts= AV_NOPTS_VALUE;
  414. pic->key_frame= 1;
  415. }
  416. /**
  417. * allocates a AVPFrame and set it to defaults.
  418. * this can be deallocated by simply calling free()
  419. */
  420. AVFrame *avcodec_alloc_frame(void){
  421. AVFrame *pic= av_malloc(sizeof(AVFrame));
  422. if(pic==NULL) return NULL;
  423. avcodec_get_frame_defaults(pic);
  424. return pic;
  425. }
  426. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  427. {
  428. int ret;
  429. if(avctx->codec)
  430. return -1;
  431. avctx->codec = codec;
  432. avctx->codec_id = codec->id;
  433. avctx->frame_number = 0;
  434. if (codec->priv_data_size > 0) {
  435. avctx->priv_data = av_mallocz(codec->priv_data_size);
  436. if (!avctx->priv_data)
  437. return -ENOMEM;
  438. } else {
  439. avctx->priv_data = NULL;
  440. }
  441. if(avctx->coded_width && avctx->coded_height)
  442. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  443. else if(avctx->width && avctx->height)
  444. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  445. if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height)){
  446. av_freep(&avctx->priv_data);
  447. return -1;
  448. }
  449. ret = avctx->codec->init(avctx);
  450. if (ret < 0) {
  451. av_freep(&avctx->priv_data);
  452. return ret;
  453. }
  454. return 0;
  455. }
  456. int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  457. const short *samples)
  458. {
  459. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  460. av_log(avctx, AV_LOG_ERROR, "buffer smaller then minimum size\n");
  461. return -1;
  462. }
  463. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  464. int ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  465. avctx->frame_number++;
  466. return ret;
  467. }else
  468. return 0;
  469. }
  470. int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  471. const AVFrame *pict)
  472. {
  473. if(buf_size < FF_MIN_BUFFER_SIZE){
  474. av_log(avctx, AV_LOG_ERROR, "buffer smaller then minimum size\n");
  475. return -1;
  476. }
  477. if(avcodec_check_dimensions(avctx,avctx->width,avctx->height))
  478. return -1;
  479. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  480. int ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  481. avctx->frame_number++;
  482. emms_c(); //needed to avoid a emms_c() call before every return;
  483. return ret;
  484. }else
  485. return 0;
  486. }
  487. /**
  488. * decode a frame.
  489. * @param buf bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE larger then the actual read bytes
  490. * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  491. * @param buf_size the size of the buffer in bytes
  492. * @param got_picture_ptr zero if no frame could be decompressed, Otherwise, it is non zero
  493. * @return -1 if error, otherwise return the number of
  494. * bytes used.
  495. */
  496. int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  497. int *got_picture_ptr,
  498. uint8_t *buf, int buf_size)
  499. {
  500. int ret;
  501. *got_picture_ptr= 0;
  502. if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height))
  503. return -1;
  504. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){
  505. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  506. buf, buf_size);
  507. emms_c(); //needed to avoid a emms_c() call before every return;
  508. if (*got_picture_ptr)
  509. avctx->frame_number++;
  510. }else
  511. ret= 0;
  512. return ret;
  513. }
  514. /* decode an audio frame. return -1 if error, otherwise return the
  515. *number of bytes used. If no frame could be decompressed,
  516. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  517. *size in BYTES. */
  518. int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,
  519. int *frame_size_ptr,
  520. uint8_t *buf, int buf_size)
  521. {
  522. int ret;
  523. *frame_size_ptr= 0;
  524. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  525. buf, buf_size);
  526. avctx->frame_number++;
  527. return ret;
  528. }
  529. int avcodec_close(AVCodecContext *avctx)
  530. {
  531. if (avctx->codec->close)
  532. avctx->codec->close(avctx);
  533. avcodec_default_free_buffers(avctx);
  534. av_freep(&avctx->priv_data);
  535. avctx->codec = NULL;
  536. return 0;
  537. }
  538. AVCodec *avcodec_find_encoder(enum CodecID id)
  539. {
  540. AVCodec *p;
  541. p = first_avcodec;
  542. while (p) {
  543. if (p->encode != NULL && p->id == id)
  544. return p;
  545. p = p->next;
  546. }
  547. return NULL;
  548. }
  549. AVCodec *avcodec_find_encoder_by_name(const char *name)
  550. {
  551. AVCodec *p;
  552. p = first_avcodec;
  553. while (p) {
  554. if (p->encode != NULL && strcmp(name,p->name) == 0)
  555. return p;
  556. p = p->next;
  557. }
  558. return NULL;
  559. }
  560. AVCodec *avcodec_find_decoder(enum CodecID id)
  561. {
  562. AVCodec *p;
  563. p = first_avcodec;
  564. while (p) {
  565. if (p->decode != NULL && p->id == id)
  566. return p;
  567. p = p->next;
  568. }
  569. return NULL;
  570. }
  571. AVCodec *avcodec_find_decoder_by_name(const char *name)
  572. {
  573. AVCodec *p;
  574. p = first_avcodec;
  575. while (p) {
  576. if (p->decode != NULL && strcmp(name,p->name) == 0)
  577. return p;
  578. p = p->next;
  579. }
  580. return NULL;
  581. }
  582. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  583. {
  584. const char *codec_name;
  585. AVCodec *p;
  586. char buf1[32];
  587. char channels_str[100];
  588. int bitrate;
  589. if (encode)
  590. p = avcodec_find_encoder(enc->codec_id);
  591. else
  592. p = avcodec_find_decoder(enc->codec_id);
  593. if (p) {
  594. codec_name = p->name;
  595. if (!encode && enc->codec_id == CODEC_ID_MP3) {
  596. if (enc->sub_id == 2)
  597. codec_name = "mp2";
  598. else if (enc->sub_id == 1)
  599. codec_name = "mp1";
  600. }
  601. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  602. /* fake mpeg2 transport stream codec (currently not
  603. registered) */
  604. codec_name = "mpeg2ts";
  605. } else if (enc->codec_name[0] != '\0') {
  606. codec_name = enc->codec_name;
  607. } else {
  608. /* output avi tags */
  609. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  610. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  611. enc->codec_tag & 0xff,
  612. (enc->codec_tag >> 8) & 0xff,
  613. (enc->codec_tag >> 16) & 0xff,
  614. (enc->codec_tag >> 24) & 0xff);
  615. } else {
  616. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  617. }
  618. codec_name = buf1;
  619. }
  620. switch(enc->codec_type) {
  621. case CODEC_TYPE_VIDEO:
  622. snprintf(buf, buf_size,
  623. "Video: %s%s",
  624. codec_name, enc->mb_decision ? " (hq)" : "");
  625. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  626. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  627. ", %s",
  628. avcodec_get_pix_fmt_name(enc->pix_fmt));
  629. }
  630. if (enc->width) {
  631. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  632. ", %dx%d, %0.2f fps",
  633. enc->width, enc->height,
  634. (float)enc->frame_rate / enc->frame_rate_base);
  635. }
  636. if (encode) {
  637. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  638. ", q=%d-%d", enc->qmin, enc->qmax);
  639. }
  640. bitrate = enc->bit_rate;
  641. break;
  642. case CODEC_TYPE_AUDIO:
  643. snprintf(buf, buf_size,
  644. "Audio: %s",
  645. codec_name);
  646. switch (enc->channels) {
  647. case 1:
  648. strcpy(channels_str, "mono");
  649. break;
  650. case 2:
  651. strcpy(channels_str, "stereo");
  652. break;
  653. case 6:
  654. strcpy(channels_str, "5:1");
  655. break;
  656. default:
  657. snprintf(channels_str, sizeof(channels_str), "%d channels", enc->channels);
  658. break;
  659. }
  660. if (enc->sample_rate) {
  661. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  662. ", %d Hz, %s",
  663. enc->sample_rate,
  664. channels_str);
  665. }
  666. /* for PCM codecs, compute bitrate directly */
  667. switch(enc->codec_id) {
  668. case CODEC_ID_PCM_S16LE:
  669. case CODEC_ID_PCM_S16BE:
  670. case CODEC_ID_PCM_U16LE:
  671. case CODEC_ID_PCM_U16BE:
  672. bitrate = enc->sample_rate * enc->channels * 16;
  673. break;
  674. case CODEC_ID_PCM_S8:
  675. case CODEC_ID_PCM_U8:
  676. case CODEC_ID_PCM_ALAW:
  677. case CODEC_ID_PCM_MULAW:
  678. bitrate = enc->sample_rate * enc->channels * 8;
  679. break;
  680. default:
  681. bitrate = enc->bit_rate;
  682. break;
  683. }
  684. break;
  685. case CODEC_TYPE_DATA:
  686. snprintf(buf, buf_size, "Data: %s", codec_name);
  687. bitrate = enc->bit_rate;
  688. break;
  689. default:
  690. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  691. return;
  692. }
  693. if (encode) {
  694. if (enc->flags & CODEC_FLAG_PASS1)
  695. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  696. ", pass 1");
  697. if (enc->flags & CODEC_FLAG_PASS2)
  698. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  699. ", pass 2");
  700. }
  701. if (bitrate != 0) {
  702. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  703. ", %d kb/s", bitrate / 1000);
  704. }
  705. }
  706. unsigned avcodec_version( void )
  707. {
  708. return LIBAVCODEC_VERSION_INT;
  709. }
  710. unsigned avcodec_build( void )
  711. {
  712. return LIBAVCODEC_BUILD;
  713. }
  714. /* must be called before any other functions */
  715. void avcodec_init(void)
  716. {
  717. static int inited = 0;
  718. if (inited != 0)
  719. return;
  720. inited = 1;
  721. dsputil_static_init();
  722. }
  723. /**
  724. * Flush buffers, should be called when seeking or when swicthing to a different stream.
  725. */
  726. void avcodec_flush_buffers(AVCodecContext *avctx)
  727. {
  728. if(avctx->codec->flush)
  729. avctx->codec->flush(avctx);
  730. }
  731. void avcodec_default_free_buffers(AVCodecContext *s){
  732. int i, j;
  733. if(s->internal_buffer==NULL) return;
  734. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  735. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  736. for(j=0; j<4; j++){
  737. av_freep(&buf->base[j]);
  738. buf->data[j]= NULL;
  739. }
  740. }
  741. av_freep(&s->internal_buffer);
  742. s->internal_buffer_count=0;
  743. }
  744. char av_get_pict_type_char(int pict_type){
  745. switch(pict_type){
  746. case I_TYPE: return 'I';
  747. case P_TYPE: return 'P';
  748. case B_TYPE: return 'B';
  749. case S_TYPE: return 'S';
  750. case SI_TYPE:return 'i';
  751. case SP_TYPE:return 'p';
  752. default: return '?';
  753. }
  754. }
  755. int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
  756. AVRational a0={0,1}, a1={1,0};
  757. int sign= (nom<0) ^ (den<0);
  758. int64_t gcd= ff_gcd(ABS(nom), ABS(den));
  759. nom = ABS(nom)/gcd;
  760. den = ABS(den)/gcd;
  761. if(nom<=max && den<=max){
  762. a1= (AVRational){nom, den};
  763. den=0;
  764. }
  765. while(den){
  766. int64_t x = nom / den;
  767. int64_t next_den= nom - den*x;
  768. int64_t a2n= x*a1.num + a0.num;
  769. int64_t a2d= x*a1.den + a0.den;
  770. if(a2n > max || a2d > max) break;
  771. a0= a1;
  772. a1= (AVRational){a2n, a2d};
  773. nom= den;
  774. den= next_den;
  775. }
  776. assert(ff_gcd(a1.num, a1.den) == 1);
  777. *dst_nom = sign ? -a1.num : a1.num;
  778. *dst_den = a1.den;
  779. return den==0;
  780. }
  781. int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd){
  782. AVInteger ai;
  783. int64_t r=0;
  784. assert(c > 0);
  785. assert(b >=0);
  786. assert(rnd >=0 && rnd<=5 && rnd!=4);
  787. if(a<0 && a != INT64_MIN) return -av_rescale_rnd(-a, b, c, rnd ^ ((rnd>>1)&1));
  788. if(rnd==AV_ROUND_NEAR_INF) r= c/2;
  789. else if(rnd&1) r= c-1;
  790. if(b<=INT_MAX && c<=INT_MAX){
  791. if(a<=INT_MAX)
  792. return (a * b + r)/c;
  793. else
  794. return a/c*b + (a%c*b + r)/c;
  795. }
  796. ai= av_mul_i(av_int2i(a), av_int2i(b));
  797. ai= av_add_i(ai, av_int2i(r));
  798. return av_i2int(av_div_i(ai, av_int2i(c)));
  799. }
  800. int64_t av_rescale(int64_t a, int64_t b, int64_t c){
  801. return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
  802. }
  803. int64_t ff_gcd(int64_t a, int64_t b){
  804. if(b) return ff_gcd(b, a%b);
  805. else return a;
  806. }
  807. /* av_log API */
  808. static int av_log_level = AV_LOG_DEBUG;
  809. static void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
  810. {
  811. static int print_prefix=1;
  812. AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
  813. if(level>av_log_level)
  814. return;
  815. #undef fprintf
  816. if(print_prefix && avc) {
  817. fprintf(stderr, "[%s @ %p]", avc->item_name(ptr), avc);
  818. }
  819. #define fprintf please_use_av_log
  820. print_prefix= strstr(fmt, "\n") != NULL;
  821. vfprintf(stderr, fmt, vl);
  822. }
  823. static void (*av_log_callback)(void*, int, const char*, va_list) = av_log_default_callback;
  824. void av_log(void* avcl, int level, const char *fmt, ...)
  825. {
  826. va_list vl;
  827. va_start(vl, fmt);
  828. av_vlog(avcl, level, fmt, vl);
  829. va_end(vl);
  830. }
  831. void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
  832. {
  833. av_log_callback(avcl, level, fmt, vl);
  834. }
  835. int av_log_get_level(void)
  836. {
  837. return av_log_level;
  838. }
  839. void av_log_set_level(int level)
  840. {
  841. av_log_level = level;
  842. }
  843. void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
  844. {
  845. av_log_callback = callback;
  846. }
  847. #if !defined(HAVE_THREADS)
  848. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  849. return -1;
  850. }
  851. #endif