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.

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