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.

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