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.

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