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.

1155 lines
34KB

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