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.

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