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.

1019 lines
28KB

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