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.

1032 lines
28KB

  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. const uint8_t ff_reverse[256]={
  48. 0x00,0x80,0x40,0xC0,0x20,0xA0,0x60,0xE0,0x10,0x90,0x50,0xD0,0x30,0xB0,0x70,0xF0,
  49. 0x08,0x88,0x48,0xC8,0x28,0xA8,0x68,0xE8,0x18,0x98,0x58,0xD8,0x38,0xB8,0x78,0xF8,
  50. 0x04,0x84,0x44,0xC4,0x24,0xA4,0x64,0xE4,0x14,0x94,0x54,0xD4,0x34,0xB4,0x74,0xF4,
  51. 0x0C,0x8C,0x4C,0xCC,0x2C,0xAC,0x6C,0xEC,0x1C,0x9C,0x5C,0xDC,0x3C,0xBC,0x7C,0xFC,
  52. 0x02,0x82,0x42,0xC2,0x22,0xA2,0x62,0xE2,0x12,0x92,0x52,0xD2,0x32,0xB2,0x72,0xF2,
  53. 0x0A,0x8A,0x4A,0xCA,0x2A,0xAA,0x6A,0xEA,0x1A,0x9A,0x5A,0xDA,0x3A,0xBA,0x7A,0xFA,
  54. 0x06,0x86,0x46,0xC6,0x26,0xA6,0x66,0xE6,0x16,0x96,0x56,0xD6,0x36,0xB6,0x76,0xF6,
  55. 0x0E,0x8E,0x4E,0xCE,0x2E,0xAE,0x6E,0xEE,0x1E,0x9E,0x5E,0xDE,0x3E,0xBE,0x7E,0xFE,
  56. 0x01,0x81,0x41,0xC1,0x21,0xA1,0x61,0xE1,0x11,0x91,0x51,0xD1,0x31,0xB1,0x71,0xF1,
  57. 0x09,0x89,0x49,0xC9,0x29,0xA9,0x69,0xE9,0x19,0x99,0x59,0xD9,0x39,0xB9,0x79,0xF9,
  58. 0x05,0x85,0x45,0xC5,0x25,0xA5,0x65,0xE5,0x15,0x95,0x55,0xD5,0x35,0xB5,0x75,0xF5,
  59. 0x0D,0x8D,0x4D,0xCD,0x2D,0xAD,0x6D,0xED,0x1D,0x9D,0x5D,0xDD,0x3D,0xBD,0x7D,0xFD,
  60. 0x03,0x83,0x43,0xC3,0x23,0xA3,0x63,0xE3,0x13,0x93,0x53,0xD3,0x33,0xB3,0x73,0xF3,
  61. 0x0B,0x8B,0x4B,0xCB,0x2B,0xAB,0x6B,0xEB,0x1B,0x9B,0x5B,0xDB,0x3B,0xBB,0x7B,0xFB,
  62. 0x07,0x87,0x47,0xC7,0x27,0xA7,0x67,0xE7,0x17,0x97,0x57,0xD7,0x37,0xB7,0x77,0xF7,
  63. 0x0F,0x8F,0x4F,0xCF,0x2F,0xAF,0x6F,0xEF,0x1F,0x9F,0x5F,0xDF,0x3F,0xBF,0x7F,0xFF,
  64. };
  65. void avcodec_default_free_buffers(AVCodecContext *s);
  66. void *av_mallocz(unsigned int size)
  67. {
  68. void *ptr;
  69. ptr = av_malloc(size);
  70. if (!ptr)
  71. return NULL;
  72. memset(ptr, 0, size);
  73. return ptr;
  74. }
  75. char *av_strdup(const char *s)
  76. {
  77. char *ptr;
  78. int len;
  79. len = strlen(s) + 1;
  80. ptr = av_malloc(len);
  81. if (!ptr)
  82. return NULL;
  83. memcpy(ptr, s, len);
  84. return ptr;
  85. }
  86. /**
  87. * realloc which does nothing if the block is large enough
  88. */
  89. void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
  90. {
  91. if(min_size < *size)
  92. return ptr;
  93. *size= FFMAX(17*min_size/16 + 32, min_size);
  94. return av_realloc(ptr, *size);
  95. }
  96. static unsigned int last_static = 0;
  97. static unsigned int allocated_static = 0;
  98. static void** array_static = NULL;
  99. /**
  100. * allocation of static arrays - do not use for normal allocation.
  101. */
  102. void *av_mallocz_static(unsigned int size)
  103. {
  104. void *ptr = av_mallocz(size);
  105. if(ptr){
  106. array_static =av_fast_realloc(array_static, &allocated_static, sizeof(void*)*(last_static+1));
  107. if(!array_static)
  108. return NULL;
  109. array_static[last_static++] = ptr;
  110. }
  111. return ptr;
  112. }
  113. /**
  114. * same as above, but does realloc
  115. */
  116. void *av_realloc_static(void *ptr, unsigned int size)
  117. {
  118. int i;
  119. if(!ptr)
  120. return av_mallocz_static(size);
  121. /* Look for the old ptr */
  122. for(i = 0; i < last_static; i++) {
  123. if(array_static[i] == ptr) {
  124. array_static[i] = av_realloc(array_static[i], size);
  125. return array_static[i];
  126. }
  127. }
  128. return NULL;
  129. }
  130. /**
  131. * free all static arrays and reset pointers to 0.
  132. */
  133. void av_free_static(void)
  134. {
  135. while(last_static){
  136. av_freep(&array_static[--last_static]);
  137. }
  138. av_freep(&array_static);
  139. }
  140. /**
  141. * Frees memory and sets the pointer to NULL.
  142. * @param arg pointer to the pointer which should be freed
  143. */
  144. void av_freep(void *arg)
  145. {
  146. void **ptr= (void**)arg;
  147. av_free(*ptr);
  148. *ptr = NULL;
  149. }
  150. /* encoder management */
  151. AVCodec *first_avcodec = NULL;
  152. void register_avcodec(AVCodec *format)
  153. {
  154. AVCodec **p;
  155. p = &first_avcodec;
  156. while (*p != NULL) p = &(*p)->next;
  157. *p = format;
  158. format->next = NULL;
  159. }
  160. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  161. s->coded_width = width;
  162. s->coded_height= height;
  163. s->width = -((-width )>>s->lowres);
  164. s->height= -((-height)>>s->lowres);
  165. }
  166. typedef struct InternalBuffer{
  167. int last_pic_num;
  168. uint8_t *base[4];
  169. uint8_t *data[4];
  170. int linesize[4];
  171. }InternalBuffer;
  172. #define INTERNAL_BUFFER_SIZE 32
  173. #define ALIGN(x, a) (((x)+(a)-1)&~((a)-1))
  174. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  175. int w_align= 1;
  176. int h_align= 1;
  177. switch(s->pix_fmt){
  178. case PIX_FMT_YUV420P:
  179. case PIX_FMT_YUV422:
  180. case PIX_FMT_UYVY422:
  181. case PIX_FMT_YUV422P:
  182. case PIX_FMT_YUV444P:
  183. case PIX_FMT_GRAY8:
  184. case PIX_FMT_YUVJ420P:
  185. case PIX_FMT_YUVJ422P:
  186. case PIX_FMT_YUVJ444P:
  187. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  188. h_align= 16;
  189. break;
  190. case PIX_FMT_YUV411P:
  191. case PIX_FMT_UYVY411:
  192. w_align=32;
  193. h_align=8;
  194. break;
  195. case PIX_FMT_YUV410P:
  196. if(s->codec_id == CODEC_ID_SVQ1){
  197. w_align=64;
  198. h_align=64;
  199. }
  200. case PIX_FMT_RGB555:
  201. if(s->codec_id == CODEC_ID_RPZA){
  202. w_align=4;
  203. h_align=4;
  204. }
  205. case PIX_FMT_PAL8:
  206. if(s->codec_id == CODEC_ID_SMC){
  207. w_align=4;
  208. h_align=4;
  209. }
  210. break;
  211. case PIX_FMT_BGR24:
  212. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  213. w_align=4;
  214. h_align=4;
  215. }
  216. break;
  217. default:
  218. w_align= 1;
  219. h_align= 1;
  220. break;
  221. }
  222. *width = ALIGN(*width , w_align);
  223. *height= ALIGN(*height, h_align);
  224. }
  225. int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
  226. if((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/4)
  227. return 0;
  228. av_log(av_log_ctx, AV_LOG_ERROR, "picture size invalid (%ux%u)\n", w, h);
  229. return -1;
  230. }
  231. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  232. int i;
  233. int w= s->width;
  234. int h= s->height;
  235. InternalBuffer *buf;
  236. int *picture_number;
  237. assert(pic->data[0]==NULL);
  238. assert(INTERNAL_BUFFER_SIZE > s->internal_buffer_count);
  239. if(avcodec_check_dimensions(s,w,h))
  240. return -1;
  241. if(s->internal_buffer==NULL){
  242. s->internal_buffer= av_mallocz(INTERNAL_BUFFER_SIZE*sizeof(InternalBuffer));
  243. }
  244. #if 0
  245. s->internal_buffer= av_fast_realloc(
  246. s->internal_buffer,
  247. &s->internal_buffer_size,
  248. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  249. );
  250. #endif
  251. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  252. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE-1]).last_pic_num; //FIXME ugly hack
  253. (*picture_number)++;
  254. if(buf->base[0]){
  255. pic->age= *picture_number - buf->last_pic_num;
  256. buf->last_pic_num= *picture_number;
  257. }else{
  258. int h_chroma_shift, v_chroma_shift;
  259. int pixel_size;
  260. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  261. switch(s->pix_fmt){
  262. case PIX_FMT_RGB555:
  263. case PIX_FMT_RGB565:
  264. case PIX_FMT_YUV422:
  265. case PIX_FMT_UYVY422:
  266. pixel_size=2;
  267. break;
  268. case PIX_FMT_RGB24:
  269. case PIX_FMT_BGR24:
  270. pixel_size=3;
  271. break;
  272. case PIX_FMT_RGBA32:
  273. pixel_size=4;
  274. break;
  275. default:
  276. pixel_size=1;
  277. }
  278. avcodec_align_dimensions(s, &w, &h);
  279. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  280. w+= EDGE_WIDTH*2;
  281. h+= EDGE_WIDTH*2;
  282. }
  283. buf->last_pic_num= -256*256*256*64;
  284. for(i=0; i<3; i++){
  285. const int h_shift= i==0 ? 0 : h_chroma_shift;
  286. const int v_shift= i==0 ? 0 : v_chroma_shift;
  287. //FIXME next ensures that linesize= 2^x uvlinesize, thats needed because some MC code assumes it
  288. buf->linesize[i]= ALIGN(pixel_size*w>>h_shift, STRIDE_ALIGN<<(h_chroma_shift-h_shift));
  289. buf->base[i]= av_malloc((buf->linesize[i]*h>>v_shift)+16); //FIXME 16
  290. if(buf->base[i]==NULL) return -1;
  291. memset(buf->base[i], 128, buf->linesize[i]*h>>v_shift);
  292. if(s->flags&CODEC_FLAG_EMU_EDGE)
  293. buf->data[i] = buf->base[i];
  294. else
  295. buf->data[i] = buf->base[i] + ALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), STRIDE_ALIGN);
  296. }
  297. pic->age= 256*256*256*64;
  298. }
  299. pic->type= FF_BUFFER_TYPE_INTERNAL;
  300. for(i=0; i<4; i++){
  301. pic->base[i]= buf->base[i];
  302. pic->data[i]= buf->data[i];
  303. pic->linesize[i]= buf->linesize[i];
  304. }
  305. s->internal_buffer_count++;
  306. return 0;
  307. }
  308. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  309. int i;
  310. InternalBuffer *buf, *last, temp;
  311. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  312. assert(s->internal_buffer_count);
  313. buf = NULL; /* avoids warning */
  314. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  315. buf= &((InternalBuffer*)s->internal_buffer)[i];
  316. if(buf->data[0] == pic->data[0])
  317. break;
  318. }
  319. assert(i < s->internal_buffer_count);
  320. s->internal_buffer_count--;
  321. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  322. temp= *buf;
  323. *buf= *last;
  324. *last= temp;
  325. for(i=0; i<3; i++){
  326. pic->data[i]=NULL;
  327. // pic->base[i]=NULL;
  328. }
  329. //printf("R%X\n", pic->opaque);
  330. }
  331. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  332. AVFrame temp_pic;
  333. int i;
  334. /* If no picture return a new buffer */
  335. if(pic->data[0] == NULL) {
  336. /* We will copy from buffer, so must be readable */
  337. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  338. return s->get_buffer(s, pic);
  339. }
  340. /* If internal buffer type return the same buffer */
  341. if(pic->type == FF_BUFFER_TYPE_INTERNAL)
  342. return 0;
  343. /*
  344. * Not internal type and reget_buffer not overridden, emulate cr buffer
  345. */
  346. temp_pic = *pic;
  347. for(i = 0; i < 4; i++)
  348. pic->data[i] = pic->base[i] = NULL;
  349. pic->opaque = NULL;
  350. /* Allocate new frame */
  351. if (s->get_buffer(s, pic))
  352. return -1;
  353. /* Copy image data from old buffer to new buffer */
  354. img_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  355. s->height);
  356. s->release_buffer(s, &temp_pic); // Release old frame
  357. return 0;
  358. }
  359. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){
  360. int i;
  361. for(i=0; i<count; i++){
  362. int r= func(c, arg[i]);
  363. if(ret) ret[i]= r;
  364. }
  365. return 0;
  366. }
  367. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat * fmt){
  368. return fmt[0];
  369. }
  370. static const char* context_to_name(void* ptr) {
  371. AVCodecContext *avc= ptr;
  372. if(avc && avc->codec && avc->codec->name)
  373. return avc->codec->name;
  374. else
  375. return "NULL";
  376. }
  377. static AVClass av_codec_context_class = { "AVCodecContext", context_to_name };
  378. void avcodec_get_context_defaults(AVCodecContext *s){
  379. memset(s, 0, sizeof(AVCodecContext));
  380. s->av_class= &av_codec_context_class;
  381. s->bit_rate= 800*1000;
  382. s->bit_rate_tolerance= s->bit_rate*10;
  383. s->qmin= 2;
  384. s->qmax= 31;
  385. s->mb_lmin= FF_QP2LAMBDA * 2;
  386. s->mb_lmax= FF_QP2LAMBDA * 31;
  387. s->rc_eq= "tex^qComp";
  388. s->qcompress= 0.5;
  389. s->max_qdiff= 3;
  390. s->b_quant_factor=1.25;
  391. s->b_quant_offset=1.25;
  392. s->i_quant_factor=-0.8;
  393. s->i_quant_offset=0.0;
  394. s->error_concealment= 3;
  395. s->error_resilience= 1;
  396. s->workaround_bugs= FF_BUG_AUTODETECT;
  397. s->time_base= (AVRational){0,1};
  398. s->gop_size= 50;
  399. s->me_method= ME_EPZS;
  400. s->get_buffer= avcodec_default_get_buffer;
  401. s->release_buffer= avcodec_default_release_buffer;
  402. s->get_format= avcodec_default_get_format;
  403. s->execute= avcodec_default_execute;
  404. s->thread_count=1;
  405. s->me_subpel_quality=8;
  406. s->lmin= FF_QP2LAMBDA * s->qmin;
  407. s->lmax= FF_QP2LAMBDA * s->qmax;
  408. s->sample_aspect_ratio= (AVRational){0,1};
  409. s->ildct_cmp= FF_CMP_VSAD;
  410. s->profile= FF_PROFILE_UNKNOWN;
  411. s->level= FF_LEVEL_UNKNOWN;
  412. s->me_penalty_compensation= 256;
  413. s->pix_fmt= PIX_FMT_NONE;
  414. s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS;
  415. s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS;
  416. s->palctrl = NULL;
  417. s->reget_buffer= avcodec_default_reget_buffer;
  418. }
  419. /**
  420. * allocates a AVCodecContext and set it to defaults.
  421. * this can be deallocated by simply calling free()
  422. */
  423. AVCodecContext *avcodec_alloc_context(void){
  424. AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
  425. if(avctx==NULL) return NULL;
  426. avcodec_get_context_defaults(avctx);
  427. return avctx;
  428. }
  429. void avcodec_get_frame_defaults(AVFrame *pic){
  430. memset(pic, 0, sizeof(AVFrame));
  431. pic->pts= AV_NOPTS_VALUE;
  432. pic->key_frame= 1;
  433. }
  434. /**
  435. * allocates a AVPFrame and set it to defaults.
  436. * this can be deallocated by simply calling free()
  437. */
  438. AVFrame *avcodec_alloc_frame(void){
  439. AVFrame *pic= av_malloc(sizeof(AVFrame));
  440. if(pic==NULL) return NULL;
  441. avcodec_get_frame_defaults(pic);
  442. return pic;
  443. }
  444. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  445. {
  446. int ret;
  447. if(avctx->codec)
  448. return -1;
  449. avctx->codec = codec;
  450. avctx->codec_id = codec->id;
  451. avctx->frame_number = 0;
  452. if (codec->priv_data_size > 0) {
  453. avctx->priv_data = av_mallocz(codec->priv_data_size);
  454. if (!avctx->priv_data)
  455. return -ENOMEM;
  456. } else {
  457. avctx->priv_data = NULL;
  458. }
  459. if(avctx->coded_width && avctx->coded_height)
  460. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  461. else if(avctx->width && avctx->height)
  462. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  463. if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height)){
  464. av_freep(&avctx->priv_data);
  465. return -1;
  466. }
  467. ret = avctx->codec->init(avctx);
  468. if (ret < 0) {
  469. av_freep(&avctx->priv_data);
  470. return ret;
  471. }
  472. return 0;
  473. }
  474. int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  475. const short *samples)
  476. {
  477. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  478. av_log(avctx, AV_LOG_ERROR, "buffer smaller then minimum size\n");
  479. return -1;
  480. }
  481. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  482. int ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  483. avctx->frame_number++;
  484. return ret;
  485. }else
  486. return 0;
  487. }
  488. int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  489. const AVFrame *pict)
  490. {
  491. if(buf_size < FF_MIN_BUFFER_SIZE){
  492. av_log(avctx, AV_LOG_ERROR, "buffer smaller then minimum size\n");
  493. return -1;
  494. }
  495. if(avcodec_check_dimensions(avctx,avctx->width,avctx->height))
  496. return -1;
  497. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  498. int ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  499. avctx->frame_number++;
  500. emms_c(); //needed to avoid a emms_c() call before every return;
  501. return ret;
  502. }else
  503. return 0;
  504. }
  505. /**
  506. * decode a frame.
  507. * @param buf bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE larger then the actual read bytes
  508. * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  509. * @param buf_size the size of the buffer in bytes
  510. * @param got_picture_ptr zero if no frame could be decompressed, Otherwise, it is non zero
  511. * @return -1 if error, otherwise return the number of
  512. * bytes used.
  513. */
  514. int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  515. int *got_picture_ptr,
  516. uint8_t *buf, int buf_size)
  517. {
  518. int ret;
  519. *got_picture_ptr= 0;
  520. if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height))
  521. return -1;
  522. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){
  523. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  524. buf, buf_size);
  525. emms_c(); //needed to avoid a emms_c() call before every return;
  526. if (*got_picture_ptr)
  527. avctx->frame_number++;
  528. }else
  529. ret= 0;
  530. return ret;
  531. }
  532. /* decode an audio frame. return -1 if error, otherwise return the
  533. *number of bytes used. If no frame could be decompressed,
  534. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  535. *size in BYTES. */
  536. int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,
  537. int *frame_size_ptr,
  538. uint8_t *buf, int buf_size)
  539. {
  540. int ret;
  541. *frame_size_ptr= 0;
  542. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  543. buf, buf_size);
  544. avctx->frame_number++;
  545. return ret;
  546. }
  547. int avcodec_close(AVCodecContext *avctx)
  548. {
  549. if (avctx->codec->close)
  550. avctx->codec->close(avctx);
  551. avcodec_default_free_buffers(avctx);
  552. av_freep(&avctx->priv_data);
  553. avctx->codec = NULL;
  554. return 0;
  555. }
  556. AVCodec *avcodec_find_encoder(enum CodecID id)
  557. {
  558. AVCodec *p;
  559. p = first_avcodec;
  560. while (p) {
  561. if (p->encode != NULL && p->id == id)
  562. return p;
  563. p = p->next;
  564. }
  565. return NULL;
  566. }
  567. AVCodec *avcodec_find_encoder_by_name(const char *name)
  568. {
  569. AVCodec *p;
  570. p = first_avcodec;
  571. while (p) {
  572. if (p->encode != NULL && strcmp(name,p->name) == 0)
  573. return p;
  574. p = p->next;
  575. }
  576. return NULL;
  577. }
  578. AVCodec *avcodec_find_decoder(enum CodecID id)
  579. {
  580. AVCodec *p;
  581. p = first_avcodec;
  582. while (p) {
  583. if (p->decode != NULL && p->id == id)
  584. return p;
  585. p = p->next;
  586. }
  587. return NULL;
  588. }
  589. AVCodec *avcodec_find_decoder_by_name(const char *name)
  590. {
  591. AVCodec *p;
  592. p = first_avcodec;
  593. while (p) {
  594. if (p->decode != NULL && strcmp(name,p->name) == 0)
  595. return p;
  596. p = p->next;
  597. }
  598. return NULL;
  599. }
  600. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  601. {
  602. const char *codec_name;
  603. AVCodec *p;
  604. char buf1[32];
  605. char channels_str[100];
  606. int bitrate;
  607. if (encode)
  608. p = avcodec_find_encoder(enc->codec_id);
  609. else
  610. p = avcodec_find_decoder(enc->codec_id);
  611. if (p) {
  612. codec_name = p->name;
  613. if (!encode && enc->codec_id == CODEC_ID_MP3) {
  614. if (enc->sub_id == 2)
  615. codec_name = "mp2";
  616. else if (enc->sub_id == 1)
  617. codec_name = "mp1";
  618. }
  619. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  620. /* fake mpeg2 transport stream codec (currently not
  621. registered) */
  622. codec_name = "mpeg2ts";
  623. } else if (enc->codec_name[0] != '\0') {
  624. codec_name = enc->codec_name;
  625. } else {
  626. /* output avi tags */
  627. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  628. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  629. enc->codec_tag & 0xff,
  630. (enc->codec_tag >> 8) & 0xff,
  631. (enc->codec_tag >> 16) & 0xff,
  632. (enc->codec_tag >> 24) & 0xff);
  633. } else {
  634. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  635. }
  636. codec_name = buf1;
  637. }
  638. switch(enc->codec_type) {
  639. case CODEC_TYPE_VIDEO:
  640. snprintf(buf, buf_size,
  641. "Video: %s%s",
  642. codec_name, enc->mb_decision ? " (hq)" : "");
  643. if (enc->pix_fmt != PIX_FMT_NONE) {
  644. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  645. ", %s",
  646. avcodec_get_pix_fmt_name(enc->pix_fmt));
  647. }
  648. if (enc->width) {
  649. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  650. ", %dx%d, %0.2f fps",
  651. enc->width, enc->height,
  652. 1/av_q2d(enc->time_base));
  653. }
  654. if (encode) {
  655. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  656. ", q=%d-%d", enc->qmin, enc->qmax);
  657. }
  658. bitrate = enc->bit_rate;
  659. break;
  660. case CODEC_TYPE_AUDIO:
  661. snprintf(buf, buf_size,
  662. "Audio: %s",
  663. codec_name);
  664. switch (enc->channels) {
  665. case 1:
  666. strcpy(channels_str, "mono");
  667. break;
  668. case 2:
  669. strcpy(channels_str, "stereo");
  670. break;
  671. case 6:
  672. strcpy(channels_str, "5:1");
  673. break;
  674. default:
  675. snprintf(channels_str, sizeof(channels_str), "%d channels", enc->channels);
  676. break;
  677. }
  678. if (enc->sample_rate) {
  679. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  680. ", %d Hz, %s",
  681. enc->sample_rate,
  682. channels_str);
  683. }
  684. /* for PCM codecs, compute bitrate directly */
  685. switch(enc->codec_id) {
  686. case CODEC_ID_PCM_S16LE:
  687. case CODEC_ID_PCM_S16BE:
  688. case CODEC_ID_PCM_U16LE:
  689. case CODEC_ID_PCM_U16BE:
  690. bitrate = enc->sample_rate * enc->channels * 16;
  691. break;
  692. case CODEC_ID_PCM_S8:
  693. case CODEC_ID_PCM_U8:
  694. case CODEC_ID_PCM_ALAW:
  695. case CODEC_ID_PCM_MULAW:
  696. bitrate = enc->sample_rate * enc->channels * 8;
  697. break;
  698. default:
  699. bitrate = enc->bit_rate;
  700. break;
  701. }
  702. break;
  703. case CODEC_TYPE_DATA:
  704. snprintf(buf, buf_size, "Data: %s", codec_name);
  705. bitrate = enc->bit_rate;
  706. break;
  707. default:
  708. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  709. return;
  710. }
  711. if (encode) {
  712. if (enc->flags & CODEC_FLAG_PASS1)
  713. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  714. ", pass 1");
  715. if (enc->flags & CODEC_FLAG_PASS2)
  716. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  717. ", pass 2");
  718. }
  719. if (bitrate != 0) {
  720. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  721. ", %d kb/s", bitrate / 1000);
  722. }
  723. }
  724. unsigned avcodec_version( void )
  725. {
  726. return LIBAVCODEC_VERSION_INT;
  727. }
  728. unsigned avcodec_build( void )
  729. {
  730. return LIBAVCODEC_BUILD;
  731. }
  732. /* must be called before any other functions */
  733. void avcodec_init(void)
  734. {
  735. static int inited = 0;
  736. if (inited != 0)
  737. return;
  738. inited = 1;
  739. dsputil_static_init();
  740. }
  741. /**
  742. * Flush buffers, should be called when seeking or when swicthing to a different stream.
  743. */
  744. void avcodec_flush_buffers(AVCodecContext *avctx)
  745. {
  746. if(avctx->codec->flush)
  747. avctx->codec->flush(avctx);
  748. }
  749. void avcodec_default_free_buffers(AVCodecContext *s){
  750. int i, j;
  751. if(s->internal_buffer==NULL) return;
  752. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  753. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  754. for(j=0; j<4; j++){
  755. av_freep(&buf->base[j]);
  756. buf->data[j]= NULL;
  757. }
  758. }
  759. av_freep(&s->internal_buffer);
  760. s->internal_buffer_count=0;
  761. }
  762. char av_get_pict_type_char(int pict_type){
  763. switch(pict_type){
  764. case I_TYPE: return 'I';
  765. case P_TYPE: return 'P';
  766. case B_TYPE: return 'B';
  767. case S_TYPE: return 'S';
  768. case SI_TYPE:return 'i';
  769. case SP_TYPE:return 'p';
  770. default: return '?';
  771. }
  772. }
  773. int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
  774. AVRational a0={0,1}, a1={1,0};
  775. int sign= (nom<0) ^ (den<0);
  776. int64_t gcd= ff_gcd(ABS(nom), ABS(den));
  777. nom = ABS(nom)/gcd;
  778. den = ABS(den)/gcd;
  779. if(nom<=max && den<=max){
  780. a1= (AVRational){nom, den};
  781. den=0;
  782. }
  783. while(den){
  784. int64_t x = nom / den;
  785. int64_t next_den= nom - den*x;
  786. int64_t a2n= x*a1.num + a0.num;
  787. int64_t a2d= x*a1.den + a0.den;
  788. if(a2n > max || a2d > max) break;
  789. a0= a1;
  790. a1= (AVRational){a2n, a2d};
  791. nom= den;
  792. den= next_den;
  793. }
  794. assert(ff_gcd(a1.num, a1.den) == 1);
  795. *dst_nom = sign ? -a1.num : a1.num;
  796. *dst_den = a1.den;
  797. return den==0;
  798. }
  799. int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd){
  800. AVInteger ai;
  801. int64_t r=0;
  802. assert(c > 0);
  803. assert(b >=0);
  804. assert(rnd >=0 && rnd<=5 && rnd!=4);
  805. if(a<0 && a != INT64_MIN) return -av_rescale_rnd(-a, b, c, rnd ^ ((rnd>>1)&1));
  806. if(rnd==AV_ROUND_NEAR_INF) r= c/2;
  807. else if(rnd&1) r= c-1;
  808. if(b<=INT_MAX && c<=INT_MAX){
  809. if(a<=INT_MAX)
  810. return (a * b + r)/c;
  811. else
  812. return a/c*b + (a%c*b + r)/c;
  813. }
  814. ai= av_mul_i(av_int2i(a), av_int2i(b));
  815. ai= av_add_i(ai, av_int2i(r));
  816. return av_i2int(av_div_i(ai, av_int2i(c)));
  817. }
  818. int64_t av_rescale(int64_t a, int64_t b, int64_t c){
  819. return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
  820. }
  821. int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq){
  822. int64_t b= bq.num * (int64_t)cq.den;
  823. int64_t c= cq.num * (int64_t)bq.den;
  824. return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
  825. }
  826. int64_t ff_gcd(int64_t a, int64_t b){
  827. if(b) return ff_gcd(b, a%b);
  828. else return a;
  829. }
  830. /* av_log API */
  831. static int av_log_level = AV_LOG_INFO;
  832. static void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
  833. {
  834. static int print_prefix=1;
  835. AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
  836. if(level>av_log_level)
  837. return;
  838. #undef fprintf
  839. if(print_prefix && avc) {
  840. fprintf(stderr, "[%s @ %p]", avc->item_name(ptr), avc);
  841. }
  842. #define fprintf please_use_av_log
  843. print_prefix= strstr(fmt, "\n") != NULL;
  844. vfprintf(stderr, fmt, vl);
  845. }
  846. static void (*av_log_callback)(void*, int, const char*, va_list) = av_log_default_callback;
  847. void av_log(void* avcl, int level, const char *fmt, ...)
  848. {
  849. va_list vl;
  850. va_start(vl, fmt);
  851. av_vlog(avcl, level, fmt, vl);
  852. va_end(vl);
  853. }
  854. void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
  855. {
  856. av_log_callback(avcl, level, fmt, vl);
  857. }
  858. int av_log_get_level(void)
  859. {
  860. return av_log_level;
  861. }
  862. void av_log_set_level(int level)
  863. {
  864. av_log_level = level;
  865. }
  866. void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
  867. {
  868. av_log_callback = callback;
  869. }
  870. #if !defined(HAVE_THREADS)
  871. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  872. return -1;
  873. }
  874. #endif
  875. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  876. {
  877. unsigned int n = 0;
  878. while(v >= 0xff) {
  879. *s++ = 0xff;
  880. v -= 0xff;
  881. n++;
  882. }
  883. *s = v;
  884. n++;
  885. return n;
  886. }