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.

993 lines
27KB

  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. * Frees memory and sets the pointer to NULL.
  127. * @param arg pointer to the pointer which should be freed
  128. */
  129. void av_freep(void *arg)
  130. {
  131. void **ptr= (void**)arg;
  132. av_free(*ptr);
  133. *ptr = NULL;
  134. }
  135. /* encoder management */
  136. AVCodec *first_avcodec = NULL;
  137. void register_avcodec(AVCodec *format)
  138. {
  139. AVCodec **p;
  140. p = &first_avcodec;
  141. while (*p != NULL) p = &(*p)->next;
  142. *p = format;
  143. format->next = NULL;
  144. }
  145. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  146. s->coded_width = width;
  147. s->coded_height= height;
  148. s->width = -((-width )>>s->lowres);
  149. s->height= -((-height)>>s->lowres);
  150. }
  151. typedef struct InternalBuffer{
  152. int last_pic_num;
  153. uint8_t *base[4];
  154. uint8_t *data[4];
  155. int linesize[4];
  156. }InternalBuffer;
  157. #define INTERNAL_BUFFER_SIZE 32
  158. #define ALIGN(x, a) (((x)+(a)-1)&~((a)-1))
  159. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  160. int w_align= 1;
  161. int h_align= 1;
  162. switch(s->pix_fmt){
  163. case PIX_FMT_YUV420P:
  164. case PIX_FMT_YUV422:
  165. case PIX_FMT_UYVY422:
  166. case PIX_FMT_YUV422P:
  167. case PIX_FMT_YUV444P:
  168. case PIX_FMT_GRAY8:
  169. case PIX_FMT_YUVJ420P:
  170. case PIX_FMT_YUVJ422P:
  171. case PIX_FMT_YUVJ444P:
  172. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  173. h_align= 16;
  174. break;
  175. case PIX_FMT_YUV411P:
  176. case PIX_FMT_UYVY411:
  177. w_align=32;
  178. h_align=8;
  179. break;
  180. case PIX_FMT_YUV410P:
  181. if(s->codec_id == CODEC_ID_SVQ1){
  182. w_align=64;
  183. h_align=64;
  184. }
  185. case PIX_FMT_RGB555:
  186. if(s->codec_id == CODEC_ID_RPZA){
  187. w_align=4;
  188. h_align=4;
  189. }
  190. case PIX_FMT_PAL8:
  191. if(s->codec_id == CODEC_ID_SMC){
  192. w_align=4;
  193. h_align=4;
  194. }
  195. break;
  196. case PIX_FMT_BGR24:
  197. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  198. w_align=4;
  199. h_align=4;
  200. }
  201. break;
  202. default:
  203. w_align= 1;
  204. h_align= 1;
  205. break;
  206. }
  207. *width = ALIGN(*width , w_align);
  208. *height= ALIGN(*height, h_align);
  209. }
  210. int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
  211. if((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/4)
  212. return 0;
  213. av_log(av_log_ctx, AV_LOG_ERROR, "picture size invalid (%ux%u)\n", w, h);
  214. return -1;
  215. }
  216. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  217. int i;
  218. int w= s->width;
  219. int h= s->height;
  220. InternalBuffer *buf;
  221. int *picture_number;
  222. assert(pic->data[0]==NULL);
  223. assert(INTERNAL_BUFFER_SIZE > s->internal_buffer_count);
  224. if(avcodec_check_dimensions(s,w,h))
  225. return -1;
  226. if(s->internal_buffer==NULL){
  227. s->internal_buffer= av_mallocz(INTERNAL_BUFFER_SIZE*sizeof(InternalBuffer));
  228. }
  229. #if 0
  230. s->internal_buffer= av_fast_realloc(
  231. s->internal_buffer,
  232. &s->internal_buffer_size,
  233. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  234. );
  235. #endif
  236. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  237. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE-1]).last_pic_num; //FIXME ugly hack
  238. (*picture_number)++;
  239. if(buf->base[0]){
  240. pic->age= *picture_number - buf->last_pic_num;
  241. buf->last_pic_num= *picture_number;
  242. }else{
  243. int h_chroma_shift, v_chroma_shift;
  244. int pixel_size;
  245. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  246. switch(s->pix_fmt){
  247. case PIX_FMT_RGB555:
  248. case PIX_FMT_RGB565:
  249. case PIX_FMT_YUV422:
  250. case PIX_FMT_UYVY422:
  251. pixel_size=2;
  252. break;
  253. case PIX_FMT_RGB24:
  254. case PIX_FMT_BGR24:
  255. pixel_size=3;
  256. break;
  257. case PIX_FMT_RGBA32:
  258. pixel_size=4;
  259. break;
  260. default:
  261. pixel_size=1;
  262. }
  263. avcodec_align_dimensions(s, &w, &h);
  264. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  265. w+= EDGE_WIDTH*2;
  266. h+= EDGE_WIDTH*2;
  267. }
  268. buf->last_pic_num= -256*256*256*64;
  269. for(i=0; i<3; i++){
  270. const int h_shift= i==0 ? 0 : h_chroma_shift;
  271. const int v_shift= i==0 ? 0 : v_chroma_shift;
  272. //FIXME next ensures that linesize= 2^x uvlinesize, thats needed because some MC code assumes it
  273. buf->linesize[i]= ALIGN(pixel_size*w>>h_shift, STRIDE_ALIGN<<(h_chroma_shift-h_shift));
  274. buf->base[i]= av_malloc((buf->linesize[i]*h>>v_shift)+16); //FIXME 16
  275. if(buf->base[i]==NULL) return -1;
  276. memset(buf->base[i], 128, buf->linesize[i]*h>>v_shift);
  277. if(s->flags&CODEC_FLAG_EMU_EDGE)
  278. buf->data[i] = buf->base[i];
  279. else
  280. buf->data[i] = buf->base[i] + ALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), STRIDE_ALIGN);
  281. }
  282. pic->age= 256*256*256*64;
  283. }
  284. pic->type= FF_BUFFER_TYPE_INTERNAL;
  285. for(i=0; i<4; i++){
  286. pic->base[i]= buf->base[i];
  287. pic->data[i]= buf->data[i];
  288. pic->linesize[i]= buf->linesize[i];
  289. }
  290. s->internal_buffer_count++;
  291. return 0;
  292. }
  293. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  294. int i;
  295. InternalBuffer *buf, *last, temp;
  296. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  297. assert(s->internal_buffer_count);
  298. buf = NULL; /* avoids warning */
  299. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  300. buf= &((InternalBuffer*)s->internal_buffer)[i];
  301. if(buf->data[0] == pic->data[0])
  302. break;
  303. }
  304. assert(i < s->internal_buffer_count);
  305. s->internal_buffer_count--;
  306. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  307. temp= *buf;
  308. *buf= *last;
  309. *last= temp;
  310. for(i=0; i<3; i++){
  311. pic->data[i]=NULL;
  312. // pic->base[i]=NULL;
  313. }
  314. //printf("R%X\n", pic->opaque);
  315. }
  316. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  317. AVFrame temp_pic;
  318. int i;
  319. /* If no picture return a new buffer */
  320. if(pic->data[0] == NULL) {
  321. /* We will copy from buffer, so must be readable */
  322. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  323. return s->get_buffer(s, pic);
  324. }
  325. /* If internal buffer type return the same buffer */
  326. if(pic->type == FF_BUFFER_TYPE_INTERNAL)
  327. return 0;
  328. /*
  329. * Not internal type and reget_buffer not overridden, emulate cr buffer
  330. */
  331. temp_pic = *pic;
  332. for(i = 0; i < 4; i++)
  333. pic->data[i] = pic->base[i] = NULL;
  334. pic->opaque = NULL;
  335. /* Allocate new frame */
  336. if (s->get_buffer(s, pic))
  337. return -1;
  338. /* Copy image data from old buffer to new buffer */
  339. img_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  340. s->height);
  341. s->release_buffer(s, &temp_pic); // Release old frame
  342. return 0;
  343. }
  344. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){
  345. int i;
  346. for(i=0; i<count; i++){
  347. int r= func(c, arg[i]);
  348. if(ret) ret[i]= r;
  349. }
  350. return 0;
  351. }
  352. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat * fmt){
  353. return fmt[0];
  354. }
  355. static const char* context_to_name(void* ptr) {
  356. AVCodecContext *avc= ptr;
  357. if(avc && avc->codec && avc->codec->name)
  358. return avc->codec->name;
  359. else
  360. return "NULL";
  361. }
  362. static AVClass av_codec_context_class = { "AVCodecContext", context_to_name };
  363. void avcodec_get_context_defaults(AVCodecContext *s){
  364. memset(s, 0, sizeof(AVCodecContext));
  365. s->av_class= &av_codec_context_class;
  366. s->bit_rate= 800*1000;
  367. s->bit_rate_tolerance= s->bit_rate*10;
  368. s->qmin= 2;
  369. s->qmax= 31;
  370. s->mb_lmin= FF_QP2LAMBDA * 2;
  371. s->mb_lmax= FF_QP2LAMBDA * 31;
  372. s->rc_eq= "tex^qComp";
  373. s->qcompress= 0.5;
  374. s->max_qdiff= 3;
  375. s->b_quant_factor=1.25;
  376. s->b_quant_offset=1.25;
  377. s->i_quant_factor=-0.8;
  378. s->i_quant_offset=0.0;
  379. s->error_concealment= 3;
  380. s->error_resilience= 1;
  381. s->workaround_bugs= FF_BUG_AUTODETECT;
  382. s->time_base= (AVRational){0,1};
  383. s->gop_size= 50;
  384. s->me_method= ME_EPZS;
  385. s->get_buffer= avcodec_default_get_buffer;
  386. s->release_buffer= avcodec_default_release_buffer;
  387. s->get_format= avcodec_default_get_format;
  388. s->execute= avcodec_default_execute;
  389. s->thread_count=1;
  390. s->me_subpel_quality=8;
  391. s->lmin= FF_QP2LAMBDA * s->qmin;
  392. s->lmax= FF_QP2LAMBDA * s->qmax;
  393. s->sample_aspect_ratio= (AVRational){0,1};
  394. s->ildct_cmp= FF_CMP_VSAD;
  395. s->profile= FF_PROFILE_UNKNOWN;
  396. s->level= FF_LEVEL_UNKNOWN;
  397. s->me_penalty_compensation= 256;
  398. s->pix_fmt= PIX_FMT_NONE;
  399. s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS;
  400. s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS;
  401. s->palctrl = NULL;
  402. s->reget_buffer= avcodec_default_reget_buffer;
  403. }
  404. /**
  405. * allocates a AVCodecContext and set it to defaults.
  406. * this can be deallocated by simply calling free()
  407. */
  408. AVCodecContext *avcodec_alloc_context(void){
  409. AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
  410. if(avctx==NULL) return NULL;
  411. avcodec_get_context_defaults(avctx);
  412. return avctx;
  413. }
  414. void avcodec_get_frame_defaults(AVFrame *pic){
  415. memset(pic, 0, sizeof(AVFrame));
  416. pic->pts= AV_NOPTS_VALUE;
  417. pic->key_frame= 1;
  418. }
  419. /**
  420. * allocates a AVPFrame and set it to defaults.
  421. * this can be deallocated by simply calling free()
  422. */
  423. AVFrame *avcodec_alloc_frame(void){
  424. AVFrame *pic= av_malloc(sizeof(AVFrame));
  425. if(pic==NULL) return NULL;
  426. avcodec_get_frame_defaults(pic);
  427. return pic;
  428. }
  429. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  430. {
  431. int ret= -1;
  432. entangled_thread_counter++;
  433. if(entangled_thread_counter != 1){
  434. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  435. goto end;
  436. }
  437. if(avctx->codec)
  438. goto end;
  439. avctx->codec = codec;
  440. avctx->codec_id = codec->id;
  441. avctx->frame_number = 0;
  442. if (codec->priv_data_size > 0) {
  443. avctx->priv_data = av_mallocz(codec->priv_data_size);
  444. if (!avctx->priv_data)
  445. goto end;
  446. } else {
  447. avctx->priv_data = NULL;
  448. }
  449. if(avctx->coded_width && avctx->coded_height)
  450. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  451. else if(avctx->width && avctx->height)
  452. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  453. if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height)){
  454. av_freep(&avctx->priv_data);
  455. goto end;
  456. }
  457. ret = avctx->codec->init(avctx);
  458. if (ret < 0) {
  459. av_freep(&avctx->priv_data);
  460. goto end;
  461. }
  462. ret=0;
  463. end:
  464. entangled_thread_counter--;
  465. return ret;
  466. }
  467. int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  468. const short *samples)
  469. {
  470. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  471. av_log(avctx, AV_LOG_ERROR, "buffer smaller then minimum size\n");
  472. return -1;
  473. }
  474. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  475. int ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  476. avctx->frame_number++;
  477. return ret;
  478. }else
  479. return 0;
  480. }
  481. int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  482. const AVFrame *pict)
  483. {
  484. if(buf_size < FF_MIN_BUFFER_SIZE){
  485. av_log(avctx, AV_LOG_ERROR, "buffer smaller then minimum size\n");
  486. return -1;
  487. }
  488. if(avcodec_check_dimensions(avctx,avctx->width,avctx->height))
  489. return -1;
  490. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  491. int ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  492. avctx->frame_number++;
  493. emms_c(); //needed to avoid an emms_c() call before every return;
  494. return ret;
  495. }else
  496. return 0;
  497. }
  498. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  499. const AVSubtitle *sub)
  500. {
  501. int ret;
  502. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)sub);
  503. avctx->frame_number++;
  504. return ret;
  505. }
  506. /**
  507. * decode a frame.
  508. * @param buf bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE larger then the actual read bytes
  509. * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  510. * @param buf_size the size of the buffer in bytes
  511. * @param got_picture_ptr zero if no frame could be decompressed, Otherwise, it is non zero
  512. * @return -1 if error, otherwise return the number of
  513. * bytes used.
  514. */
  515. int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  516. int *got_picture_ptr,
  517. uint8_t *buf, int buf_size)
  518. {
  519. int ret;
  520. *got_picture_ptr= 0;
  521. if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height))
  522. return -1;
  523. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){
  524. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  525. buf, buf_size);
  526. emms_c(); //needed to avoid an emms_c() call before every return;
  527. if (*got_picture_ptr)
  528. avctx->frame_number++;
  529. }else
  530. ret= 0;
  531. return ret;
  532. }
  533. /* decode an audio frame. return -1 if error, otherwise return the
  534. *number of bytes used. If no frame could be decompressed,
  535. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  536. *size in BYTES. */
  537. int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,
  538. int *frame_size_ptr,
  539. uint8_t *buf, int buf_size)
  540. {
  541. int ret;
  542. *frame_size_ptr= 0;
  543. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){
  544. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  545. buf, buf_size);
  546. avctx->frame_number++;
  547. }else
  548. ret= 0;
  549. return ret;
  550. }
  551. /* decode a subtitle message. return -1 if error, otherwise return the
  552. *number of bytes used. If no subtitle could be decompressed,
  553. *got_sub_ptr is zero. Otherwise, the subtitle is stored in *sub. */
  554. int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
  555. int *got_sub_ptr,
  556. const uint8_t *buf, int buf_size)
  557. {
  558. int ret;
  559. *got_sub_ptr = 0;
  560. ret = avctx->codec->decode(avctx, sub, got_sub_ptr,
  561. (uint8_t *)buf, buf_size);
  562. if (*got_sub_ptr)
  563. avctx->frame_number++;
  564. return ret;
  565. }
  566. int avcodec_close(AVCodecContext *avctx)
  567. {
  568. entangled_thread_counter++;
  569. if(entangled_thread_counter != 1){
  570. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  571. entangled_thread_counter--;
  572. return -1;
  573. }
  574. if (avctx->codec->close)
  575. avctx->codec->close(avctx);
  576. avcodec_default_free_buffers(avctx);
  577. av_freep(&avctx->priv_data);
  578. avctx->codec = NULL;
  579. entangled_thread_counter--;
  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. /* av_log API */
  804. static int av_log_level = AV_LOG_INFO;
  805. static void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
  806. {
  807. static int print_prefix=1;
  808. AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
  809. if(level>av_log_level)
  810. return;
  811. #undef fprintf
  812. if(print_prefix && avc) {
  813. fprintf(stderr, "[%s @ %p]", avc->item_name(ptr), avc);
  814. }
  815. #define fprintf please_use_av_log
  816. print_prefix= strstr(fmt, "\n") != NULL;
  817. vfprintf(stderr, fmt, vl);
  818. }
  819. static void (*av_log_callback)(void*, int, const char*, va_list) = av_log_default_callback;
  820. void av_log(void* avcl, int level, const char *fmt, ...)
  821. {
  822. va_list vl;
  823. va_start(vl, fmt);
  824. av_vlog(avcl, level, fmt, vl);
  825. va_end(vl);
  826. }
  827. void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
  828. {
  829. av_log_callback(avcl, level, fmt, vl);
  830. }
  831. int av_log_get_level(void)
  832. {
  833. return av_log_level;
  834. }
  835. void av_log_set_level(int level)
  836. {
  837. av_log_level = level;
  838. }
  839. void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
  840. {
  841. av_log_callback = callback;
  842. }
  843. #if !defined(HAVE_THREADS)
  844. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  845. return -1;
  846. }
  847. #endif
  848. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  849. {
  850. unsigned int n = 0;
  851. while(v >= 0xff) {
  852. *s++ = 0xff;
  853. v -= 0xff;
  854. n++;
  855. }
  856. *s = v;
  857. n++;
  858. return n;
  859. }