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.

1112 lines
31KB

  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. static int volatile entangled_thread_counter=0;
  66. void avcodec_default_free_buffers(AVCodecContext *s);
  67. void *av_mallocz(unsigned int size)
  68. {
  69. void *ptr;
  70. ptr = av_malloc(size);
  71. if (!ptr)
  72. return NULL;
  73. memset(ptr, 0, size);
  74. return ptr;
  75. }
  76. char *av_strdup(const char *s)
  77. {
  78. char *ptr;
  79. int len;
  80. len = strlen(s) + 1;
  81. ptr = av_malloc(len);
  82. if (!ptr)
  83. return NULL;
  84. memcpy(ptr, s, len);
  85. return ptr;
  86. }
  87. /**
  88. * realloc which does nothing if the block is large enough
  89. */
  90. void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
  91. {
  92. if(min_size < *size)
  93. return ptr;
  94. *size= FFMAX(17*min_size/16 + 32, min_size);
  95. return av_realloc(ptr, *size);
  96. }
  97. static unsigned int last_static = 0;
  98. static unsigned int allocated_static = 0;
  99. static void** array_static = NULL;
  100. /**
  101. * allocation of static arrays - do not use for normal allocation.
  102. */
  103. void *av_mallocz_static(unsigned int size)
  104. {
  105. void *ptr = av_mallocz(size);
  106. if(ptr){
  107. array_static =av_fast_realloc(array_static, &allocated_static, sizeof(void*)*(last_static+1));
  108. if(!array_static)
  109. return NULL;
  110. array_static[last_static++] = ptr;
  111. }
  112. return ptr;
  113. }
  114. /**
  115. * same as above, but does realloc
  116. */
  117. void *av_realloc_static(void *ptr, unsigned int size)
  118. {
  119. int i;
  120. if(!ptr)
  121. return av_mallocz_static(size);
  122. /* Look for the old ptr */
  123. for(i = 0; i < last_static; i++) {
  124. if(array_static[i] == ptr) {
  125. array_static[i] = av_realloc(array_static[i], size);
  126. return array_static[i];
  127. }
  128. }
  129. return NULL;
  130. }
  131. /**
  132. * free all static arrays and reset pointers to 0.
  133. */
  134. void av_free_static(void)
  135. {
  136. while(last_static){
  137. av_freep(&array_static[--last_static]);
  138. }
  139. av_freep(&array_static);
  140. }
  141. /**
  142. * Frees memory and sets the pointer to NULL.
  143. * @param arg pointer to the pointer which should be freed
  144. */
  145. void av_freep(void *arg)
  146. {
  147. void **ptr= (void**)arg;
  148. av_free(*ptr);
  149. *ptr = NULL;
  150. }
  151. /* encoder management */
  152. AVCodec *first_avcodec = NULL;
  153. void register_avcodec(AVCodec *format)
  154. {
  155. AVCodec **p;
  156. p = &first_avcodec;
  157. while (*p != NULL) p = &(*p)->next;
  158. *p = format;
  159. format->next = NULL;
  160. }
  161. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  162. s->coded_width = width;
  163. s->coded_height= height;
  164. s->width = -((-width )>>s->lowres);
  165. s->height= -((-height)>>s->lowres);
  166. }
  167. typedef struct InternalBuffer{
  168. int last_pic_num;
  169. uint8_t *base[4];
  170. uint8_t *data[4];
  171. int linesize[4];
  172. }InternalBuffer;
  173. #define INTERNAL_BUFFER_SIZE 32
  174. #define ALIGN(x, a) (((x)+(a)-1)&~((a)-1))
  175. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  176. int w_align= 1;
  177. int h_align= 1;
  178. switch(s->pix_fmt){
  179. case PIX_FMT_YUV420P:
  180. case PIX_FMT_YUV422:
  181. case PIX_FMT_UYVY422:
  182. case PIX_FMT_YUV422P:
  183. case PIX_FMT_YUV444P:
  184. case PIX_FMT_GRAY8:
  185. case PIX_FMT_YUVJ420P:
  186. case PIX_FMT_YUVJ422P:
  187. case PIX_FMT_YUVJ444P:
  188. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  189. h_align= 16;
  190. break;
  191. case PIX_FMT_YUV411P:
  192. case PIX_FMT_UYVY411:
  193. w_align=32;
  194. h_align=8;
  195. break;
  196. case PIX_FMT_YUV410P:
  197. if(s->codec_id == CODEC_ID_SVQ1){
  198. w_align=64;
  199. h_align=64;
  200. }
  201. case PIX_FMT_RGB555:
  202. if(s->codec_id == CODEC_ID_RPZA){
  203. w_align=4;
  204. h_align=4;
  205. }
  206. case PIX_FMT_PAL8:
  207. if(s->codec_id == CODEC_ID_SMC){
  208. w_align=4;
  209. h_align=4;
  210. }
  211. break;
  212. case PIX_FMT_BGR24:
  213. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  214. w_align=4;
  215. h_align=4;
  216. }
  217. break;
  218. default:
  219. w_align= 1;
  220. h_align= 1;
  221. break;
  222. }
  223. *width = ALIGN(*width , w_align);
  224. *height= ALIGN(*height, h_align);
  225. }
  226. int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
  227. if((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/4)
  228. return 0;
  229. av_log(av_log_ctx, AV_LOG_ERROR, "picture size invalid (%ux%u)\n", w, h);
  230. return -1;
  231. }
  232. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  233. int i;
  234. int w= s->width;
  235. int h= s->height;
  236. InternalBuffer *buf;
  237. int *picture_number;
  238. assert(pic->data[0]==NULL);
  239. assert(INTERNAL_BUFFER_SIZE > s->internal_buffer_count);
  240. if(avcodec_check_dimensions(s,w,h))
  241. return -1;
  242. if(s->internal_buffer==NULL){
  243. s->internal_buffer= av_mallocz(INTERNAL_BUFFER_SIZE*sizeof(InternalBuffer));
  244. }
  245. #if 0
  246. s->internal_buffer= av_fast_realloc(
  247. s->internal_buffer,
  248. &s->internal_buffer_size,
  249. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  250. );
  251. #endif
  252. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  253. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE-1]).last_pic_num; //FIXME ugly hack
  254. (*picture_number)++;
  255. if(buf->base[0]){
  256. pic->age= *picture_number - buf->last_pic_num;
  257. buf->last_pic_num= *picture_number;
  258. }else{
  259. int h_chroma_shift, v_chroma_shift;
  260. int pixel_size;
  261. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  262. switch(s->pix_fmt){
  263. case PIX_FMT_RGB555:
  264. case PIX_FMT_RGB565:
  265. case PIX_FMT_YUV422:
  266. case PIX_FMT_UYVY422:
  267. pixel_size=2;
  268. break;
  269. case PIX_FMT_RGB24:
  270. case PIX_FMT_BGR24:
  271. pixel_size=3;
  272. break;
  273. case PIX_FMT_RGBA32:
  274. pixel_size=4;
  275. break;
  276. default:
  277. pixel_size=1;
  278. }
  279. avcodec_align_dimensions(s, &w, &h);
  280. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  281. w+= EDGE_WIDTH*2;
  282. h+= EDGE_WIDTH*2;
  283. }
  284. buf->last_pic_num= -256*256*256*64;
  285. for(i=0; i<3; i++){
  286. const int h_shift= i==0 ? 0 : h_chroma_shift;
  287. const int v_shift= i==0 ? 0 : v_chroma_shift;
  288. //FIXME next ensures that linesize= 2^x uvlinesize, thats needed because some MC code assumes it
  289. buf->linesize[i]= ALIGN(pixel_size*w>>h_shift, STRIDE_ALIGN<<(h_chroma_shift-h_shift));
  290. buf->base[i]= av_malloc((buf->linesize[i]*h>>v_shift)+16); //FIXME 16
  291. if(buf->base[i]==NULL) return -1;
  292. memset(buf->base[i], 128, buf->linesize[i]*h>>v_shift);
  293. if(s->flags&CODEC_FLAG_EMU_EDGE)
  294. buf->data[i] = buf->base[i];
  295. else
  296. buf->data[i] = buf->base[i] + ALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), STRIDE_ALIGN);
  297. }
  298. pic->age= 256*256*256*64;
  299. }
  300. pic->type= FF_BUFFER_TYPE_INTERNAL;
  301. for(i=0; i<4; i++){
  302. pic->base[i]= buf->base[i];
  303. pic->data[i]= buf->data[i];
  304. pic->linesize[i]= buf->linesize[i];
  305. }
  306. s->internal_buffer_count++;
  307. return 0;
  308. }
  309. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  310. int i;
  311. InternalBuffer *buf, *last, temp;
  312. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  313. assert(s->internal_buffer_count);
  314. buf = NULL; /* avoids warning */
  315. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  316. buf= &((InternalBuffer*)s->internal_buffer)[i];
  317. if(buf->data[0] == pic->data[0])
  318. break;
  319. }
  320. assert(i < s->internal_buffer_count);
  321. s->internal_buffer_count--;
  322. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  323. temp= *buf;
  324. *buf= *last;
  325. *last= temp;
  326. for(i=0; i<3; i++){
  327. pic->data[i]=NULL;
  328. // pic->base[i]=NULL;
  329. }
  330. //printf("R%X\n", pic->opaque);
  331. }
  332. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  333. AVFrame temp_pic;
  334. int i;
  335. /* If no picture return a new buffer */
  336. if(pic->data[0] == NULL) {
  337. /* We will copy from buffer, so must be readable */
  338. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  339. return s->get_buffer(s, pic);
  340. }
  341. /* If internal buffer type return the same buffer */
  342. if(pic->type == FF_BUFFER_TYPE_INTERNAL)
  343. return 0;
  344. /*
  345. * Not internal type and reget_buffer not overridden, emulate cr buffer
  346. */
  347. temp_pic = *pic;
  348. for(i = 0; i < 4; i++)
  349. pic->data[i] = pic->base[i] = NULL;
  350. pic->opaque = NULL;
  351. /* Allocate new frame */
  352. if (s->get_buffer(s, pic))
  353. return -1;
  354. /* Copy image data from old buffer to new buffer */
  355. img_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  356. s->height);
  357. s->release_buffer(s, &temp_pic); // Release old frame
  358. return 0;
  359. }
  360. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){
  361. int i;
  362. for(i=0; i<count; i++){
  363. int r= func(c, arg[i]);
  364. if(ret) ret[i]= r;
  365. }
  366. return 0;
  367. }
  368. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat * fmt){
  369. return fmt[0];
  370. }
  371. static const char* context_to_name(void* ptr) {
  372. AVCodecContext *avc= ptr;
  373. if(avc && avc->codec && avc->codec->name)
  374. return avc->codec->name;
  375. else
  376. return "NULL";
  377. }
  378. static AVClass av_codec_context_class = { "AVCodecContext", context_to_name };
  379. void avcodec_get_context_defaults(AVCodecContext *s){
  380. memset(s, 0, sizeof(AVCodecContext));
  381. s->av_class= &av_codec_context_class;
  382. s->bit_rate= 800*1000;
  383. s->bit_rate_tolerance= s->bit_rate*10;
  384. s->qmin= 2;
  385. s->qmax= 31;
  386. s->mb_lmin= FF_QP2LAMBDA * 2;
  387. s->mb_lmax= FF_QP2LAMBDA * 31;
  388. s->rc_eq= "tex^qComp";
  389. s->qcompress= 0.5;
  390. s->max_qdiff= 3;
  391. s->b_quant_factor=1.25;
  392. s->b_quant_offset=1.25;
  393. s->i_quant_factor=-0.8;
  394. s->i_quant_offset=0.0;
  395. s->error_concealment= 3;
  396. s->error_resilience= 1;
  397. s->workaround_bugs= FF_BUG_AUTODETECT;
  398. s->time_base= (AVRational){0,1};
  399. s->gop_size= 50;
  400. s->me_method= ME_EPZS;
  401. s->get_buffer= avcodec_default_get_buffer;
  402. s->release_buffer= avcodec_default_release_buffer;
  403. s->get_format= avcodec_default_get_format;
  404. s->execute= avcodec_default_execute;
  405. s->thread_count=1;
  406. s->me_subpel_quality=8;
  407. s->lmin= FF_QP2LAMBDA * s->qmin;
  408. s->lmax= FF_QP2LAMBDA * s->qmax;
  409. s->sample_aspect_ratio= (AVRational){0,1};
  410. s->ildct_cmp= FF_CMP_VSAD;
  411. s->profile= FF_PROFILE_UNKNOWN;
  412. s->level= FF_LEVEL_UNKNOWN;
  413. s->me_penalty_compensation= 256;
  414. s->pix_fmt= PIX_FMT_NONE;
  415. s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS;
  416. s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS;
  417. s->palctrl = NULL;
  418. s->reget_buffer= avcodec_default_reget_buffer;
  419. }
  420. /**
  421. * allocates a AVCodecContext and set it to defaults.
  422. * this can be deallocated by simply calling free()
  423. */
  424. AVCodecContext *avcodec_alloc_context(void){
  425. AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
  426. if(avctx==NULL) return NULL;
  427. avcodec_get_context_defaults(avctx);
  428. return avctx;
  429. }
  430. void avcodec_get_frame_defaults(AVFrame *pic){
  431. memset(pic, 0, sizeof(AVFrame));
  432. pic->pts= AV_NOPTS_VALUE;
  433. pic->key_frame= 1;
  434. }
  435. /**
  436. * allocates a AVPFrame and set it to defaults.
  437. * this can be deallocated by simply calling free()
  438. */
  439. AVFrame *avcodec_alloc_frame(void){
  440. AVFrame *pic= av_malloc(sizeof(AVFrame));
  441. if(pic==NULL) return NULL;
  442. avcodec_get_frame_defaults(pic);
  443. return pic;
  444. }
  445. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  446. {
  447. int ret= -1;
  448. entangled_thread_counter++;
  449. if(entangled_thread_counter != 1){
  450. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  451. goto end;
  452. }
  453. if(avctx->codec)
  454. goto end;
  455. avctx->codec = codec;
  456. avctx->codec_id = codec->id;
  457. avctx->frame_number = 0;
  458. if (codec->priv_data_size > 0) {
  459. avctx->priv_data = av_mallocz(codec->priv_data_size);
  460. if (!avctx->priv_data)
  461. goto end;
  462. } else {
  463. avctx->priv_data = NULL;
  464. }
  465. if(avctx->coded_width && avctx->coded_height)
  466. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  467. else if(avctx->width && avctx->height)
  468. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  469. if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height)){
  470. av_freep(&avctx->priv_data);
  471. goto end;
  472. }
  473. ret = avctx->codec->init(avctx);
  474. if (ret < 0) {
  475. av_freep(&avctx->priv_data);
  476. goto end;
  477. }
  478. ret=0;
  479. end:
  480. entangled_thread_counter--;
  481. return ret;
  482. }
  483. int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  484. const short *samples)
  485. {
  486. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  487. av_log(avctx, AV_LOG_ERROR, "buffer smaller then minimum size\n");
  488. return -1;
  489. }
  490. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  491. int ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  492. avctx->frame_number++;
  493. return ret;
  494. }else
  495. return 0;
  496. }
  497. int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  498. const AVFrame *pict)
  499. {
  500. if(buf_size < FF_MIN_BUFFER_SIZE){
  501. av_log(avctx, AV_LOG_ERROR, "buffer smaller then minimum size\n");
  502. return -1;
  503. }
  504. if(avcodec_check_dimensions(avctx,avctx->width,avctx->height))
  505. return -1;
  506. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  507. int ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  508. avctx->frame_number++;
  509. emms_c(); //needed to avoid an emms_c() call before every return;
  510. return ret;
  511. }else
  512. return 0;
  513. }
  514. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  515. const AVSubtitle *sub)
  516. {
  517. int ret;
  518. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)sub);
  519. avctx->frame_number++;
  520. return ret;
  521. }
  522. /**
  523. * decode a frame.
  524. * @param buf bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE larger then the actual read bytes
  525. * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  526. * @param buf_size the size of the buffer in bytes
  527. * @param got_picture_ptr zero if no frame could be decompressed, Otherwise, it is non zero
  528. * @return -1 if error, otherwise return the number of
  529. * bytes used.
  530. */
  531. int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  532. int *got_picture_ptr,
  533. uint8_t *buf, int buf_size)
  534. {
  535. int ret;
  536. *got_picture_ptr= 0;
  537. if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height))
  538. return -1;
  539. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){
  540. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  541. buf, buf_size);
  542. emms_c(); //needed to avoid an emms_c() call before every return;
  543. if (*got_picture_ptr)
  544. avctx->frame_number++;
  545. }else
  546. ret= 0;
  547. return ret;
  548. }
  549. /* decode an audio frame. return -1 if error, otherwise return the
  550. *number of bytes used. If no frame could be decompressed,
  551. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  552. *size in BYTES. */
  553. int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,
  554. int *frame_size_ptr,
  555. uint8_t *buf, int buf_size)
  556. {
  557. int ret;
  558. *frame_size_ptr= 0;
  559. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){
  560. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  561. buf, buf_size);
  562. avctx->frame_number++;
  563. }else
  564. ret= 0;
  565. return ret;
  566. }
  567. /* decode a subtitle message. return -1 if error, otherwise return the
  568. *number of bytes used. If no subtitle could be decompressed,
  569. *got_sub_ptr is zero. Otherwise, the subtitle is stored in *sub. */
  570. int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
  571. int *got_sub_ptr,
  572. const uint8_t *buf, int buf_size)
  573. {
  574. int ret;
  575. *got_sub_ptr = 0;
  576. ret = avctx->codec->decode(avctx, sub, got_sub_ptr,
  577. (uint8_t *)buf, buf_size);
  578. if (*got_sub_ptr)
  579. avctx->frame_number++;
  580. return ret;
  581. }
  582. int avcodec_close(AVCodecContext *avctx)
  583. {
  584. entangled_thread_counter++;
  585. if(entangled_thread_counter != 1){
  586. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  587. entangled_thread_counter--;
  588. return -1;
  589. }
  590. if (avctx->codec->close)
  591. avctx->codec->close(avctx);
  592. avcodec_default_free_buffers(avctx);
  593. av_freep(&avctx->priv_data);
  594. avctx->codec = NULL;
  595. entangled_thread_counter--;
  596. return 0;
  597. }
  598. AVCodec *avcodec_find_encoder(enum CodecID id)
  599. {
  600. AVCodec *p;
  601. p = first_avcodec;
  602. while (p) {
  603. if (p->encode != NULL && p->id == id)
  604. return p;
  605. p = p->next;
  606. }
  607. return NULL;
  608. }
  609. AVCodec *avcodec_find_encoder_by_name(const char *name)
  610. {
  611. AVCodec *p;
  612. p = first_avcodec;
  613. while (p) {
  614. if (p->encode != NULL && strcmp(name,p->name) == 0)
  615. return p;
  616. p = p->next;
  617. }
  618. return NULL;
  619. }
  620. AVCodec *avcodec_find_decoder(enum CodecID id)
  621. {
  622. AVCodec *p;
  623. p = first_avcodec;
  624. while (p) {
  625. if (p->decode != NULL && p->id == id)
  626. return p;
  627. p = p->next;
  628. }
  629. return NULL;
  630. }
  631. AVCodec *avcodec_find_decoder_by_name(const char *name)
  632. {
  633. AVCodec *p;
  634. p = first_avcodec;
  635. while (p) {
  636. if (p->decode != NULL && strcmp(name,p->name) == 0)
  637. return p;
  638. p = p->next;
  639. }
  640. return NULL;
  641. }
  642. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  643. {
  644. const char *codec_name;
  645. AVCodec *p;
  646. char buf1[32];
  647. char channels_str[100];
  648. int bitrate;
  649. if (encode)
  650. p = avcodec_find_encoder(enc->codec_id);
  651. else
  652. p = avcodec_find_decoder(enc->codec_id);
  653. if (p) {
  654. codec_name = p->name;
  655. if (!encode && enc->codec_id == CODEC_ID_MP3) {
  656. if (enc->sub_id == 2)
  657. codec_name = "mp2";
  658. else if (enc->sub_id == 1)
  659. codec_name = "mp1";
  660. }
  661. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  662. /* fake mpeg2 transport stream codec (currently not
  663. registered) */
  664. codec_name = "mpeg2ts";
  665. } else if (enc->codec_name[0] != '\0') {
  666. codec_name = enc->codec_name;
  667. } else {
  668. /* output avi tags */
  669. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  670. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  671. enc->codec_tag & 0xff,
  672. (enc->codec_tag >> 8) & 0xff,
  673. (enc->codec_tag >> 16) & 0xff,
  674. (enc->codec_tag >> 24) & 0xff);
  675. } else {
  676. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  677. }
  678. codec_name = buf1;
  679. }
  680. switch(enc->codec_type) {
  681. case CODEC_TYPE_VIDEO:
  682. snprintf(buf, buf_size,
  683. "Video: %s%s",
  684. codec_name, enc->mb_decision ? " (hq)" : "");
  685. if (enc->pix_fmt != PIX_FMT_NONE) {
  686. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  687. ", %s",
  688. avcodec_get_pix_fmt_name(enc->pix_fmt));
  689. }
  690. if (enc->width) {
  691. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  692. ", %dx%d, %0.2f fps",
  693. enc->width, enc->height,
  694. 1/av_q2d(enc->time_base));
  695. }
  696. if (encode) {
  697. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  698. ", q=%d-%d", enc->qmin, enc->qmax);
  699. }
  700. bitrate = enc->bit_rate;
  701. break;
  702. case CODEC_TYPE_AUDIO:
  703. snprintf(buf, buf_size,
  704. "Audio: %s",
  705. codec_name);
  706. switch (enc->channels) {
  707. case 1:
  708. strcpy(channels_str, "mono");
  709. break;
  710. case 2:
  711. strcpy(channels_str, "stereo");
  712. break;
  713. case 6:
  714. strcpy(channels_str, "5:1");
  715. break;
  716. default:
  717. snprintf(channels_str, sizeof(channels_str), "%d channels", enc->channels);
  718. break;
  719. }
  720. if (enc->sample_rate) {
  721. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  722. ", %d Hz, %s",
  723. enc->sample_rate,
  724. channels_str);
  725. }
  726. /* for PCM codecs, compute bitrate directly */
  727. switch(enc->codec_id) {
  728. case CODEC_ID_PCM_S16LE:
  729. case CODEC_ID_PCM_S16BE:
  730. case CODEC_ID_PCM_U16LE:
  731. case CODEC_ID_PCM_U16BE:
  732. bitrate = enc->sample_rate * enc->channels * 16;
  733. break;
  734. case CODEC_ID_PCM_S8:
  735. case CODEC_ID_PCM_U8:
  736. case CODEC_ID_PCM_ALAW:
  737. case CODEC_ID_PCM_MULAW:
  738. bitrate = enc->sample_rate * enc->channels * 8;
  739. break;
  740. default:
  741. bitrate = enc->bit_rate;
  742. break;
  743. }
  744. break;
  745. case CODEC_TYPE_DATA:
  746. snprintf(buf, buf_size, "Data: %s", codec_name);
  747. bitrate = enc->bit_rate;
  748. break;
  749. case CODEC_TYPE_SUBTITLE:
  750. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  751. bitrate = enc->bit_rate;
  752. break;
  753. default:
  754. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  755. return;
  756. }
  757. if (encode) {
  758. if (enc->flags & CODEC_FLAG_PASS1)
  759. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  760. ", pass 1");
  761. if (enc->flags & CODEC_FLAG_PASS2)
  762. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  763. ", pass 2");
  764. }
  765. if (bitrate != 0) {
  766. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  767. ", %d kb/s", bitrate / 1000);
  768. }
  769. }
  770. unsigned avcodec_version( void )
  771. {
  772. return LIBAVCODEC_VERSION_INT;
  773. }
  774. unsigned avcodec_build( void )
  775. {
  776. return LIBAVCODEC_BUILD;
  777. }
  778. /* must be called before any other functions */
  779. void avcodec_init(void)
  780. {
  781. static int inited = 0;
  782. if (inited != 0)
  783. return;
  784. inited = 1;
  785. dsputil_static_init();
  786. }
  787. /**
  788. * Flush buffers, should be called when seeking or when swicthing to a different stream.
  789. */
  790. void avcodec_flush_buffers(AVCodecContext *avctx)
  791. {
  792. if(avctx->codec->flush)
  793. avctx->codec->flush(avctx);
  794. }
  795. void avcodec_default_free_buffers(AVCodecContext *s){
  796. int i, j;
  797. if(s->internal_buffer==NULL) return;
  798. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  799. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  800. for(j=0; j<4; j++){
  801. av_freep(&buf->base[j]);
  802. buf->data[j]= NULL;
  803. }
  804. }
  805. av_freep(&s->internal_buffer);
  806. s->internal_buffer_count=0;
  807. }
  808. char av_get_pict_type_char(int pict_type){
  809. switch(pict_type){
  810. case I_TYPE: return 'I';
  811. case P_TYPE: return 'P';
  812. case B_TYPE: return 'B';
  813. case S_TYPE: return 'S';
  814. case SI_TYPE:return 'i';
  815. case SP_TYPE:return 'p';
  816. default: return '?';
  817. }
  818. }
  819. int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
  820. AVRational a0={0,1}, a1={1,0};
  821. int sign= (nom<0) ^ (den<0);
  822. int64_t gcd= ff_gcd(ABS(nom), ABS(den));
  823. nom = ABS(nom)/gcd;
  824. den = ABS(den)/gcd;
  825. if(nom<=max && den<=max){
  826. a1= (AVRational){nom, den};
  827. den=0;
  828. }
  829. while(den){
  830. int64_t x = nom / den;
  831. int64_t next_den= nom - den*x;
  832. int64_t a2n= x*a1.num + a0.num;
  833. int64_t a2d= x*a1.den + a0.den;
  834. if(a2n > max || a2d > max) break;
  835. a0= a1;
  836. a1= (AVRational){a2n, a2d};
  837. nom= den;
  838. den= next_den;
  839. }
  840. assert(ff_gcd(a1.num, a1.den) == 1);
  841. *dst_nom = sign ? -a1.num : a1.num;
  842. *dst_den = a1.den;
  843. return den==0;
  844. }
  845. int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd){
  846. AVInteger ai;
  847. int64_t r=0;
  848. assert(c > 0);
  849. assert(b >=0);
  850. assert(rnd >=0 && rnd<=5 && rnd!=4);
  851. if(a<0 && a != INT64_MIN) return -av_rescale_rnd(-a, b, c, rnd ^ ((rnd>>1)&1));
  852. if(rnd==AV_ROUND_NEAR_INF) r= c/2;
  853. else if(rnd&1) r= c-1;
  854. if(b<=INT_MAX && c<=INT_MAX){
  855. if(a<=INT_MAX)
  856. return (a * b + r)/c;
  857. else
  858. return a/c*b + (a%c*b + r)/c;
  859. }
  860. ai= av_mul_i(av_int2i(a), av_int2i(b));
  861. ai= av_add_i(ai, av_int2i(r));
  862. return av_i2int(av_div_i(ai, av_int2i(c)));
  863. }
  864. int64_t av_rescale(int64_t a, int64_t b, int64_t c){
  865. return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
  866. }
  867. int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq){
  868. int64_t b= bq.num * (int64_t)cq.den;
  869. int64_t c= cq.num * (int64_t)bq.den;
  870. return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
  871. }
  872. int64_t ff_gcd(int64_t a, int64_t b){
  873. if(b) return ff_gcd(b, a%b);
  874. else return a;
  875. }
  876. double av_int2dbl(int64_t v){
  877. if(v+v > 0xFFELLU<<52)
  878. return 0.0/0.0;
  879. return ldexp(((v&(1LL<<52)-1) + (1LL<<52)) * (v>>63|1), (v>>52&0x7FF)-1075);
  880. }
  881. float av_int2flt(int32_t v){
  882. if(v+v > 0xFF000000U)
  883. return 0.0/0.0;
  884. return ldexp(((v&0x7FFFFF) + (1<<23)) * (v>>31|1), (v>>23&0xFF)-150);
  885. }
  886. int64_t av_dbl2int(double d){
  887. int e;
  888. if ( !d) return 0;
  889. else if(d-d) return 0x7FF0000000000000LL + ((int64_t)(d<0)<<63) + (d!=d);
  890. d= frexp(d, &e);
  891. return (int64_t)(d<0)<<63 | (e+1022LL)<<52 | (int64_t)((fabs(d)-0.5)*(1LL<<53));
  892. }
  893. int32_t av_flt2int(float d){
  894. int e;
  895. if ( !d) return 0;
  896. else if(d-d) return 0x7F800000 + ((d<0)<<31) + (d!=d);
  897. d= frexp(d, &e);
  898. return (d<0)<<31 | (e+126)<<23 | (int64_t)((fabs(d)-0.5)*(1<<24));
  899. }
  900. /* av_log API */
  901. static int av_log_level = AV_LOG_INFO;
  902. static void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
  903. {
  904. static int print_prefix=1;
  905. AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
  906. if(level>av_log_level)
  907. return;
  908. #undef fprintf
  909. if(print_prefix && avc) {
  910. fprintf(stderr, "[%s @ %p]", avc->item_name(ptr), avc);
  911. }
  912. #define fprintf please_use_av_log
  913. print_prefix= strstr(fmt, "\n") != NULL;
  914. vfprintf(stderr, fmt, vl);
  915. }
  916. static void (*av_log_callback)(void*, int, const char*, va_list) = av_log_default_callback;
  917. void av_log(void* avcl, int level, const char *fmt, ...)
  918. {
  919. va_list vl;
  920. va_start(vl, fmt);
  921. av_vlog(avcl, level, fmt, vl);
  922. va_end(vl);
  923. }
  924. void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
  925. {
  926. av_log_callback(avcl, level, fmt, vl);
  927. }
  928. int av_log_get_level(void)
  929. {
  930. return av_log_level;
  931. }
  932. void av_log_set_level(int level)
  933. {
  934. av_log_level = level;
  935. }
  936. void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
  937. {
  938. av_log_callback = callback;
  939. }
  940. #if !defined(HAVE_THREADS)
  941. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  942. return -1;
  943. }
  944. #endif
  945. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  946. {
  947. unsigned int n = 0;
  948. while(v >= 0xff) {
  949. *s++ = 0xff;
  950. v -= 0xff;
  951. n++;
  952. }
  953. *s = v;
  954. n++;
  955. return n;
  956. }