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.

1106 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 "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. if(s->codec_id == CODEC_ID_SMC){
  140. w_align=4;
  141. h_align=4;
  142. }
  143. break;
  144. case PIX_FMT_BGR24:
  145. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  146. w_align=4;
  147. h_align=4;
  148. }
  149. break;
  150. default:
  151. w_align= 1;
  152. h_align= 1;
  153. break;
  154. }
  155. *width = ALIGN(*width , w_align);
  156. *height= ALIGN(*height, h_align);
  157. if(s->codec_id == CODEC_ID_H264)
  158. *height+=2; // some of the optimized chroma MC reads one line too much
  159. }
  160. int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
  161. if((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/4)
  162. return 0;
  163. av_log(av_log_ctx, AV_LOG_ERROR, "picture size invalid (%ux%u)\n", w, h);
  164. return -1;
  165. }
  166. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  167. int i;
  168. int w= s->width;
  169. int h= s->height;
  170. InternalBuffer *buf;
  171. int *picture_number;
  172. if(pic->data[0]!=NULL) {
  173. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  174. return -1;
  175. }
  176. if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
  177. av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
  178. return -1;
  179. }
  180. if(avcodec_check_dimensions(s,w,h))
  181. return -1;
  182. if(s->internal_buffer==NULL){
  183. s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
  184. }
  185. #if 0
  186. s->internal_buffer= av_fast_realloc(
  187. s->internal_buffer,
  188. &s->internal_buffer_size,
  189. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  190. );
  191. #endif
  192. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  193. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  194. (*picture_number)++;
  195. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  196. for(i=0; i<4; i++){
  197. av_freep(&buf->base[i]);
  198. buf->data[i]= NULL;
  199. }
  200. }
  201. if(buf->base[0]){
  202. pic->age= *picture_number - buf->last_pic_num;
  203. buf->last_pic_num= *picture_number;
  204. }else{
  205. int h_chroma_shift, v_chroma_shift;
  206. int size[4] = {0};
  207. int tmpsize;
  208. AVPicture picture;
  209. int stride_align[4];
  210. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  211. avcodec_align_dimensions(s, &w, &h);
  212. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  213. w+= EDGE_WIDTH*2;
  214. h+= EDGE_WIDTH*2;
  215. }
  216. ff_fill_linesize(&picture, s->pix_fmt, w);
  217. for (i=0; i<4; i++){
  218. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  219. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  220. //picture size unneccessarily in some cases. The solution here is not
  221. //pretty and better ideas are welcome!
  222. #if HAVE_MMX
  223. if(s->codec_id == CODEC_ID_SVQ1)
  224. stride_align[i]= 16;
  225. else
  226. #endif
  227. stride_align[i] = STRIDE_ALIGN;
  228. picture.linesize[i] = ALIGN(picture.linesize[i], stride_align[i]);
  229. }
  230. tmpsize = ff_fill_pointer(&picture, NULL, s->pix_fmt, h);
  231. for (i=0; i<3 && picture.data[i+1]; i++)
  232. size[i] = picture.data[i+1] - picture.data[i];
  233. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  234. buf->last_pic_num= -256*256*256*64;
  235. memset(buf->base, 0, sizeof(buf->base));
  236. memset(buf->data, 0, sizeof(buf->data));
  237. for(i=0; i<4 && size[i]; i++){
  238. const int h_shift= i==0 ? 0 : h_chroma_shift;
  239. const int v_shift= i==0 ? 0 : v_chroma_shift;
  240. buf->linesize[i]= picture.linesize[i];
  241. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  242. if(buf->base[i]==NULL) return -1;
  243. memset(buf->base[i], 128, size[i]);
  244. // no edge if EDEG EMU or not planar YUV, we check for PAL8 redundantly to protect against a exploitable bug regression ...
  245. if((s->flags&CODEC_FLAG_EMU_EDGE) || (s->pix_fmt == PIX_FMT_PAL8) || !size[2])
  246. buf->data[i] = buf->base[i];
  247. else
  248. buf->data[i] = buf->base[i] + ALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
  249. }
  250. buf->width = s->width;
  251. buf->height = s->height;
  252. buf->pix_fmt= s->pix_fmt;
  253. pic->age= 256*256*256*64;
  254. }
  255. pic->type= FF_BUFFER_TYPE_INTERNAL;
  256. for(i=0; i<4; i++){
  257. pic->base[i]= buf->base[i];
  258. pic->data[i]= buf->data[i];
  259. pic->linesize[i]= buf->linesize[i];
  260. }
  261. s->internal_buffer_count++;
  262. pic->reordered_opaque= s->reordered_opaque;
  263. if(s->debug&FF_DEBUG_BUFFERS)
  264. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  265. return 0;
  266. }
  267. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  268. int i;
  269. InternalBuffer *buf, *last;
  270. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  271. assert(s->internal_buffer_count);
  272. buf = NULL; /* avoids warning */
  273. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  274. buf= &((InternalBuffer*)s->internal_buffer)[i];
  275. if(buf->data[0] == pic->data[0])
  276. break;
  277. }
  278. assert(i < s->internal_buffer_count);
  279. s->internal_buffer_count--;
  280. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  281. FFSWAP(InternalBuffer, *buf, *last);
  282. for(i=0; i<4; i++){
  283. pic->data[i]=NULL;
  284. // pic->base[i]=NULL;
  285. }
  286. //printf("R%X\n", pic->opaque);
  287. if(s->debug&FF_DEBUG_BUFFERS)
  288. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  289. }
  290. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  291. AVFrame temp_pic;
  292. int i;
  293. /* If no picture return a new buffer */
  294. if(pic->data[0] == NULL) {
  295. /* We will copy from buffer, so must be readable */
  296. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  297. return s->get_buffer(s, pic);
  298. }
  299. /* If internal buffer type return the same buffer */
  300. if(pic->type == FF_BUFFER_TYPE_INTERNAL)
  301. return 0;
  302. /*
  303. * Not internal type and reget_buffer not overridden, emulate cr buffer
  304. */
  305. temp_pic = *pic;
  306. for(i = 0; i < 4; i++)
  307. pic->data[i] = pic->base[i] = NULL;
  308. pic->opaque = NULL;
  309. /* Allocate new frame */
  310. if (s->get_buffer(s, pic))
  311. return -1;
  312. /* Copy image data from old buffer to new buffer */
  313. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  314. s->height);
  315. s->release_buffer(s, &temp_pic); // Release old frame
  316. return 0;
  317. }
  318. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  319. int i;
  320. for(i=0; i<count; i++){
  321. int r= func(c, (char*)arg + i*size);
  322. if(ret) ret[i]= r;
  323. }
  324. return 0;
  325. }
  326. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat * fmt){
  327. return fmt[0];
  328. }
  329. void avcodec_get_frame_defaults(AVFrame *pic){
  330. memset(pic, 0, sizeof(AVFrame));
  331. pic->pts= AV_NOPTS_VALUE;
  332. pic->key_frame= 1;
  333. }
  334. AVFrame *avcodec_alloc_frame(void){
  335. AVFrame *pic= av_malloc(sizeof(AVFrame));
  336. if(pic==NULL) return NULL;
  337. avcodec_get_frame_defaults(pic);
  338. return pic;
  339. }
  340. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  341. {
  342. int ret= -1;
  343. entangled_thread_counter++;
  344. if(entangled_thread_counter != 1){
  345. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  346. goto end;
  347. }
  348. if(avctx->codec || !codec)
  349. goto end;
  350. if (codec->priv_data_size > 0) {
  351. avctx->priv_data = av_mallocz(codec->priv_data_size);
  352. if (!avctx->priv_data) {
  353. ret = AVERROR(ENOMEM);
  354. goto end;
  355. }
  356. } else {
  357. avctx->priv_data = NULL;
  358. }
  359. if(avctx->coded_width && avctx->coded_height)
  360. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  361. else if(avctx->width && avctx->height)
  362. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  363. if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height)){
  364. av_freep(&avctx->priv_data);
  365. ret = AVERROR(EINVAL);
  366. goto end;
  367. }
  368. avctx->codec = codec;
  369. avctx->codec_id = codec->id;
  370. avctx->frame_number = 0;
  371. if(avctx->codec->init){
  372. ret = avctx->codec->init(avctx);
  373. if (ret < 0) {
  374. av_freep(&avctx->priv_data);
  375. avctx->codec= NULL;
  376. goto end;
  377. }
  378. }
  379. ret=0;
  380. end:
  381. entangled_thread_counter--;
  382. return ret;
  383. }
  384. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  385. const short *samples)
  386. {
  387. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  388. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  389. return -1;
  390. }
  391. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  392. int ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  393. avctx->frame_number++;
  394. return ret;
  395. }else
  396. return 0;
  397. }
  398. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  399. const AVFrame *pict)
  400. {
  401. if(buf_size < FF_MIN_BUFFER_SIZE){
  402. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  403. return -1;
  404. }
  405. if(avcodec_check_dimensions(avctx,avctx->width,avctx->height))
  406. return -1;
  407. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  408. int ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  409. avctx->frame_number++;
  410. emms_c(); //needed to avoid an emms_c() call before every return;
  411. return ret;
  412. }else
  413. return 0;
  414. }
  415. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  416. const AVSubtitle *sub)
  417. {
  418. int ret;
  419. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)sub);
  420. avctx->frame_number++;
  421. return ret;
  422. }
  423. int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  424. int *got_picture_ptr,
  425. const uint8_t *buf, int buf_size)
  426. {
  427. int ret;
  428. *got_picture_ptr= 0;
  429. if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height))
  430. return -1;
  431. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){
  432. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  433. buf, buf_size);
  434. emms_c(); //needed to avoid an emms_c() call before every return;
  435. if (*got_picture_ptr)
  436. avctx->frame_number++;
  437. }else
  438. ret= 0;
  439. return ret;
  440. }
  441. int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
  442. int *frame_size_ptr,
  443. const uint8_t *buf, int buf_size)
  444. {
  445. int ret;
  446. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){
  447. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  448. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  449. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  450. return -1;
  451. }
  452. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  453. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  454. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  455. return -1;
  456. }
  457. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  458. buf, buf_size);
  459. avctx->frame_number++;
  460. }else{
  461. ret= 0;
  462. *frame_size_ptr=0;
  463. }
  464. return ret;
  465. }
  466. int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
  467. int *got_sub_ptr,
  468. const uint8_t *buf, int buf_size)
  469. {
  470. int ret;
  471. *got_sub_ptr = 0;
  472. ret = avctx->codec->decode(avctx, sub, got_sub_ptr,
  473. buf, buf_size);
  474. if (*got_sub_ptr)
  475. avctx->frame_number++;
  476. return ret;
  477. }
  478. int avcodec_close(AVCodecContext *avctx)
  479. {
  480. entangled_thread_counter++;
  481. if(entangled_thread_counter != 1){
  482. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  483. entangled_thread_counter--;
  484. return -1;
  485. }
  486. if (HAVE_THREADS && avctx->thread_opaque)
  487. avcodec_thread_free(avctx);
  488. if (avctx->codec->close)
  489. avctx->codec->close(avctx);
  490. avcodec_default_free_buffers(avctx);
  491. av_freep(&avctx->priv_data);
  492. avctx->codec = NULL;
  493. entangled_thread_counter--;
  494. return 0;
  495. }
  496. AVCodec *avcodec_find_encoder(enum CodecID id)
  497. {
  498. AVCodec *p;
  499. p = first_avcodec;
  500. while (p) {
  501. if (p->encode != NULL && p->id == id)
  502. return p;
  503. p = p->next;
  504. }
  505. return NULL;
  506. }
  507. AVCodec *avcodec_find_encoder_by_name(const char *name)
  508. {
  509. AVCodec *p;
  510. if (!name)
  511. return NULL;
  512. p = first_avcodec;
  513. while (p) {
  514. if (p->encode != NULL && strcmp(name,p->name) == 0)
  515. return p;
  516. p = p->next;
  517. }
  518. return NULL;
  519. }
  520. AVCodec *avcodec_find_decoder(enum CodecID id)
  521. {
  522. AVCodec *p;
  523. p = first_avcodec;
  524. while (p) {
  525. if (p->decode != NULL && p->id == id)
  526. return p;
  527. p = p->next;
  528. }
  529. return NULL;
  530. }
  531. AVCodec *avcodec_find_decoder_by_name(const char *name)
  532. {
  533. AVCodec *p;
  534. if (!name)
  535. return NULL;
  536. p = first_avcodec;
  537. while (p) {
  538. if (p->decode != NULL && strcmp(name,p->name) == 0)
  539. return p;
  540. p = p->next;
  541. }
  542. return NULL;
  543. }
  544. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  545. {
  546. const char *codec_name;
  547. AVCodec *p;
  548. char buf1[32];
  549. int bitrate;
  550. AVRational display_aspect_ratio;
  551. if (encode)
  552. p = avcodec_find_encoder(enc->codec_id);
  553. else
  554. p = avcodec_find_decoder(enc->codec_id);
  555. if (p) {
  556. codec_name = p->name;
  557. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  558. /* fake mpeg2 transport stream codec (currently not
  559. registered) */
  560. codec_name = "mpeg2ts";
  561. } else if (enc->codec_name[0] != '\0') {
  562. codec_name = enc->codec_name;
  563. } else {
  564. /* output avi tags */
  565. if( isprint(enc->codec_tag&0xFF) && isprint((enc->codec_tag>>8)&0xFF)
  566. && isprint((enc->codec_tag>>16)&0xFF) && isprint((enc->codec_tag>>24)&0xFF)){
  567. snprintf(buf1, sizeof(buf1), "%c%c%c%c / 0x%04X",
  568. enc->codec_tag & 0xff,
  569. (enc->codec_tag >> 8) & 0xff,
  570. (enc->codec_tag >> 16) & 0xff,
  571. (enc->codec_tag >> 24) & 0xff,
  572. enc->codec_tag);
  573. } else {
  574. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  575. }
  576. codec_name = buf1;
  577. }
  578. switch(enc->codec_type) {
  579. case CODEC_TYPE_VIDEO:
  580. snprintf(buf, buf_size,
  581. "Video: %s%s",
  582. codec_name, enc->mb_decision ? " (hq)" : "");
  583. if (enc->pix_fmt != PIX_FMT_NONE) {
  584. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  585. ", %s",
  586. avcodec_get_pix_fmt_name(enc->pix_fmt));
  587. }
  588. if (enc->width) {
  589. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  590. ", %dx%d",
  591. enc->width, enc->height);
  592. if (enc->sample_aspect_ratio.num) {
  593. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  594. enc->width*enc->sample_aspect_ratio.num,
  595. enc->height*enc->sample_aspect_ratio.den,
  596. 1024*1024);
  597. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  598. " [PAR %d:%d DAR %d:%d]",
  599. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  600. display_aspect_ratio.num, display_aspect_ratio.den);
  601. }
  602. if(av_log_get_level() >= AV_LOG_DEBUG){
  603. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  604. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  605. ", %d/%d",
  606. enc->time_base.num/g, enc->time_base.den/g);
  607. }
  608. }
  609. if (encode) {
  610. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  611. ", q=%d-%d", enc->qmin, enc->qmax);
  612. }
  613. bitrate = enc->bit_rate;
  614. break;
  615. case CODEC_TYPE_AUDIO:
  616. snprintf(buf, buf_size,
  617. "Audio: %s",
  618. codec_name);
  619. if (enc->sample_rate) {
  620. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  621. ", %d Hz", enc->sample_rate);
  622. }
  623. av_strlcat(buf, ", ", buf_size);
  624. avcodec_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  625. if (enc->sample_fmt != SAMPLE_FMT_NONE) {
  626. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  627. ", %s", avcodec_get_sample_fmt_name(enc->sample_fmt));
  628. }
  629. /* for PCM codecs, compute bitrate directly */
  630. switch(enc->codec_id) {
  631. case CODEC_ID_PCM_F64BE:
  632. case CODEC_ID_PCM_F64LE:
  633. bitrate = enc->sample_rate * enc->channels * 64;
  634. break;
  635. case CODEC_ID_PCM_S32LE:
  636. case CODEC_ID_PCM_S32BE:
  637. case CODEC_ID_PCM_U32LE:
  638. case CODEC_ID_PCM_U32BE:
  639. case CODEC_ID_PCM_F32BE:
  640. case CODEC_ID_PCM_F32LE:
  641. bitrate = enc->sample_rate * enc->channels * 32;
  642. break;
  643. case CODEC_ID_PCM_S24LE:
  644. case CODEC_ID_PCM_S24BE:
  645. case CODEC_ID_PCM_U24LE:
  646. case CODEC_ID_PCM_U24BE:
  647. case CODEC_ID_PCM_S24DAUD:
  648. bitrate = enc->sample_rate * enc->channels * 24;
  649. break;
  650. case CODEC_ID_PCM_S16LE:
  651. case CODEC_ID_PCM_S16BE:
  652. case CODEC_ID_PCM_S16LE_PLANAR:
  653. case CODEC_ID_PCM_U16LE:
  654. case CODEC_ID_PCM_U16BE:
  655. bitrate = enc->sample_rate * enc->channels * 16;
  656. break;
  657. case CODEC_ID_PCM_S8:
  658. case CODEC_ID_PCM_U8:
  659. case CODEC_ID_PCM_ALAW:
  660. case CODEC_ID_PCM_MULAW:
  661. case CODEC_ID_PCM_ZORK:
  662. bitrate = enc->sample_rate * enc->channels * 8;
  663. break;
  664. default:
  665. bitrate = enc->bit_rate;
  666. break;
  667. }
  668. break;
  669. case CODEC_TYPE_DATA:
  670. snprintf(buf, buf_size, "Data: %s", codec_name);
  671. bitrate = enc->bit_rate;
  672. break;
  673. case CODEC_TYPE_SUBTITLE:
  674. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  675. bitrate = enc->bit_rate;
  676. break;
  677. case CODEC_TYPE_ATTACHMENT:
  678. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  679. bitrate = enc->bit_rate;
  680. break;
  681. default:
  682. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  683. return;
  684. }
  685. if (encode) {
  686. if (enc->flags & CODEC_FLAG_PASS1)
  687. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  688. ", pass 1");
  689. if (enc->flags & CODEC_FLAG_PASS2)
  690. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  691. ", pass 2");
  692. }
  693. if (bitrate != 0) {
  694. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  695. ", %d kb/s", bitrate / 1000);
  696. }
  697. }
  698. unsigned avcodec_version( void )
  699. {
  700. return LIBAVCODEC_VERSION_INT;
  701. }
  702. void avcodec_init(void)
  703. {
  704. static int initialized = 0;
  705. if (initialized != 0)
  706. return;
  707. initialized = 1;
  708. dsputil_static_init();
  709. }
  710. void avcodec_flush_buffers(AVCodecContext *avctx)
  711. {
  712. if(avctx->codec->flush)
  713. avctx->codec->flush(avctx);
  714. }
  715. void avcodec_default_free_buffers(AVCodecContext *s){
  716. int i, j;
  717. if(s->internal_buffer==NULL) return;
  718. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  719. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  720. for(j=0; j<4; j++){
  721. av_freep(&buf->base[j]);
  722. buf->data[j]= NULL;
  723. }
  724. }
  725. av_freep(&s->internal_buffer);
  726. s->internal_buffer_count=0;
  727. }
  728. char av_get_pict_type_char(int pict_type){
  729. switch(pict_type){
  730. case FF_I_TYPE: return 'I';
  731. case FF_P_TYPE: return 'P';
  732. case FF_B_TYPE: return 'B';
  733. case FF_S_TYPE: return 'S';
  734. case FF_SI_TYPE:return 'i';
  735. case FF_SP_TYPE:return 'p';
  736. case FF_BI_TYPE:return 'b';
  737. default: return '?';
  738. }
  739. }
  740. int av_get_bits_per_sample(enum CodecID codec_id){
  741. switch(codec_id){
  742. case CODEC_ID_ADPCM_SBPRO_2:
  743. return 2;
  744. case CODEC_ID_ADPCM_SBPRO_3:
  745. return 3;
  746. case CODEC_ID_ADPCM_SBPRO_4:
  747. case CODEC_ID_ADPCM_CT:
  748. return 4;
  749. case CODEC_ID_PCM_ALAW:
  750. case CODEC_ID_PCM_MULAW:
  751. case CODEC_ID_PCM_S8:
  752. case CODEC_ID_PCM_U8:
  753. case CODEC_ID_PCM_ZORK:
  754. return 8;
  755. case CODEC_ID_PCM_S16BE:
  756. case CODEC_ID_PCM_S16LE:
  757. case CODEC_ID_PCM_S16LE_PLANAR:
  758. case CODEC_ID_PCM_U16BE:
  759. case CODEC_ID_PCM_U16LE:
  760. return 16;
  761. case CODEC_ID_PCM_S24DAUD:
  762. case CODEC_ID_PCM_S24BE:
  763. case CODEC_ID_PCM_S24LE:
  764. case CODEC_ID_PCM_U24BE:
  765. case CODEC_ID_PCM_U24LE:
  766. return 24;
  767. case CODEC_ID_PCM_S32BE:
  768. case CODEC_ID_PCM_S32LE:
  769. case CODEC_ID_PCM_U32BE:
  770. case CODEC_ID_PCM_U32LE:
  771. case CODEC_ID_PCM_F32BE:
  772. case CODEC_ID_PCM_F32LE:
  773. return 32;
  774. case CODEC_ID_PCM_F64BE:
  775. case CODEC_ID_PCM_F64LE:
  776. return 64;
  777. default:
  778. return 0;
  779. }
  780. }
  781. int av_get_bits_per_sample_format(enum SampleFormat sample_fmt) {
  782. switch (sample_fmt) {
  783. case SAMPLE_FMT_U8:
  784. return 8;
  785. case SAMPLE_FMT_S16:
  786. return 16;
  787. case SAMPLE_FMT_S32:
  788. case SAMPLE_FMT_FLT:
  789. return 32;
  790. case SAMPLE_FMT_DBL:
  791. return 64;
  792. default:
  793. return 0;
  794. }
  795. }
  796. #if !HAVE_THREADS
  797. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  798. return -1;
  799. }
  800. #endif
  801. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  802. {
  803. unsigned int n = 0;
  804. while(v >= 0xff) {
  805. *s++ = 0xff;
  806. v -= 0xff;
  807. n++;
  808. }
  809. *s = v;
  810. n++;
  811. return n;
  812. }
  813. /* Wrapper to work around the lack of mkstemp() on mingw/cygin.
  814. * Also, tries to create file in /tmp first, if possible.
  815. * *prefix can be a character constant; *filename will be allocated internally.
  816. * Returns file descriptor of opened file (or -1 on error)
  817. * and opened file name in **filename. */
  818. int av_tempfile(char *prefix, char **filename) {
  819. int fd=-1;
  820. #if !HAVE_MKSTEMP
  821. *filename = tempnam(".", prefix);
  822. #else
  823. size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */
  824. *filename = av_malloc(len);
  825. #endif
  826. /* -----common section-----*/
  827. if (*filename == NULL) {
  828. av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
  829. return -1;
  830. }
  831. #if !HAVE_MKSTEMP
  832. fd = open(*filename, O_RDWR | O_BINARY | O_CREAT, 0444);
  833. #else
  834. snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
  835. fd = mkstemp(*filename);
  836. if (fd < 0) {
  837. snprintf(*filename, len, "./%sXXXXXX", prefix);
  838. fd = mkstemp(*filename);
  839. }
  840. #endif
  841. /* -----common section-----*/
  842. if (fd < 0) {
  843. av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename);
  844. return -1;
  845. }
  846. return fd; /* success */
  847. }
  848. typedef struct {
  849. const char *abbr;
  850. int width, height;
  851. } VideoFrameSizeAbbr;
  852. typedef struct {
  853. const char *abbr;
  854. int rate_num, rate_den;
  855. } VideoFrameRateAbbr;
  856. static const VideoFrameSizeAbbr video_frame_size_abbrs[] = {
  857. { "ntsc", 720, 480 },
  858. { "pal", 720, 576 },
  859. { "qntsc", 352, 240 }, /* VCD compliant NTSC */
  860. { "qpal", 352, 288 }, /* VCD compliant PAL */
  861. { "sntsc", 640, 480 }, /* square pixel NTSC */
  862. { "spal", 768, 576 }, /* square pixel PAL */
  863. { "film", 352, 240 },
  864. { "ntsc-film", 352, 240 },
  865. { "sqcif", 128, 96 },
  866. { "qcif", 176, 144 },
  867. { "cif", 352, 288 },
  868. { "4cif", 704, 576 },
  869. { "qqvga", 160, 120 },
  870. { "qvga", 320, 240 },
  871. { "vga", 640, 480 },
  872. { "svga", 800, 600 },
  873. { "xga", 1024, 768 },
  874. { "uxga", 1600,1200 },
  875. { "qxga", 2048,1536 },
  876. { "sxga", 1280,1024 },
  877. { "qsxga", 2560,2048 },
  878. { "hsxga", 5120,4096 },
  879. { "wvga", 852, 480 },
  880. { "wxga", 1366, 768 },
  881. { "wsxga", 1600,1024 },
  882. { "wuxga", 1920,1200 },
  883. { "woxga", 2560,1600 },
  884. { "wqsxga", 3200,2048 },
  885. { "wquxga", 3840,2400 },
  886. { "whsxga", 6400,4096 },
  887. { "whuxga", 7680,4800 },
  888. { "cga", 320, 200 },
  889. { "ega", 640, 350 },
  890. { "hd480", 852, 480 },
  891. { "hd720", 1280, 720 },
  892. { "hd1080", 1920,1080 },
  893. };
  894. static const VideoFrameRateAbbr video_frame_rate_abbrs[]= {
  895. { "ntsc", 30000, 1001 },
  896. { "pal", 25, 1 },
  897. { "qntsc", 30000, 1001 }, /* VCD compliant NTSC */
  898. { "qpal", 25, 1 }, /* VCD compliant PAL */
  899. { "sntsc", 30000, 1001 }, /* square pixel NTSC */
  900. { "spal", 25, 1 }, /* square pixel PAL */
  901. { "film", 24, 1 },
  902. { "ntsc-film", 24000, 1001 },
  903. };
  904. int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
  905. {
  906. int i;
  907. int n = FF_ARRAY_ELEMS(video_frame_size_abbrs);
  908. const char *p;
  909. int frame_width = 0, frame_height = 0;
  910. for(i=0;i<n;i++) {
  911. if (!strcmp(video_frame_size_abbrs[i].abbr, str)) {
  912. frame_width = video_frame_size_abbrs[i].width;
  913. frame_height = video_frame_size_abbrs[i].height;
  914. break;
  915. }
  916. }
  917. if (i == n) {
  918. p = str;
  919. frame_width = strtol(p, (char **)&p, 10);
  920. if (*p)
  921. p++;
  922. frame_height = strtol(p, (char **)&p, 10);
  923. }
  924. if (frame_width <= 0 || frame_height <= 0)
  925. return -1;
  926. *width_ptr = frame_width;
  927. *height_ptr = frame_height;
  928. return 0;
  929. }
  930. int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
  931. {
  932. int i;
  933. int n = FF_ARRAY_ELEMS(video_frame_rate_abbrs);
  934. char* cp;
  935. /* First, we check our abbreviation table */
  936. for (i = 0; i < n; ++i)
  937. if (!strcmp(video_frame_rate_abbrs[i].abbr, arg)) {
  938. frame_rate->num = video_frame_rate_abbrs[i].rate_num;
  939. frame_rate->den = video_frame_rate_abbrs[i].rate_den;
  940. return 0;
  941. }
  942. /* Then, we try to parse it as fraction */
  943. cp = strchr(arg, '/');
  944. if (!cp)
  945. cp = strchr(arg, ':');
  946. if (cp) {
  947. char* cpp;
  948. frame_rate->num = strtol(arg, &cpp, 10);
  949. if (cpp != arg || cpp == cp)
  950. frame_rate->den = strtol(cp+1, &cpp, 10);
  951. else
  952. frame_rate->num = 0;
  953. }
  954. else {
  955. /* Finally we give up and parse it as double */
  956. AVRational time_base = av_d2q(strtod(arg, 0), 1001000);
  957. frame_rate->den = time_base.den;
  958. frame_rate->num = time_base.num;
  959. }
  960. if (!frame_rate->num || !frame_rate->den)
  961. return -1;
  962. else
  963. return 0;
  964. }
  965. void ff_log_missing_feature(void *avc, const char *feature, int want_sample)
  966. {
  967. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  968. "version to the newest one from SVN. If the problem still "
  969. "occurs, it means that your file has a feature which has not "
  970. "been implemented.", feature);
  971. if(want_sample)
  972. ff_log_ask_for_sample(avc, NULL);
  973. else
  974. av_log(avc, AV_LOG_WARNING, "\n");
  975. }
  976. void ff_log_ask_for_sample(void *avc, const char *msg)
  977. {
  978. if (msg)
  979. av_log(avc, AV_LOG_WARNING, "%s ", msg);
  980. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  981. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  982. "and contact the ffmpeg-devel mailing list.\n");
  983. }