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.

1110 lines
33KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file libavcodec/utils.c
  24. * utils.
  25. */
  26. /* needed for mkstemp() */
  27. #define _XOPEN_SOURCE 600
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/integer.h"
  30. #include "libavutil/crc.h"
  31. #include "avcodec.h"
  32. #include "dsputil.h"
  33. #include "opt.h"
  34. #include "imgconvert.h"
  35. #include "audioconvert.h"
  36. #include "internal.h"
  37. #include <stdlib.h>
  38. #include <stdarg.h>
  39. #include <limits.h>
  40. #include <float.h>
  41. #if !HAVE_MKSTEMP
  42. #include <fcntl.h>
  43. #endif
  44. const uint8_t ff_reverse[256]={
  45. 0x00,0x80,0x40,0xC0,0x20,0xA0,0x60,0xE0,0x10,0x90,0x50,0xD0,0x30,0xB0,0x70,0xF0,
  46. 0x08,0x88,0x48,0xC8,0x28,0xA8,0x68,0xE8,0x18,0x98,0x58,0xD8,0x38,0xB8,0x78,0xF8,
  47. 0x04,0x84,0x44,0xC4,0x24,0xA4,0x64,0xE4,0x14,0x94,0x54,0xD4,0x34,0xB4,0x74,0xF4,
  48. 0x0C,0x8C,0x4C,0xCC,0x2C,0xAC,0x6C,0xEC,0x1C,0x9C,0x5C,0xDC,0x3C,0xBC,0x7C,0xFC,
  49. 0x02,0x82,0x42,0xC2,0x22,0xA2,0x62,0xE2,0x12,0x92,0x52,0xD2,0x32,0xB2,0x72,0xF2,
  50. 0x0A,0x8A,0x4A,0xCA,0x2A,0xAA,0x6A,0xEA,0x1A,0x9A,0x5A,0xDA,0x3A,0xBA,0x7A,0xFA,
  51. 0x06,0x86,0x46,0xC6,0x26,0xA6,0x66,0xE6,0x16,0x96,0x56,0xD6,0x36,0xB6,0x76,0xF6,
  52. 0x0E,0x8E,0x4E,0xCE,0x2E,0xAE,0x6E,0xEE,0x1E,0x9E,0x5E,0xDE,0x3E,0xBE,0x7E,0xFE,
  53. 0x01,0x81,0x41,0xC1,0x21,0xA1,0x61,0xE1,0x11,0x91,0x51,0xD1,0x31,0xB1,0x71,0xF1,
  54. 0x09,0x89,0x49,0xC9,0x29,0xA9,0x69,0xE9,0x19,0x99,0x59,0xD9,0x39,0xB9,0x79,0xF9,
  55. 0x05,0x85,0x45,0xC5,0x25,0xA5,0x65,0xE5,0x15,0x95,0x55,0xD5,0x35,0xB5,0x75,0xF5,
  56. 0x0D,0x8D,0x4D,0xCD,0x2D,0xAD,0x6D,0xED,0x1D,0x9D,0x5D,0xDD,0x3D,0xBD,0x7D,0xFD,
  57. 0x03,0x83,0x43,0xC3,0x23,0xA3,0x63,0xE3,0x13,0x93,0x53,0xD3,0x33,0xB3,0x73,0xF3,
  58. 0x0B,0x8B,0x4B,0xCB,0x2B,0xAB,0x6B,0xEB,0x1B,0x9B,0x5B,0xDB,0x3B,0xBB,0x7B,0xFB,
  59. 0x07,0x87,0x47,0xC7,0x27,0xA7,0x67,0xE7,0x17,0x97,0x57,0xD7,0x37,0xB7,0x77,0xF7,
  60. 0x0F,0x8F,0x4F,0xCF,0x2F,0xAF,0x6F,0xEF,0x1F,0x9F,0x5F,0xDF,0x3F,0xBF,0x7F,0xFF,
  61. };
  62. static int volatile entangled_thread_counter=0;
  63. void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
  64. {
  65. if(min_size < *size)
  66. return ptr;
  67. *size= FFMAX(17*min_size/16 + 32, min_size);
  68. ptr= av_realloc(ptr, *size);
  69. if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
  70. *size= 0;
  71. return ptr;
  72. }
  73. /* encoder management */
  74. static AVCodec *first_avcodec = NULL;
  75. AVCodec *av_codec_next(AVCodec *c){
  76. if(c) return c->next;
  77. else return first_avcodec;
  78. }
  79. void register_avcodec(AVCodec *codec)
  80. {
  81. AVCodec **p;
  82. avcodec_init();
  83. p = &first_avcodec;
  84. while (*p != NULL) p = &(*p)->next;
  85. *p = codec;
  86. codec->next = NULL;
  87. }
  88. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  89. s->coded_width = width;
  90. s->coded_height= height;
  91. s->width = -((-width )>>s->lowres);
  92. s->height= -((-height)>>s->lowres);
  93. }
  94. typedef struct InternalBuffer{
  95. int last_pic_num;
  96. uint8_t *base[4];
  97. uint8_t *data[4];
  98. int linesize[4];
  99. int width, height;
  100. enum PixelFormat pix_fmt;
  101. }InternalBuffer;
  102. #define INTERNAL_BUFFER_SIZE 32
  103. #define ALIGN(x, a) (((x)+(a)-1)&~((a)-1))
  104. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  105. int w_align= 1;
  106. int h_align= 1;
  107. switch(s->pix_fmt){
  108. case PIX_FMT_YUV420P:
  109. case PIX_FMT_YUYV422:
  110. case PIX_FMT_UYVY422:
  111. case PIX_FMT_YUV422P:
  112. case PIX_FMT_YUV444P:
  113. case PIX_FMT_GRAY8:
  114. case PIX_FMT_GRAY16BE:
  115. case PIX_FMT_GRAY16LE:
  116. case PIX_FMT_YUVJ420P:
  117. case PIX_FMT_YUVJ422P:
  118. case PIX_FMT_YUVJ444P:
  119. case PIX_FMT_YUVA420P:
  120. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  121. h_align= 16;
  122. break;
  123. case PIX_FMT_YUV411P:
  124. case PIX_FMT_UYYVYY411:
  125. w_align=32;
  126. h_align=8;
  127. break;
  128. case PIX_FMT_YUV410P:
  129. if(s->codec_id == CODEC_ID_SVQ1){
  130. w_align=64;
  131. h_align=64;
  132. }
  133. case PIX_FMT_RGB555:
  134. if(s->codec_id == CODEC_ID_RPZA){
  135. w_align=4;
  136. h_align=4;
  137. }
  138. case PIX_FMT_PAL8:
  139. case PIX_FMT_BGR8:
  140. case PIX_FMT_RGB8:
  141. if(s->codec_id == CODEC_ID_SMC){
  142. w_align=4;
  143. h_align=4;
  144. }
  145. break;
  146. case PIX_FMT_BGR24:
  147. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  148. w_align=4;
  149. h_align=4;
  150. }
  151. break;
  152. default:
  153. w_align= 1;
  154. h_align= 1;
  155. break;
  156. }
  157. *width = ALIGN(*width , w_align);
  158. *height= ALIGN(*height, h_align);
  159. if(s->codec_id == CODEC_ID_H264)
  160. *height+=2; // some of the optimized chroma MC reads one line too much
  161. }
  162. int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
  163. if((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/4)
  164. return 0;
  165. av_log(av_log_ctx, AV_LOG_ERROR, "picture size invalid (%ux%u)\n", w, h);
  166. return -1;
  167. }
  168. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  169. int i;
  170. int w= s->width;
  171. int h= s->height;
  172. InternalBuffer *buf;
  173. int *picture_number;
  174. if(pic->data[0]!=NULL) {
  175. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  176. return -1;
  177. }
  178. if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
  179. av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
  180. return -1;
  181. }
  182. if(avcodec_check_dimensions(s,w,h))
  183. return -1;
  184. if(s->internal_buffer==NULL){
  185. s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
  186. }
  187. #if 0
  188. s->internal_buffer= av_fast_realloc(
  189. s->internal_buffer,
  190. &s->internal_buffer_size,
  191. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  192. );
  193. #endif
  194. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  195. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  196. (*picture_number)++;
  197. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  198. for(i=0; i<4; i++){
  199. av_freep(&buf->base[i]);
  200. buf->data[i]= NULL;
  201. }
  202. }
  203. if(buf->base[0]){
  204. pic->age= *picture_number - buf->last_pic_num;
  205. buf->last_pic_num= *picture_number;
  206. }else{
  207. int h_chroma_shift, v_chroma_shift;
  208. int size[4] = {0};
  209. int tmpsize;
  210. AVPicture picture;
  211. int stride_align[4];
  212. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  213. avcodec_align_dimensions(s, &w, &h);
  214. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  215. w+= EDGE_WIDTH*2;
  216. h+= EDGE_WIDTH*2;
  217. }
  218. ff_fill_linesize(&picture, s->pix_fmt, w);
  219. for (i=0; i<4; i++){
  220. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  221. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  222. //picture size unneccessarily in some cases. The solution here is not
  223. //pretty and better ideas are welcome!
  224. #if HAVE_MMX
  225. if(s->codec_id == CODEC_ID_SVQ1)
  226. stride_align[i]= 16;
  227. else
  228. #endif
  229. stride_align[i] = STRIDE_ALIGN;
  230. picture.linesize[i] = ALIGN(picture.linesize[i], stride_align[i]);
  231. }
  232. tmpsize = ff_fill_pointer(&picture, NULL, s->pix_fmt, h);
  233. for (i=0; i<3 && picture.data[i+1]; i++)
  234. size[i] = picture.data[i+1] - picture.data[i];
  235. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  236. buf->last_pic_num= -256*256*256*64;
  237. memset(buf->base, 0, sizeof(buf->base));
  238. memset(buf->data, 0, sizeof(buf->data));
  239. for(i=0; i<4 && size[i]; i++){
  240. const int h_shift= i==0 ? 0 : h_chroma_shift;
  241. const int v_shift= i==0 ? 0 : v_chroma_shift;
  242. buf->linesize[i]= picture.linesize[i];
  243. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  244. if(buf->base[i]==NULL) return -1;
  245. memset(buf->base[i], 128, size[i]);
  246. // no edge if EDEG EMU or not planar YUV
  247. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  248. buf->data[i] = buf->base[i];
  249. else
  250. buf->data[i] = buf->base[i] + ALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
  251. }
  252. if(size[1] && !size[2])
  253. ff_set_systematic_pal((uint32_t*)buf->data[1], s->pix_fmt);
  254. buf->width = s->width;
  255. buf->height = s->height;
  256. buf->pix_fmt= s->pix_fmt;
  257. pic->age= 256*256*256*64;
  258. }
  259. pic->type= FF_BUFFER_TYPE_INTERNAL;
  260. for(i=0; i<4; i++){
  261. pic->base[i]= buf->base[i];
  262. pic->data[i]= buf->data[i];
  263. pic->linesize[i]= buf->linesize[i];
  264. }
  265. s->internal_buffer_count++;
  266. pic->reordered_opaque= s->reordered_opaque;
  267. if(s->debug&FF_DEBUG_BUFFERS)
  268. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  269. return 0;
  270. }
  271. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  272. int i;
  273. InternalBuffer *buf, *last;
  274. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  275. assert(s->internal_buffer_count);
  276. buf = NULL; /* avoids warning */
  277. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  278. buf= &((InternalBuffer*)s->internal_buffer)[i];
  279. if(buf->data[0] == pic->data[0])
  280. break;
  281. }
  282. assert(i < s->internal_buffer_count);
  283. s->internal_buffer_count--;
  284. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  285. FFSWAP(InternalBuffer, *buf, *last);
  286. for(i=0; i<4; i++){
  287. pic->data[i]=NULL;
  288. // pic->base[i]=NULL;
  289. }
  290. //printf("R%X\n", pic->opaque);
  291. if(s->debug&FF_DEBUG_BUFFERS)
  292. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  293. }
  294. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  295. AVFrame temp_pic;
  296. int i;
  297. /* If no picture return a new buffer */
  298. if(pic->data[0] == NULL) {
  299. /* We will copy from buffer, so must be readable */
  300. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  301. return s->get_buffer(s, pic);
  302. }
  303. /* If internal buffer type return the same buffer */
  304. if(pic->type == FF_BUFFER_TYPE_INTERNAL)
  305. return 0;
  306. /*
  307. * Not internal type and reget_buffer not overridden, emulate cr buffer
  308. */
  309. temp_pic = *pic;
  310. for(i = 0; i < 4; i++)
  311. pic->data[i] = pic->base[i] = NULL;
  312. pic->opaque = NULL;
  313. /* Allocate new frame */
  314. if (s->get_buffer(s, pic))
  315. return -1;
  316. /* Copy image data from old buffer to new buffer */
  317. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  318. s->height);
  319. s->release_buffer(s, &temp_pic); // Release old frame
  320. return 0;
  321. }
  322. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  323. int i;
  324. for(i=0; i<count; i++){
  325. int r= func(c, (char*)arg + i*size);
  326. if(ret) ret[i]= r;
  327. }
  328. return 0;
  329. }
  330. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat * fmt){
  331. return fmt[0];
  332. }
  333. void avcodec_get_frame_defaults(AVFrame *pic){
  334. memset(pic, 0, sizeof(AVFrame));
  335. pic->pts= AV_NOPTS_VALUE;
  336. pic->key_frame= 1;
  337. }
  338. AVFrame *avcodec_alloc_frame(void){
  339. AVFrame *pic= av_malloc(sizeof(AVFrame));
  340. if(pic==NULL) return NULL;
  341. avcodec_get_frame_defaults(pic);
  342. return pic;
  343. }
  344. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  345. {
  346. int ret= -1;
  347. entangled_thread_counter++;
  348. if(entangled_thread_counter != 1){
  349. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  350. goto end;
  351. }
  352. if(avctx->codec || !codec)
  353. goto end;
  354. if (codec->priv_data_size > 0) {
  355. avctx->priv_data = av_mallocz(codec->priv_data_size);
  356. if (!avctx->priv_data) {
  357. ret = AVERROR(ENOMEM);
  358. goto end;
  359. }
  360. } else {
  361. avctx->priv_data = NULL;
  362. }
  363. if(avctx->coded_width && avctx->coded_height)
  364. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  365. else if(avctx->width && avctx->height)
  366. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  367. if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height)){
  368. av_freep(&avctx->priv_data);
  369. ret = AVERROR(EINVAL);
  370. goto end;
  371. }
  372. avctx->codec = codec;
  373. avctx->codec_id = codec->id;
  374. avctx->frame_number = 0;
  375. if(avctx->codec->init){
  376. ret = avctx->codec->init(avctx);
  377. if (ret < 0) {
  378. av_freep(&avctx->priv_data);
  379. avctx->codec= NULL;
  380. goto end;
  381. }
  382. }
  383. ret=0;
  384. end:
  385. entangled_thread_counter--;
  386. return ret;
  387. }
  388. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  389. const short *samples)
  390. {
  391. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  392. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  393. return -1;
  394. }
  395. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  396. int ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  397. avctx->frame_number++;
  398. return ret;
  399. }else
  400. return 0;
  401. }
  402. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  403. const AVFrame *pict)
  404. {
  405. if(buf_size < FF_MIN_BUFFER_SIZE){
  406. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  407. return -1;
  408. }
  409. if(avcodec_check_dimensions(avctx,avctx->width,avctx->height))
  410. return -1;
  411. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  412. int ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  413. avctx->frame_number++;
  414. emms_c(); //needed to avoid an emms_c() call before every return;
  415. return ret;
  416. }else
  417. return 0;
  418. }
  419. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  420. const AVSubtitle *sub)
  421. {
  422. int ret;
  423. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)sub);
  424. avctx->frame_number++;
  425. return ret;
  426. }
  427. int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  428. int *got_picture_ptr,
  429. const uint8_t *buf, int buf_size)
  430. {
  431. int ret;
  432. *got_picture_ptr= 0;
  433. if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height))
  434. return -1;
  435. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){
  436. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  437. buf, buf_size);
  438. emms_c(); //needed to avoid an emms_c() call before every return;
  439. if (*got_picture_ptr)
  440. avctx->frame_number++;
  441. }else
  442. ret= 0;
  443. return ret;
  444. }
  445. int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
  446. int *frame_size_ptr,
  447. const uint8_t *buf, int buf_size)
  448. {
  449. int ret;
  450. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){
  451. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  452. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  453. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  454. return -1;
  455. }
  456. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  457. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  458. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  459. return -1;
  460. }
  461. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  462. buf, buf_size);
  463. avctx->frame_number++;
  464. }else{
  465. ret= 0;
  466. *frame_size_ptr=0;
  467. }
  468. return ret;
  469. }
  470. int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
  471. int *got_sub_ptr,
  472. const uint8_t *buf, int buf_size)
  473. {
  474. int ret;
  475. *got_sub_ptr = 0;
  476. ret = avctx->codec->decode(avctx, sub, got_sub_ptr,
  477. buf, buf_size);
  478. if (*got_sub_ptr)
  479. avctx->frame_number++;
  480. return ret;
  481. }
  482. int avcodec_close(AVCodecContext *avctx)
  483. {
  484. entangled_thread_counter++;
  485. if(entangled_thread_counter != 1){
  486. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  487. entangled_thread_counter--;
  488. return -1;
  489. }
  490. if (HAVE_THREADS && avctx->thread_opaque)
  491. avcodec_thread_free(avctx);
  492. if (avctx->codec->close)
  493. avctx->codec->close(avctx);
  494. avcodec_default_free_buffers(avctx);
  495. av_freep(&avctx->priv_data);
  496. avctx->codec = NULL;
  497. entangled_thread_counter--;
  498. return 0;
  499. }
  500. AVCodec *avcodec_find_encoder(enum CodecID id)
  501. {
  502. AVCodec *p;
  503. p = first_avcodec;
  504. while (p) {
  505. if (p->encode != NULL && p->id == id)
  506. return p;
  507. p = p->next;
  508. }
  509. return NULL;
  510. }
  511. AVCodec *avcodec_find_encoder_by_name(const char *name)
  512. {
  513. AVCodec *p;
  514. if (!name)
  515. return NULL;
  516. p = first_avcodec;
  517. while (p) {
  518. if (p->encode != NULL && strcmp(name,p->name) == 0)
  519. return p;
  520. p = p->next;
  521. }
  522. return NULL;
  523. }
  524. AVCodec *avcodec_find_decoder(enum CodecID id)
  525. {
  526. AVCodec *p;
  527. p = first_avcodec;
  528. while (p) {
  529. if (p->decode != NULL && p->id == id)
  530. return p;
  531. p = p->next;
  532. }
  533. return NULL;
  534. }
  535. AVCodec *avcodec_find_decoder_by_name(const char *name)
  536. {
  537. AVCodec *p;
  538. if (!name)
  539. return NULL;
  540. p = first_avcodec;
  541. while (p) {
  542. if (p->decode != NULL && strcmp(name,p->name) == 0)
  543. return p;
  544. p = p->next;
  545. }
  546. return NULL;
  547. }
  548. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  549. {
  550. const char *codec_name;
  551. AVCodec *p;
  552. char buf1[32];
  553. int bitrate;
  554. AVRational display_aspect_ratio;
  555. if (encode)
  556. p = avcodec_find_encoder(enc->codec_id);
  557. else
  558. p = avcodec_find_decoder(enc->codec_id);
  559. if (p) {
  560. codec_name = p->name;
  561. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  562. /* fake mpeg2 transport stream codec (currently not
  563. registered) */
  564. codec_name = "mpeg2ts";
  565. } else if (enc->codec_name[0] != '\0') {
  566. codec_name = enc->codec_name;
  567. } else {
  568. /* output avi tags */
  569. if( isprint(enc->codec_tag&0xFF) && isprint((enc->codec_tag>>8)&0xFF)
  570. && isprint((enc->codec_tag>>16)&0xFF) && isprint((enc->codec_tag>>24)&0xFF)){
  571. snprintf(buf1, sizeof(buf1), "%c%c%c%c / 0x%04X",
  572. enc->codec_tag & 0xff,
  573. (enc->codec_tag >> 8) & 0xff,
  574. (enc->codec_tag >> 16) & 0xff,
  575. (enc->codec_tag >> 24) & 0xff,
  576. enc->codec_tag);
  577. } else {
  578. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  579. }
  580. codec_name = buf1;
  581. }
  582. switch(enc->codec_type) {
  583. case CODEC_TYPE_VIDEO:
  584. snprintf(buf, buf_size,
  585. "Video: %s%s",
  586. codec_name, enc->mb_decision ? " (hq)" : "");
  587. if (enc->pix_fmt != PIX_FMT_NONE) {
  588. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  589. ", %s",
  590. avcodec_get_pix_fmt_name(enc->pix_fmt));
  591. }
  592. if (enc->width) {
  593. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  594. ", %dx%d",
  595. enc->width, enc->height);
  596. if (enc->sample_aspect_ratio.num) {
  597. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  598. enc->width*enc->sample_aspect_ratio.num,
  599. enc->height*enc->sample_aspect_ratio.den,
  600. 1024*1024);
  601. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  602. " [PAR %d:%d DAR %d:%d]",
  603. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  604. display_aspect_ratio.num, display_aspect_ratio.den);
  605. }
  606. if(av_log_get_level() >= AV_LOG_DEBUG){
  607. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  608. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  609. ", %d/%d",
  610. enc->time_base.num/g, enc->time_base.den/g);
  611. }
  612. }
  613. if (encode) {
  614. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  615. ", q=%d-%d", enc->qmin, enc->qmax);
  616. }
  617. bitrate = enc->bit_rate;
  618. break;
  619. case CODEC_TYPE_AUDIO:
  620. snprintf(buf, buf_size,
  621. "Audio: %s",
  622. codec_name);
  623. if (enc->sample_rate) {
  624. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  625. ", %d Hz", enc->sample_rate);
  626. }
  627. av_strlcat(buf, ", ", buf_size);
  628. avcodec_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  629. if (enc->sample_fmt != SAMPLE_FMT_NONE) {
  630. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  631. ", %s", avcodec_get_sample_fmt_name(enc->sample_fmt));
  632. }
  633. /* for PCM codecs, compute bitrate directly */
  634. switch(enc->codec_id) {
  635. case CODEC_ID_PCM_F64BE:
  636. case CODEC_ID_PCM_F64LE:
  637. bitrate = enc->sample_rate * enc->channels * 64;
  638. break;
  639. case CODEC_ID_PCM_S32LE:
  640. case CODEC_ID_PCM_S32BE:
  641. case CODEC_ID_PCM_U32LE:
  642. case CODEC_ID_PCM_U32BE:
  643. case CODEC_ID_PCM_F32BE:
  644. case CODEC_ID_PCM_F32LE:
  645. bitrate = enc->sample_rate * enc->channels * 32;
  646. break;
  647. case CODEC_ID_PCM_S24LE:
  648. case CODEC_ID_PCM_S24BE:
  649. case CODEC_ID_PCM_U24LE:
  650. case CODEC_ID_PCM_U24BE:
  651. case CODEC_ID_PCM_S24DAUD:
  652. bitrate = enc->sample_rate * enc->channels * 24;
  653. break;
  654. case CODEC_ID_PCM_S16LE:
  655. case CODEC_ID_PCM_S16BE:
  656. case CODEC_ID_PCM_S16LE_PLANAR:
  657. case CODEC_ID_PCM_U16LE:
  658. case CODEC_ID_PCM_U16BE:
  659. bitrate = enc->sample_rate * enc->channels * 16;
  660. break;
  661. case CODEC_ID_PCM_S8:
  662. case CODEC_ID_PCM_U8:
  663. case CODEC_ID_PCM_ALAW:
  664. case CODEC_ID_PCM_MULAW:
  665. case CODEC_ID_PCM_ZORK:
  666. bitrate = enc->sample_rate * enc->channels * 8;
  667. break;
  668. default:
  669. bitrate = enc->bit_rate;
  670. break;
  671. }
  672. break;
  673. case CODEC_TYPE_DATA:
  674. snprintf(buf, buf_size, "Data: %s", codec_name);
  675. bitrate = enc->bit_rate;
  676. break;
  677. case CODEC_TYPE_SUBTITLE:
  678. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  679. bitrate = enc->bit_rate;
  680. break;
  681. case CODEC_TYPE_ATTACHMENT:
  682. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  683. bitrate = enc->bit_rate;
  684. break;
  685. default:
  686. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  687. return;
  688. }
  689. if (encode) {
  690. if (enc->flags & CODEC_FLAG_PASS1)
  691. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  692. ", pass 1");
  693. if (enc->flags & CODEC_FLAG_PASS2)
  694. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  695. ", pass 2");
  696. }
  697. if (bitrate != 0) {
  698. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  699. ", %d kb/s", bitrate / 1000);
  700. }
  701. }
  702. unsigned avcodec_version( void )
  703. {
  704. return LIBAVCODEC_VERSION_INT;
  705. }
  706. void avcodec_init(void)
  707. {
  708. static int initialized = 0;
  709. if (initialized != 0)
  710. return;
  711. initialized = 1;
  712. dsputil_static_init();
  713. }
  714. void avcodec_flush_buffers(AVCodecContext *avctx)
  715. {
  716. if(avctx->codec->flush)
  717. avctx->codec->flush(avctx);
  718. }
  719. void avcodec_default_free_buffers(AVCodecContext *s){
  720. int i, j;
  721. if(s->internal_buffer==NULL) return;
  722. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  723. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  724. for(j=0; j<4; j++){
  725. av_freep(&buf->base[j]);
  726. buf->data[j]= NULL;
  727. }
  728. }
  729. av_freep(&s->internal_buffer);
  730. s->internal_buffer_count=0;
  731. }
  732. char av_get_pict_type_char(int pict_type){
  733. switch(pict_type){
  734. case FF_I_TYPE: return 'I';
  735. case FF_P_TYPE: return 'P';
  736. case FF_B_TYPE: return 'B';
  737. case FF_S_TYPE: return 'S';
  738. case FF_SI_TYPE:return 'i';
  739. case FF_SP_TYPE:return 'p';
  740. case FF_BI_TYPE:return 'b';
  741. default: return '?';
  742. }
  743. }
  744. int av_get_bits_per_sample(enum CodecID codec_id){
  745. switch(codec_id){
  746. case CODEC_ID_ADPCM_SBPRO_2:
  747. return 2;
  748. case CODEC_ID_ADPCM_SBPRO_3:
  749. return 3;
  750. case CODEC_ID_ADPCM_SBPRO_4:
  751. case CODEC_ID_ADPCM_CT:
  752. return 4;
  753. case CODEC_ID_PCM_ALAW:
  754. case CODEC_ID_PCM_MULAW:
  755. case CODEC_ID_PCM_S8:
  756. case CODEC_ID_PCM_U8:
  757. case CODEC_ID_PCM_ZORK:
  758. return 8;
  759. case CODEC_ID_PCM_S16BE:
  760. case CODEC_ID_PCM_S16LE:
  761. case CODEC_ID_PCM_S16LE_PLANAR:
  762. case CODEC_ID_PCM_U16BE:
  763. case CODEC_ID_PCM_U16LE:
  764. return 16;
  765. case CODEC_ID_PCM_S24DAUD:
  766. case CODEC_ID_PCM_S24BE:
  767. case CODEC_ID_PCM_S24LE:
  768. case CODEC_ID_PCM_U24BE:
  769. case CODEC_ID_PCM_U24LE:
  770. return 24;
  771. case CODEC_ID_PCM_S32BE:
  772. case CODEC_ID_PCM_S32LE:
  773. case CODEC_ID_PCM_U32BE:
  774. case CODEC_ID_PCM_U32LE:
  775. case CODEC_ID_PCM_F32BE:
  776. case CODEC_ID_PCM_F32LE:
  777. return 32;
  778. case CODEC_ID_PCM_F64BE:
  779. case CODEC_ID_PCM_F64LE:
  780. return 64;
  781. default:
  782. return 0;
  783. }
  784. }
  785. int av_get_bits_per_sample_format(enum SampleFormat sample_fmt) {
  786. switch (sample_fmt) {
  787. case SAMPLE_FMT_U8:
  788. return 8;
  789. case SAMPLE_FMT_S16:
  790. return 16;
  791. case SAMPLE_FMT_S32:
  792. case SAMPLE_FMT_FLT:
  793. return 32;
  794. case SAMPLE_FMT_DBL:
  795. return 64;
  796. default:
  797. return 0;
  798. }
  799. }
  800. #if !HAVE_THREADS
  801. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  802. return -1;
  803. }
  804. #endif
  805. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  806. {
  807. unsigned int n = 0;
  808. while(v >= 0xff) {
  809. *s++ = 0xff;
  810. v -= 0xff;
  811. n++;
  812. }
  813. *s = v;
  814. n++;
  815. return n;
  816. }
  817. /* Wrapper to work around the lack of mkstemp() on mingw/cygin.
  818. * Also, tries to create file in /tmp first, if possible.
  819. * *prefix can be a character constant; *filename will be allocated internally.
  820. * Returns file descriptor of opened file (or -1 on error)
  821. * and opened file name in **filename. */
  822. int av_tempfile(char *prefix, char **filename) {
  823. int fd=-1;
  824. #if !HAVE_MKSTEMP
  825. *filename = tempnam(".", prefix);
  826. #else
  827. size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */
  828. *filename = av_malloc(len);
  829. #endif
  830. /* -----common section-----*/
  831. if (*filename == NULL) {
  832. av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
  833. return -1;
  834. }
  835. #if !HAVE_MKSTEMP
  836. fd = open(*filename, O_RDWR | O_BINARY | O_CREAT, 0444);
  837. #else
  838. snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
  839. fd = mkstemp(*filename);
  840. if (fd < 0) {
  841. snprintf(*filename, len, "./%sXXXXXX", prefix);
  842. fd = mkstemp(*filename);
  843. }
  844. #endif
  845. /* -----common section-----*/
  846. if (fd < 0) {
  847. av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename);
  848. return -1;
  849. }
  850. return fd; /* success */
  851. }
  852. typedef struct {
  853. const char *abbr;
  854. int width, height;
  855. } VideoFrameSizeAbbr;
  856. typedef struct {
  857. const char *abbr;
  858. int rate_num, rate_den;
  859. } VideoFrameRateAbbr;
  860. static const VideoFrameSizeAbbr video_frame_size_abbrs[] = {
  861. { "ntsc", 720, 480 },
  862. { "pal", 720, 576 },
  863. { "qntsc", 352, 240 }, /* VCD compliant NTSC */
  864. { "qpal", 352, 288 }, /* VCD compliant PAL */
  865. { "sntsc", 640, 480 }, /* square pixel NTSC */
  866. { "spal", 768, 576 }, /* square pixel PAL */
  867. { "film", 352, 240 },
  868. { "ntsc-film", 352, 240 },
  869. { "sqcif", 128, 96 },
  870. { "qcif", 176, 144 },
  871. { "cif", 352, 288 },
  872. { "4cif", 704, 576 },
  873. { "qqvga", 160, 120 },
  874. { "qvga", 320, 240 },
  875. { "vga", 640, 480 },
  876. { "svga", 800, 600 },
  877. { "xga", 1024, 768 },
  878. { "uxga", 1600,1200 },
  879. { "qxga", 2048,1536 },
  880. { "sxga", 1280,1024 },
  881. { "qsxga", 2560,2048 },
  882. { "hsxga", 5120,4096 },
  883. { "wvga", 852, 480 },
  884. { "wxga", 1366, 768 },
  885. { "wsxga", 1600,1024 },
  886. { "wuxga", 1920,1200 },
  887. { "woxga", 2560,1600 },
  888. { "wqsxga", 3200,2048 },
  889. { "wquxga", 3840,2400 },
  890. { "whsxga", 6400,4096 },
  891. { "whuxga", 7680,4800 },
  892. { "cga", 320, 200 },
  893. { "ega", 640, 350 },
  894. { "hd480", 852, 480 },
  895. { "hd720", 1280, 720 },
  896. { "hd1080", 1920,1080 },
  897. };
  898. static const VideoFrameRateAbbr video_frame_rate_abbrs[]= {
  899. { "ntsc", 30000, 1001 },
  900. { "pal", 25, 1 },
  901. { "qntsc", 30000, 1001 }, /* VCD compliant NTSC */
  902. { "qpal", 25, 1 }, /* VCD compliant PAL */
  903. { "sntsc", 30000, 1001 }, /* square pixel NTSC */
  904. { "spal", 25, 1 }, /* square pixel PAL */
  905. { "film", 24, 1 },
  906. { "ntsc-film", 24000, 1001 },
  907. };
  908. int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
  909. {
  910. int i;
  911. int n = FF_ARRAY_ELEMS(video_frame_size_abbrs);
  912. const char *p;
  913. int frame_width = 0, frame_height = 0;
  914. for(i=0;i<n;i++) {
  915. if (!strcmp(video_frame_size_abbrs[i].abbr, str)) {
  916. frame_width = video_frame_size_abbrs[i].width;
  917. frame_height = video_frame_size_abbrs[i].height;
  918. break;
  919. }
  920. }
  921. if (i == n) {
  922. p = str;
  923. frame_width = strtol(p, (char **)&p, 10);
  924. if (*p)
  925. p++;
  926. frame_height = strtol(p, (char **)&p, 10);
  927. }
  928. if (frame_width <= 0 || frame_height <= 0)
  929. return -1;
  930. *width_ptr = frame_width;
  931. *height_ptr = frame_height;
  932. return 0;
  933. }
  934. int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
  935. {
  936. int i;
  937. int n = FF_ARRAY_ELEMS(video_frame_rate_abbrs);
  938. char* cp;
  939. /* First, we check our abbreviation table */
  940. for (i = 0; i < n; ++i)
  941. if (!strcmp(video_frame_rate_abbrs[i].abbr, arg)) {
  942. frame_rate->num = video_frame_rate_abbrs[i].rate_num;
  943. frame_rate->den = video_frame_rate_abbrs[i].rate_den;
  944. return 0;
  945. }
  946. /* Then, we try to parse it as fraction */
  947. cp = strchr(arg, '/');
  948. if (!cp)
  949. cp = strchr(arg, ':');
  950. if (cp) {
  951. char* cpp;
  952. frame_rate->num = strtol(arg, &cpp, 10);
  953. if (cpp != arg || cpp == cp)
  954. frame_rate->den = strtol(cp+1, &cpp, 10);
  955. else
  956. frame_rate->num = 0;
  957. }
  958. else {
  959. /* Finally we give up and parse it as double */
  960. AVRational time_base = av_d2q(strtod(arg, 0), 1001000);
  961. frame_rate->den = time_base.den;
  962. frame_rate->num = time_base.num;
  963. }
  964. if (!frame_rate->num || !frame_rate->den)
  965. return -1;
  966. else
  967. return 0;
  968. }
  969. void ff_log_missing_feature(void *avc, const char *feature, int want_sample)
  970. {
  971. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  972. "version to the newest one from SVN. If the problem still "
  973. "occurs, it means that your file has a feature which has not "
  974. "been implemented.", feature);
  975. if(want_sample)
  976. ff_log_ask_for_sample(avc, NULL);
  977. else
  978. av_log(avc, AV_LOG_WARNING, "\n");
  979. }
  980. void ff_log_ask_for_sample(void *avc, const char *msg)
  981. {
  982. if (msg)
  983. av_log(avc, AV_LOG_WARNING, "%s ", msg);
  984. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  985. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  986. "and contact the ffmpeg-devel mailing list.\n");
  987. }