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.

1216 lines
36KB

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