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.

1062 lines
29KB

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