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.

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