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.

2370 lines
74KB

  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
  24. * utils.
  25. */
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/audioconvert.h"
  32. #include "libavutil/imgutils.h"
  33. #include "libavutil/samplefmt.h"
  34. #include "libavutil/dict.h"
  35. #include "libavutil/avassert.h"
  36. #include "avcodec.h"
  37. #include "dsputil.h"
  38. #include "libavutil/opt.h"
  39. #include "imgconvert.h"
  40. #include "thread.h"
  41. #include "audioconvert.h"
  42. #include "internal.h"
  43. #include "bytestream.h"
  44. #include <stdlib.h>
  45. #include <stdarg.h>
  46. #include <limits.h>
  47. #include <float.h>
  48. static int volatile entangled_thread_counter=0;
  49. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  50. static void *codec_mutex;
  51. static void *avformat_mutex;
  52. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  53. {
  54. if(min_size < *size)
  55. return ptr;
  56. min_size= FFMAX(17*min_size/16 + 32, min_size);
  57. ptr= av_realloc(ptr, min_size);
  58. 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
  59. min_size= 0;
  60. *size= min_size;
  61. return ptr;
  62. }
  63. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  64. {
  65. void **p = ptr;
  66. if (min_size < *size)
  67. return 0;
  68. min_size= FFMAX(17*min_size/16 + 32, min_size);
  69. av_free(*p);
  70. *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  71. if (!*p) min_size = 0;
  72. *size= min_size;
  73. return 1;
  74. }
  75. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  76. {
  77. ff_fast_malloc(ptr, size, min_size, 0);
  78. }
  79. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  80. {
  81. uint8_t **p = ptr;
  82. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  83. av_freep(p);
  84. *size = 0;
  85. return;
  86. }
  87. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  88. memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  89. }
  90. /* encoder management */
  91. static AVCodec *first_avcodec = NULL;
  92. AVCodec *av_codec_next(AVCodec *c){
  93. if(c) return c->next;
  94. else return first_avcodec;
  95. }
  96. static void avcodec_init(void)
  97. {
  98. static int initialized = 0;
  99. if (initialized != 0)
  100. return;
  101. initialized = 1;
  102. ff_dsputil_static_init();
  103. }
  104. int av_codec_is_encoder(AVCodec *codec)
  105. {
  106. return codec && (codec->encode || codec->encode2);
  107. }
  108. int av_codec_is_decoder(AVCodec *codec)
  109. {
  110. return codec && codec->decode;
  111. }
  112. void avcodec_register(AVCodec *codec)
  113. {
  114. AVCodec **p;
  115. avcodec_init();
  116. p = &first_avcodec;
  117. while (*p != NULL) p = &(*p)->next;
  118. *p = codec;
  119. codec->next = NULL;
  120. if (codec->init_static_data)
  121. codec->init_static_data(codec);
  122. }
  123. unsigned avcodec_get_edge_width(void)
  124. {
  125. return EDGE_WIDTH;
  126. }
  127. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  128. s->coded_width = width;
  129. s->coded_height= height;
  130. s->width = -((-width )>>s->lowres);
  131. s->height= -((-height)>>s->lowres);
  132. }
  133. #define INTERNAL_BUFFER_SIZE (32+1)
  134. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  135. int linesize_align[AV_NUM_DATA_POINTERS])
  136. {
  137. int i;
  138. int w_align= 1;
  139. int h_align= 1;
  140. switch(s->pix_fmt){
  141. case PIX_FMT_YUV420P:
  142. case PIX_FMT_YUYV422:
  143. case PIX_FMT_UYVY422:
  144. case PIX_FMT_YUV422P:
  145. case PIX_FMT_YUV440P:
  146. case PIX_FMT_YUV444P:
  147. case PIX_FMT_GBRP:
  148. case PIX_FMT_GRAY8:
  149. case PIX_FMT_GRAY16BE:
  150. case PIX_FMT_GRAY16LE:
  151. case PIX_FMT_YUVJ420P:
  152. case PIX_FMT_YUVJ422P:
  153. case PIX_FMT_YUVJ440P:
  154. case PIX_FMT_YUVJ444P:
  155. case PIX_FMT_YUVA420P:
  156. case PIX_FMT_YUVA444P:
  157. case PIX_FMT_YUV420P9LE:
  158. case PIX_FMT_YUV420P9BE:
  159. case PIX_FMT_YUV420P10LE:
  160. case PIX_FMT_YUV420P10BE:
  161. case PIX_FMT_YUV422P9LE:
  162. case PIX_FMT_YUV422P9BE:
  163. case PIX_FMT_YUV422P10LE:
  164. case PIX_FMT_YUV422P10BE:
  165. case PIX_FMT_YUV444P9LE:
  166. case PIX_FMT_YUV444P9BE:
  167. case PIX_FMT_YUV444P10LE:
  168. case PIX_FMT_YUV444P10BE:
  169. case PIX_FMT_GBRP9LE:
  170. case PIX_FMT_GBRP9BE:
  171. case PIX_FMT_GBRP10LE:
  172. case PIX_FMT_GBRP10BE:
  173. w_align = 16; //FIXME assume 16 pixel per macroblock
  174. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  175. break;
  176. case PIX_FMT_YUV411P:
  177. case PIX_FMT_UYYVYY411:
  178. w_align=32;
  179. h_align=8;
  180. break;
  181. case PIX_FMT_YUV410P:
  182. if(s->codec_id == CODEC_ID_SVQ1){
  183. w_align=64;
  184. h_align=64;
  185. }
  186. case PIX_FMT_RGB555:
  187. if(s->codec_id == CODEC_ID_RPZA){
  188. w_align=4;
  189. h_align=4;
  190. }
  191. case PIX_FMT_PAL8:
  192. case PIX_FMT_BGR8:
  193. case PIX_FMT_RGB8:
  194. if(s->codec_id == CODEC_ID_SMC){
  195. w_align=4;
  196. h_align=4;
  197. }
  198. break;
  199. case PIX_FMT_BGR24:
  200. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  201. w_align=4;
  202. h_align=4;
  203. }
  204. break;
  205. default:
  206. w_align= 1;
  207. h_align= 1;
  208. break;
  209. }
  210. if(s->codec_id == CODEC_ID_IFF_ILBM || s->codec_id == CODEC_ID_IFF_BYTERUN1){
  211. w_align= FFMAX(w_align, 8);
  212. }
  213. *width = FFALIGN(*width , w_align);
  214. *height= FFALIGN(*height, h_align);
  215. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  216. *height+=2; // some of the optimized chroma MC reads one line too much
  217. // which is also done in mpeg decoders with lowres > 0
  218. for (i = 0; i < 4; i++)
  219. linesize_align[i] = STRIDE_ALIGN;
  220. }
  221. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  222. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  223. int linesize_align[AV_NUM_DATA_POINTERS];
  224. int align;
  225. avcodec_align_dimensions2(s, width, height, linesize_align);
  226. align = FFMAX(linesize_align[0], linesize_align[3]);
  227. linesize_align[1] <<= chroma_shift;
  228. linesize_align[2] <<= chroma_shift;
  229. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  230. *width=FFALIGN(*width, align);
  231. }
  232. void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
  233. {
  234. if (s->pkt) {
  235. pic->pkt_pts = s->pkt->pts;
  236. pic->pkt_pos = s->pkt->pos;
  237. } else {
  238. pic->pkt_pts = AV_NOPTS_VALUE;
  239. pic->pkt_pos = -1;
  240. }
  241. pic->reordered_opaque= s->reordered_opaque;
  242. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  243. pic->width = s->width;
  244. pic->height = s->height;
  245. pic->format = s->pix_fmt;
  246. }
  247. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  248. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  249. int buf_size, int align)
  250. {
  251. int ch, planar, needed_size, ret = 0;
  252. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  253. frame->nb_samples, sample_fmt,
  254. align);
  255. if (buf_size < needed_size)
  256. return AVERROR(EINVAL);
  257. planar = av_sample_fmt_is_planar(sample_fmt);
  258. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  259. if (!(frame->extended_data = av_mallocz(nb_channels *
  260. sizeof(*frame->extended_data))))
  261. return AVERROR(ENOMEM);
  262. } else {
  263. frame->extended_data = frame->data;
  264. }
  265. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  266. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  267. sample_fmt, align)) < 0) {
  268. if (frame->extended_data != frame->data)
  269. av_freep(&frame->extended_data);
  270. return ret;
  271. }
  272. if (frame->extended_data != frame->data) {
  273. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  274. frame->data[ch] = frame->extended_data[ch];
  275. }
  276. return ret;
  277. }
  278. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  279. {
  280. AVCodecInternal *avci = avctx->internal;
  281. InternalBuffer *buf;
  282. int buf_size, ret;
  283. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  284. frame->nb_samples, avctx->sample_fmt,
  285. 0);
  286. if (buf_size < 0)
  287. return AVERROR(EINVAL);
  288. /* allocate InternalBuffer if needed */
  289. if (!avci->buffer) {
  290. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  291. if (!avci->buffer)
  292. return AVERROR(ENOMEM);
  293. }
  294. buf = avci->buffer;
  295. /* if there is a previously-used internal buffer, check its size and
  296. channel count to see if we can reuse it */
  297. if (buf->extended_data) {
  298. /* if current buffer is too small, free it */
  299. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  300. av_free(buf->extended_data[0]);
  301. if (buf->extended_data != buf->data)
  302. av_freep(&buf->extended_data);
  303. buf->extended_data = NULL;
  304. buf->data[0] = NULL;
  305. }
  306. /* if number of channels has changed, reset and/or free extended data
  307. pointers but leave data buffer in buf->data[0] for reuse */
  308. if (buf->nb_channels != avctx->channels) {
  309. if (buf->extended_data != buf->data)
  310. av_free(buf->extended_data);
  311. buf->extended_data = NULL;
  312. }
  313. }
  314. /* if there is no previous buffer or the previous buffer cannot be used
  315. as-is, allocate a new buffer and/or rearrange the channel pointers */
  316. if (!buf->extended_data) {
  317. if (!buf->data[0]) {
  318. if (!(buf->data[0] = av_mallocz(buf_size)))
  319. return AVERROR(ENOMEM);
  320. buf->audio_data_size = buf_size;
  321. }
  322. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  323. avctx->sample_fmt, buf->data[0],
  324. buf->audio_data_size, 0)))
  325. return ret;
  326. if (frame->extended_data == frame->data)
  327. buf->extended_data = buf->data;
  328. else
  329. buf->extended_data = frame->extended_data;
  330. memcpy(buf->data, frame->data, sizeof(frame->data));
  331. buf->linesize[0] = frame->linesize[0];
  332. buf->nb_channels = avctx->channels;
  333. } else {
  334. /* copy InternalBuffer info to the AVFrame */
  335. frame->extended_data = buf->extended_data;
  336. frame->linesize[0] = buf->linesize[0];
  337. memcpy(frame->data, buf->data, sizeof(frame->data));
  338. }
  339. frame->type = FF_BUFFER_TYPE_INTERNAL;
  340. if (avctx->pkt) {
  341. frame->pkt_pts = avctx->pkt->pts;
  342. frame->pkt_pos = avctx->pkt->pos;
  343. } else {
  344. frame->pkt_pts = AV_NOPTS_VALUE;
  345. frame->pkt_pos = -1;
  346. }
  347. frame->reordered_opaque = avctx->reordered_opaque;
  348. frame->sample_rate = avctx->sample_rate;
  349. frame->format = avctx->sample_fmt;
  350. frame->channel_layout = avctx->channel_layout;
  351. if (avctx->debug & FF_DEBUG_BUFFERS)
  352. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  353. "internal audio buffer used\n", frame);
  354. return 0;
  355. }
  356. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  357. {
  358. int i;
  359. int w= s->width;
  360. int h= s->height;
  361. InternalBuffer *buf;
  362. AVCodecInternal *avci = s->internal;
  363. if(pic->data[0]!=NULL) {
  364. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  365. return -1;
  366. }
  367. if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  368. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  369. return -1;
  370. }
  371. if(av_image_check_size(w, h, 0, s) || s->pix_fmt<0)
  372. return -1;
  373. if (!avci->buffer) {
  374. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
  375. sizeof(InternalBuffer));
  376. }
  377. buf = &avci->buffer[avci->buffer_count];
  378. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  379. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  380. av_freep(&buf->base[i]);
  381. buf->data[i]= NULL;
  382. }
  383. }
  384. if (!buf->base[0]) {
  385. int h_chroma_shift, v_chroma_shift;
  386. int size[4] = {0};
  387. int tmpsize;
  388. int unaligned;
  389. AVPicture picture;
  390. int stride_align[AV_NUM_DATA_POINTERS];
  391. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  392. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  393. avcodec_align_dimensions2(s, &w, &h, stride_align);
  394. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  395. w+= EDGE_WIDTH*2;
  396. h+= EDGE_WIDTH*2;
  397. }
  398. do {
  399. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  400. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  401. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  402. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  403. w += w & ~(w-1);
  404. unaligned = 0;
  405. for (i=0; i<4; i++){
  406. unaligned |= picture.linesize[i] % stride_align[i];
  407. }
  408. } while (unaligned);
  409. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  410. if (tmpsize < 0)
  411. return -1;
  412. for (i=0; i<3 && picture.data[i+1]; i++)
  413. size[i] = picture.data[i+1] - picture.data[i];
  414. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  415. memset(buf->base, 0, sizeof(buf->base));
  416. memset(buf->data, 0, sizeof(buf->data));
  417. for(i=0; i<4 && size[i]; i++){
  418. const int h_shift= i==0 ? 0 : h_chroma_shift;
  419. const int v_shift= i==0 ? 0 : v_chroma_shift;
  420. buf->linesize[i]= picture.linesize[i];
  421. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  422. if(buf->base[i]==NULL) return -1;
  423. memset(buf->base[i], 128, size[i]);
  424. // no edge if EDGE EMU or not planar YUV
  425. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  426. buf->data[i] = buf->base[i];
  427. else
  428. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
  429. }
  430. for (; i < AV_NUM_DATA_POINTERS; i++) {
  431. buf->base[i] = buf->data[i] = NULL;
  432. buf->linesize[i] = 0;
  433. }
  434. if(size[1] && !size[2])
  435. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  436. buf->width = s->width;
  437. buf->height = s->height;
  438. buf->pix_fmt= s->pix_fmt;
  439. }
  440. pic->type= FF_BUFFER_TYPE_INTERNAL;
  441. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  442. pic->base[i]= buf->base[i];
  443. pic->data[i]= buf->data[i];
  444. pic->linesize[i]= buf->linesize[i];
  445. }
  446. pic->extended_data = pic->data;
  447. avci->buffer_count++;
  448. pic->width = buf->width;
  449. pic->height = buf->height;
  450. pic->format = buf->pix_fmt;
  451. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  452. if (s->pkt) {
  453. pic->pkt_pts = s->pkt->pts;
  454. pic->pkt_pos = s->pkt->pos;
  455. } else {
  456. pic->pkt_pts = AV_NOPTS_VALUE;
  457. pic->pkt_pos = -1;
  458. }
  459. pic->reordered_opaque= s->reordered_opaque;
  460. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  461. pic->width = s->width;
  462. pic->height = s->height;
  463. pic->format = s->pix_fmt;
  464. if(s->debug&FF_DEBUG_BUFFERS)
  465. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  466. "buffers used\n", pic, avci->buffer_count);
  467. return 0;
  468. }
  469. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  470. {
  471. switch (avctx->codec_type) {
  472. case AVMEDIA_TYPE_VIDEO:
  473. return video_get_buffer(avctx, frame);
  474. case AVMEDIA_TYPE_AUDIO:
  475. return audio_get_buffer(avctx, frame);
  476. default:
  477. return -1;
  478. }
  479. }
  480. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  481. int i;
  482. InternalBuffer *buf, *last;
  483. AVCodecInternal *avci = s->internal;
  484. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  485. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  486. assert(avci->buffer_count);
  487. if (avci->buffer) {
  488. buf = NULL; /* avoids warning */
  489. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  490. buf = &avci->buffer[i];
  491. if (buf->data[0] == pic->data[0])
  492. break;
  493. }
  494. assert(i < avci->buffer_count);
  495. avci->buffer_count--;
  496. last = &avci->buffer[avci->buffer_count];
  497. if (buf != last)
  498. FFSWAP(InternalBuffer, *buf, *last);
  499. }
  500. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  501. pic->data[i]=NULL;
  502. // pic->base[i]=NULL;
  503. }
  504. //printf("R%X\n", pic->opaque);
  505. if(s->debug&FF_DEBUG_BUFFERS)
  506. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  507. "buffers used\n", pic, avci->buffer_count);
  508. }
  509. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  510. AVFrame temp_pic;
  511. int i;
  512. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  513. if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
  514. av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  515. pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
  516. s->release_buffer(s, pic);
  517. }
  518. ff_init_buffer_info(s, pic);
  519. /* If no picture return a new buffer */
  520. if(pic->data[0] == NULL) {
  521. /* We will copy from buffer, so must be readable */
  522. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  523. return s->get_buffer(s, pic);
  524. }
  525. assert(s->pix_fmt == pic->format);
  526. /* If internal buffer type return the same buffer */
  527. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  528. return 0;
  529. }
  530. /*
  531. * Not internal type and reget_buffer not overridden, emulate cr buffer
  532. */
  533. temp_pic = *pic;
  534. for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
  535. pic->data[i] = pic->base[i] = NULL;
  536. pic->opaque = NULL;
  537. /* Allocate new frame */
  538. if (s->get_buffer(s, pic))
  539. return -1;
  540. /* Copy image data from old buffer to new buffer */
  541. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  542. s->height);
  543. s->release_buffer(s, &temp_pic); // Release old frame
  544. return 0;
  545. }
  546. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  547. int i;
  548. for(i=0; i<count; i++){
  549. int r= func(c, (char*)arg + i*size);
  550. if(ret) ret[i]= r;
  551. }
  552. return 0;
  553. }
  554. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  555. int i;
  556. for(i=0; i<count; i++){
  557. int r= func(c, arg, i, 0);
  558. if(ret) ret[i]= r;
  559. }
  560. return 0;
  561. }
  562. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  563. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  564. ++fmt;
  565. return fmt[0];
  566. }
  567. void avcodec_get_frame_defaults(AVFrame *pic){
  568. memset(pic, 0, sizeof(AVFrame));
  569. pic->pts = pic->pkt_dts = pic->pkt_pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
  570. pic->pkt_pos = -1;
  571. pic->key_frame= 1;
  572. pic->sample_aspect_ratio = (AVRational){0, 1};
  573. pic->format = -1; /* unknown */
  574. }
  575. AVFrame *avcodec_alloc_frame(void){
  576. AVFrame *pic= av_malloc(sizeof(AVFrame));
  577. if(pic==NULL) return NULL;
  578. avcodec_get_frame_defaults(pic);
  579. return pic;
  580. }
  581. #define MAKE_ACCESSORS(str, name, type, field) \
  582. type av_##name##_get_##field(const str *s) { return s->field; } \
  583. void av_##name##_set_##field(str *s, type v) { s->field = v; }
  584. MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
  585. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
  586. MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
  587. MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
  588. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  589. {
  590. memset(sub, 0, sizeof(*sub));
  591. sub->pts = AV_NOPTS_VALUE;
  592. }
  593. static int get_bit_rate(AVCodecContext *ctx)
  594. {
  595. int bit_rate;
  596. int bits_per_sample;
  597. switch(ctx->codec_type) {
  598. case AVMEDIA_TYPE_VIDEO:
  599. case AVMEDIA_TYPE_DATA:
  600. case AVMEDIA_TYPE_SUBTITLE:
  601. case AVMEDIA_TYPE_ATTACHMENT:
  602. bit_rate = ctx->bit_rate;
  603. break;
  604. case AVMEDIA_TYPE_AUDIO:
  605. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  606. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  607. break;
  608. default:
  609. bit_rate = 0;
  610. break;
  611. }
  612. return bit_rate;
  613. }
  614. #if FF_API_AVCODEC_OPEN
  615. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  616. {
  617. return avcodec_open2(avctx, codec, NULL);
  618. }
  619. #endif
  620. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  621. {
  622. int ret = 0;
  623. AVDictionary *tmp = NULL;
  624. if (avcodec_is_open(avctx))
  625. return 0;
  626. if ((!codec && !avctx->codec)) {
  627. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  628. return AVERROR(EINVAL);
  629. }
  630. if ((codec && avctx->codec && codec != avctx->codec)) {
  631. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  632. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  633. return AVERROR(EINVAL);
  634. }
  635. if (!codec)
  636. codec = avctx->codec;
  637. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  638. return AVERROR(EINVAL);
  639. if (options)
  640. av_dict_copy(&tmp, *options, 0);
  641. /* If there is a user-supplied mutex locking routine, call it. */
  642. if (ff_lockmgr_cb) {
  643. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  644. return -1;
  645. }
  646. entangled_thread_counter++;
  647. if(entangled_thread_counter != 1){
  648. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  649. ret = -1;
  650. goto end;
  651. }
  652. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  653. if (!avctx->internal) {
  654. ret = AVERROR(ENOMEM);
  655. goto end;
  656. }
  657. if (codec->priv_data_size > 0) {
  658. if(!avctx->priv_data){
  659. avctx->priv_data = av_mallocz(codec->priv_data_size);
  660. if (!avctx->priv_data) {
  661. ret = AVERROR(ENOMEM);
  662. goto end;
  663. }
  664. if (codec->priv_class) {
  665. *(const AVClass**)avctx->priv_data= codec->priv_class;
  666. av_opt_set_defaults(avctx->priv_data);
  667. }
  668. }
  669. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  670. goto free_and_end;
  671. } else {
  672. avctx->priv_data = NULL;
  673. }
  674. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  675. goto free_and_end;
  676. if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
  677. if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  678. av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, see -strict -2\n");
  679. ret = -1;
  680. goto free_and_end;
  681. }
  682. //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
  683. if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
  684. if(avctx->coded_width && avctx->coded_height)
  685. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  686. else if(avctx->width && avctx->height)
  687. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  688. }
  689. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  690. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  691. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  692. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  693. avcodec_set_dimensions(avctx, 0, 0);
  694. }
  695. /* if the decoder init function was already called previously,
  696. free the already allocated subtitle_header before overwriting it */
  697. if (av_codec_is_decoder(codec))
  698. av_freep(&avctx->subtitle_header);
  699. #define SANE_NB_CHANNELS 128U
  700. if (avctx->channels > SANE_NB_CHANNELS) {
  701. ret = AVERROR(EINVAL);
  702. goto free_and_end;
  703. }
  704. avctx->codec = codec;
  705. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  706. avctx->codec_id == CODEC_ID_NONE) {
  707. avctx->codec_type = codec->type;
  708. avctx->codec_id = codec->id;
  709. }
  710. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  711. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  712. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  713. ret = AVERROR(EINVAL);
  714. goto free_and_end;
  715. }
  716. avctx->frame_number = 0;
  717. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  718. (!avctx->time_base.num || !avctx->time_base.den)) {
  719. avctx->time_base.num = 1;
  720. avctx->time_base.den = avctx->sample_rate;
  721. }
  722. if (!HAVE_THREADS)
  723. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  724. if (HAVE_THREADS && !avctx->thread_opaque) {
  725. ret = ff_thread_init(avctx);
  726. if (ret < 0) {
  727. goto free_and_end;
  728. }
  729. }
  730. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  731. avctx->thread_count = 1;
  732. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  733. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  734. avctx->codec->max_lowres);
  735. ret = AVERROR(EINVAL);
  736. goto free_and_end;
  737. }
  738. if (av_codec_is_encoder(avctx->codec)) {
  739. int i;
  740. if (avctx->codec->sample_fmts) {
  741. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  742. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  743. break;
  744. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  745. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  746. ret = AVERROR(EINVAL);
  747. goto free_and_end;
  748. }
  749. }
  750. if (avctx->codec->pix_fmts) {
  751. for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
  752. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  753. break;
  754. if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE) {
  755. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  756. ret = AVERROR(EINVAL);
  757. goto free_and_end;
  758. }
  759. }
  760. if (avctx->codec->supported_samplerates) {
  761. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  762. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  763. break;
  764. if (avctx->codec->supported_samplerates[i] == 0) {
  765. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  766. ret = AVERROR(EINVAL);
  767. goto free_and_end;
  768. }
  769. }
  770. if (avctx->codec->channel_layouts) {
  771. if (!avctx->channel_layout) {
  772. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  773. } else {
  774. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  775. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  776. break;
  777. if (avctx->codec->channel_layouts[i] == 0) {
  778. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  779. ret = AVERROR(EINVAL);
  780. goto free_and_end;
  781. }
  782. }
  783. }
  784. if (avctx->channel_layout && avctx->channels) {
  785. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  786. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  787. ret = AVERROR(EINVAL);
  788. goto free_and_end;
  789. }
  790. } else if (avctx->channel_layout) {
  791. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  792. }
  793. }
  794. avctx->pts_correction_num_faulty_pts =
  795. avctx->pts_correction_num_faulty_dts = 0;
  796. avctx->pts_correction_last_pts =
  797. avctx->pts_correction_last_dts = INT64_MIN;
  798. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  799. ret = avctx->codec->init(avctx);
  800. if (ret < 0) {
  801. goto free_and_end;
  802. }
  803. }
  804. ret=0;
  805. if (av_codec_is_decoder(avctx->codec)) {
  806. if (!avctx->bit_rate)
  807. avctx->bit_rate = get_bit_rate(avctx);
  808. /* validate channel layout from the decoder */
  809. if (avctx->channel_layout &&
  810. av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  811. av_log(avctx, AV_LOG_WARNING, "channel layout does not match number of channels\n");
  812. avctx->channel_layout = 0;
  813. }
  814. }
  815. end:
  816. entangled_thread_counter--;
  817. /* Release any user-supplied mutex. */
  818. if (ff_lockmgr_cb) {
  819. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  820. }
  821. if (options) {
  822. av_dict_free(options);
  823. *options = tmp;
  824. }
  825. return ret;
  826. free_and_end:
  827. av_dict_free(&tmp);
  828. av_freep(&avctx->priv_data);
  829. av_freep(&avctx->internal);
  830. avctx->codec= NULL;
  831. goto end;
  832. }
  833. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
  834. {
  835. if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  836. av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
  837. return AVERROR(EINVAL);
  838. }
  839. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  840. if (!avpkt->data || avpkt->size < size) {
  841. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  842. avpkt->data = avctx->internal->byte_buffer;
  843. avpkt->size = avctx->internal->byte_buffer_size;
  844. avpkt->destruct = NULL;
  845. }
  846. if (avpkt->data) {
  847. void *destruct = avpkt->destruct;
  848. if (avpkt->size < size) {
  849. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
  850. return AVERROR(EINVAL);
  851. }
  852. av_init_packet(avpkt);
  853. avpkt->destruct = destruct;
  854. avpkt->size = size;
  855. return 0;
  856. } else {
  857. int ret = av_new_packet(avpkt, size);
  858. if (ret < 0)
  859. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
  860. return ret;
  861. }
  862. }
  863. int ff_alloc_packet(AVPacket *avpkt, int size)
  864. {
  865. return ff_alloc_packet2(NULL, avpkt, size);
  866. }
  867. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  868. AVPacket *avpkt,
  869. const AVFrame *frame,
  870. int *got_packet_ptr)
  871. {
  872. int ret;
  873. AVPacket user_pkt = *avpkt;
  874. int nb_samples;
  875. int needs_realloc = !user_pkt.data;
  876. *got_packet_ptr = 0;
  877. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  878. av_free_packet(avpkt);
  879. av_init_packet(avpkt);
  880. avpkt->size = 0;
  881. return 0;
  882. }
  883. /* check for valid frame size */
  884. if (frame) {
  885. nb_samples = frame->nb_samples;
  886. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  887. if (nb_samples > avctx->frame_size)
  888. return AVERROR(EINVAL);
  889. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  890. if (nb_samples != avctx->frame_size)
  891. return AVERROR(EINVAL);
  892. }
  893. } else {
  894. nb_samples = avctx->frame_size;
  895. }
  896. if (avctx->codec->encode2) {
  897. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  898. if (!ret && *got_packet_ptr) {
  899. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  900. if (avpkt->pts == AV_NOPTS_VALUE)
  901. avpkt->pts = frame->pts;
  902. if (!avpkt->duration)
  903. avpkt->duration = ff_samples_to_time_base(avctx,
  904. frame->nb_samples);
  905. }
  906. avpkt->dts = avpkt->pts;
  907. } else {
  908. avpkt->size = 0;
  909. }
  910. } else {
  911. /* for compatibility with encoders not supporting encode2(), we need to
  912. allocate a packet buffer if the user has not provided one or check
  913. the size otherwise */
  914. int fs_tmp = 0;
  915. int buf_size = avpkt->size;
  916. if (!user_pkt.data) {
  917. if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
  918. av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
  919. if (!frame)
  920. return AVERROR(EINVAL);
  921. buf_size = nb_samples * avctx->channels *
  922. av_get_bits_per_sample(avctx->codec_id) / 8;
  923. } else {
  924. /* this is a guess as to the required size.
  925. if an encoder needs more than this, it should probably
  926. implement encode2() */
  927. buf_size = 2 * avctx->frame_size * avctx->channels *
  928. av_get_bytes_per_sample(avctx->sample_fmt);
  929. buf_size += 2*FF_MIN_BUFFER_SIZE;
  930. }
  931. }
  932. if ((ret = ff_alloc_packet2(avctx, avpkt, buf_size)))
  933. return ret;
  934. /* Encoders using AVCodec.encode() that support
  935. CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
  936. the smaller size when encoding the last frame.
  937. This code can be removed once all encoders supporting
  938. CODEC_CAP_SMALL_LAST_FRAME use encode2() */
  939. if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
  940. nb_samples < avctx->frame_size) {
  941. fs_tmp = avctx->frame_size;
  942. avctx->frame_size = nb_samples;
  943. }
  944. /* encode the frame */
  945. ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
  946. frame ? frame->data[0] : NULL);
  947. if (ret >= 0) {
  948. if (!ret) {
  949. /* no output. if the packet data was allocated by libavcodec,
  950. free it */
  951. if (!user_pkt.data && avpkt->data != avctx->internal->byte_buffer)
  952. av_freep(&avpkt->data);
  953. } else {
  954. if (avctx->coded_frame)
  955. avpkt->pts = avpkt->dts = avctx->coded_frame->pts;
  956. /* Set duration for final small packet. This can be removed
  957. once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
  958. encode2() */
  959. if (fs_tmp) {
  960. avpkt->duration = ff_samples_to_time_base(avctx,
  961. avctx->frame_size);
  962. }
  963. }
  964. avpkt->size = ret;
  965. *got_packet_ptr = (ret > 0);
  966. ret = 0;
  967. }
  968. if (fs_tmp)
  969. avctx->frame_size = fs_tmp;
  970. }
  971. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  972. needs_realloc = 0;
  973. if (user_pkt.data) {
  974. if (user_pkt.size >= avpkt->size) {
  975. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  976. } else {
  977. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  978. avpkt->size = user_pkt.size;
  979. ret = -1;
  980. }
  981. avpkt->data = user_pkt.data;
  982. avpkt->destruct = user_pkt.destruct;
  983. } else {
  984. if (av_dup_packet(avpkt) < 0) {
  985. ret = AVERROR(ENOMEM);
  986. }
  987. }
  988. }
  989. if (!ret) {
  990. if (needs_realloc && avpkt->data) {
  991. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  992. if (new_data)
  993. avpkt->data = new_data;
  994. }
  995. avctx->frame_number++;
  996. }
  997. if (ret < 0 || !*got_packet_ptr)
  998. av_free_packet(avpkt);
  999. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1000. this needs to be moved to the encoders, but for now we can do it
  1001. here to simplify things */
  1002. avpkt->flags |= AV_PKT_FLAG_KEY;
  1003. return ret;
  1004. }
  1005. #if FF_API_OLD_DECODE_AUDIO
  1006. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1007. uint8_t *buf, int buf_size,
  1008. const short *samples)
  1009. {
  1010. AVPacket pkt;
  1011. AVFrame frame0;
  1012. AVFrame *frame;
  1013. int ret, samples_size, got_packet;
  1014. av_init_packet(&pkt);
  1015. pkt.data = buf;
  1016. pkt.size = buf_size;
  1017. if (samples) {
  1018. frame = &frame0;
  1019. avcodec_get_frame_defaults(frame);
  1020. if (avctx->frame_size) {
  1021. frame->nb_samples = avctx->frame_size;
  1022. } else {
  1023. /* if frame_size is not set, the number of samples must be
  1024. calculated from the buffer size */
  1025. int64_t nb_samples;
  1026. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1027. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1028. "support this codec\n");
  1029. return AVERROR(EINVAL);
  1030. }
  1031. nb_samples = (int64_t)buf_size * 8 /
  1032. (av_get_bits_per_sample(avctx->codec_id) *
  1033. avctx->channels);
  1034. if (nb_samples >= INT_MAX)
  1035. return AVERROR(EINVAL);
  1036. frame->nb_samples = nb_samples;
  1037. }
  1038. /* it is assumed that the samples buffer is large enough based on the
  1039. relevant parameters */
  1040. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1041. frame->nb_samples,
  1042. avctx->sample_fmt, 1);
  1043. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1044. avctx->sample_fmt,
  1045. (const uint8_t *)samples, samples_size, 1)))
  1046. return ret;
  1047. /* fabricate frame pts from sample count.
  1048. this is needed because the avcodec_encode_audio() API does not have
  1049. a way for the user to provide pts */
  1050. if(avctx->sample_rate && avctx->time_base.num)
  1051. frame->pts = ff_samples_to_time_base(avctx,
  1052. avctx->internal->sample_count);
  1053. else
  1054. frame->pts = AV_NOPTS_VALUE;
  1055. avctx->internal->sample_count += frame->nb_samples;
  1056. } else {
  1057. frame = NULL;
  1058. }
  1059. got_packet = 0;
  1060. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1061. if (!ret && got_packet && avctx->coded_frame) {
  1062. avctx->coded_frame->pts = pkt.pts;
  1063. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1064. }
  1065. /* free any side data since we cannot return it */
  1066. ff_packet_free_side_data(&pkt);
  1067. if (frame && frame->extended_data != frame->data)
  1068. av_freep(&frame->extended_data);
  1069. return ret ? ret : pkt.size;
  1070. }
  1071. #endif
  1072. #if FF_API_OLD_ENCODE_VIDEO
  1073. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1074. const AVFrame *pict)
  1075. {
  1076. AVPacket pkt;
  1077. int ret, got_packet = 0;
  1078. if(buf_size < FF_MIN_BUFFER_SIZE){
  1079. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1080. return -1;
  1081. }
  1082. av_init_packet(&pkt);
  1083. pkt.data = buf;
  1084. pkt.size = buf_size;
  1085. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1086. if (!ret && got_packet && avctx->coded_frame) {
  1087. avctx->coded_frame->pts = pkt.pts;
  1088. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1089. }
  1090. /* free any side data since we cannot return it */
  1091. if (pkt.side_data_elems > 0) {
  1092. int i;
  1093. for (i = 0; i < pkt.side_data_elems; i++)
  1094. av_free(pkt.side_data[i].data);
  1095. av_freep(&pkt.side_data);
  1096. pkt.side_data_elems = 0;
  1097. }
  1098. return ret ? ret : pkt.size;
  1099. }
  1100. #endif
  1101. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1102. AVPacket *avpkt,
  1103. const AVFrame *frame,
  1104. int *got_packet_ptr)
  1105. {
  1106. int ret;
  1107. AVPacket user_pkt = *avpkt;
  1108. int needs_realloc = !user_pkt.data;
  1109. *got_packet_ptr = 0;
  1110. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1111. av_free_packet(avpkt);
  1112. av_init_packet(avpkt);
  1113. avpkt->size = 0;
  1114. return 0;
  1115. }
  1116. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1117. return AVERROR(EINVAL);
  1118. av_assert0(avctx->codec->encode2);
  1119. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1120. av_assert0(ret <= 0);
  1121. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1122. needs_realloc = 0;
  1123. if (user_pkt.data) {
  1124. if (user_pkt.size >= avpkt->size) {
  1125. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1126. } else {
  1127. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1128. avpkt->size = user_pkt.size;
  1129. ret = -1;
  1130. }
  1131. avpkt->data = user_pkt.data;
  1132. avpkt->destruct = user_pkt.destruct;
  1133. } else {
  1134. if (av_dup_packet(avpkt) < 0) {
  1135. ret = AVERROR(ENOMEM);
  1136. }
  1137. }
  1138. }
  1139. if (!ret) {
  1140. if (!*got_packet_ptr)
  1141. avpkt->size = 0;
  1142. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1143. avpkt->pts = avpkt->dts = frame->pts;
  1144. if (needs_realloc && avpkt->data &&
  1145. avpkt->destruct == av_destruct_packet) {
  1146. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1147. if (new_data)
  1148. avpkt->data = new_data;
  1149. }
  1150. avctx->frame_number++;
  1151. }
  1152. if (ret < 0 || !*got_packet_ptr)
  1153. av_free_packet(avpkt);
  1154. emms_c();
  1155. return ret;
  1156. }
  1157. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1158. const AVSubtitle *sub)
  1159. {
  1160. int ret;
  1161. if(sub->start_display_time) {
  1162. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1163. return -1;
  1164. }
  1165. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)(intptr_t)sub);
  1166. avctx->frame_number++;
  1167. return ret;
  1168. }
  1169. /**
  1170. * Attempt to guess proper monotonic timestamps for decoded video frames
  1171. * which might have incorrect times. Input timestamps may wrap around, in
  1172. * which case the output will as well.
  1173. *
  1174. * @param pts the pts field of the decoded AVPacket, as passed through
  1175. * AVFrame.pkt_pts
  1176. * @param dts the dts field of the decoded AVPacket
  1177. * @return one of the input values, may be AV_NOPTS_VALUE
  1178. */
  1179. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1180. int64_t reordered_pts, int64_t dts)
  1181. {
  1182. int64_t pts = AV_NOPTS_VALUE;
  1183. if (dts != AV_NOPTS_VALUE) {
  1184. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1185. ctx->pts_correction_last_dts = dts;
  1186. }
  1187. if (reordered_pts != AV_NOPTS_VALUE) {
  1188. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1189. ctx->pts_correction_last_pts = reordered_pts;
  1190. }
  1191. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1192. && reordered_pts != AV_NOPTS_VALUE)
  1193. pts = reordered_pts;
  1194. else
  1195. pts = dts;
  1196. return pts;
  1197. }
  1198. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1199. {
  1200. int size = 0;
  1201. const uint8_t *data;
  1202. uint32_t flags;
  1203. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1204. return;
  1205. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1206. if (!data || size < 4)
  1207. return;
  1208. flags = bytestream_get_le32(&data);
  1209. size -= 4;
  1210. if (size < 4) /* Required for any of the changes */
  1211. return;
  1212. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1213. avctx->channels = bytestream_get_le32(&data);
  1214. size -= 4;
  1215. }
  1216. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1217. if (size < 8)
  1218. return;
  1219. avctx->channel_layout = bytestream_get_le64(&data);
  1220. size -= 8;
  1221. }
  1222. if (size < 4)
  1223. return;
  1224. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1225. avctx->sample_rate = bytestream_get_le32(&data);
  1226. size -= 4;
  1227. }
  1228. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1229. if (size < 8)
  1230. return;
  1231. avctx->width = bytestream_get_le32(&data);
  1232. avctx->height = bytestream_get_le32(&data);
  1233. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1234. size -= 8;
  1235. }
  1236. }
  1237. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1238. int *got_picture_ptr,
  1239. const AVPacket *avpkt)
  1240. {
  1241. int ret;
  1242. // copy to ensure we do not change avpkt
  1243. AVPacket tmp = *avpkt;
  1244. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1245. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1246. return AVERROR(EINVAL);
  1247. }
  1248. *got_picture_ptr= 0;
  1249. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1250. return -1;
  1251. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  1252. int did_split = av_packet_split_side_data(&tmp);
  1253. apply_param_change(avctx, &tmp);
  1254. avctx->pkt = &tmp;
  1255. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1256. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1257. &tmp);
  1258. else {
  1259. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1260. &tmp);
  1261. picture->pkt_dts= avpkt->dts;
  1262. if(!avctx->has_b_frames){
  1263. picture->pkt_pos= avpkt->pos;
  1264. }
  1265. //FIXME these should be under if(!avctx->has_b_frames)
  1266. if (!picture->sample_aspect_ratio.num)
  1267. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1268. if (!picture->width)
  1269. picture->width = avctx->width;
  1270. if (!picture->height)
  1271. picture->height = avctx->height;
  1272. if (picture->format == PIX_FMT_NONE)
  1273. picture->format = avctx->pix_fmt;
  1274. }
  1275. emms_c(); //needed to avoid an emms_c() call before every return;
  1276. avctx->pkt = NULL;
  1277. if (did_split)
  1278. ff_packet_free_side_data(&tmp);
  1279. if (*got_picture_ptr){
  1280. avctx->frame_number++;
  1281. picture->best_effort_timestamp = guess_correct_pts(avctx,
  1282. picture->pkt_pts,
  1283. picture->pkt_dts);
  1284. }
  1285. }else
  1286. ret= 0;
  1287. return ret;
  1288. }
  1289. #if FF_API_OLD_DECODE_AUDIO
  1290. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1291. int *frame_size_ptr,
  1292. AVPacket *avpkt)
  1293. {
  1294. AVFrame frame;
  1295. int ret, got_frame = 0;
  1296. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1297. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1298. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1299. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1300. "avcodec_decode_audio4()\n");
  1301. avctx->get_buffer = avcodec_default_get_buffer;
  1302. avctx->release_buffer = avcodec_default_release_buffer;
  1303. }
  1304. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1305. if (ret >= 0 && got_frame) {
  1306. int ch, plane_size;
  1307. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1308. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1309. frame.nb_samples,
  1310. avctx->sample_fmt, 1);
  1311. if (*frame_size_ptr < data_size) {
  1312. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1313. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1314. return AVERROR(EINVAL);
  1315. }
  1316. memcpy(samples, frame.extended_data[0], plane_size);
  1317. if (planar && avctx->channels > 1) {
  1318. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1319. for (ch = 1; ch < avctx->channels; ch++) {
  1320. memcpy(out, frame.extended_data[ch], plane_size);
  1321. out += plane_size;
  1322. }
  1323. }
  1324. *frame_size_ptr = data_size;
  1325. } else {
  1326. *frame_size_ptr = 0;
  1327. }
  1328. return ret;
  1329. }
  1330. #endif
  1331. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1332. AVFrame *frame,
  1333. int *got_frame_ptr,
  1334. const AVPacket *avpkt)
  1335. {
  1336. int ret = 0;
  1337. *got_frame_ptr = 0;
  1338. if (!avpkt->data && avpkt->size) {
  1339. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1340. return AVERROR(EINVAL);
  1341. }
  1342. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1343. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1344. return AVERROR(EINVAL);
  1345. }
  1346. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1347. // copy to ensure we do not change avpkt
  1348. AVPacket tmp = *avpkt;
  1349. int did_split = av_packet_split_side_data(&tmp);
  1350. apply_param_change(avctx, &tmp);
  1351. avctx->pkt = &tmp;
  1352. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1353. if (ret >= 0 && *got_frame_ptr) {
  1354. avctx->frame_number++;
  1355. frame->pkt_dts = avpkt->dts;
  1356. if (frame->format == AV_SAMPLE_FMT_NONE)
  1357. frame->format = avctx->sample_fmt;
  1358. if (!frame->channel_layout)
  1359. frame->channel_layout = avctx->channel_layout;
  1360. if (!frame->sample_rate)
  1361. frame->sample_rate = avctx->sample_rate;
  1362. }
  1363. avctx->pkt = NULL;
  1364. if (did_split)
  1365. ff_packet_free_side_data(&tmp);
  1366. }
  1367. return ret;
  1368. }
  1369. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1370. int *got_sub_ptr,
  1371. AVPacket *avpkt)
  1372. {
  1373. int ret;
  1374. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  1375. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  1376. return AVERROR(EINVAL);
  1377. }
  1378. avctx->pkt = avpkt;
  1379. *got_sub_ptr = 0;
  1380. avcodec_get_subtitle_defaults(sub);
  1381. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1382. if (*got_sub_ptr)
  1383. avctx->frame_number++;
  1384. return ret;
  1385. }
  1386. void avsubtitle_free(AVSubtitle *sub)
  1387. {
  1388. int i;
  1389. for (i = 0; i < sub->num_rects; i++)
  1390. {
  1391. av_freep(&sub->rects[i]->pict.data[0]);
  1392. av_freep(&sub->rects[i]->pict.data[1]);
  1393. av_freep(&sub->rects[i]->pict.data[2]);
  1394. av_freep(&sub->rects[i]->pict.data[3]);
  1395. av_freep(&sub->rects[i]->text);
  1396. av_freep(&sub->rects[i]->ass);
  1397. av_freep(&sub->rects[i]);
  1398. }
  1399. av_freep(&sub->rects);
  1400. memset(sub, 0, sizeof(AVSubtitle));
  1401. }
  1402. av_cold int avcodec_close(AVCodecContext *avctx)
  1403. {
  1404. /* If there is a user-supplied mutex locking routine, call it. */
  1405. if (ff_lockmgr_cb) {
  1406. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1407. return -1;
  1408. }
  1409. entangled_thread_counter++;
  1410. if(entangled_thread_counter != 1){
  1411. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1412. entangled_thread_counter--;
  1413. return -1;
  1414. }
  1415. if (avcodec_is_open(avctx)) {
  1416. if (HAVE_THREADS && avctx->thread_opaque)
  1417. ff_thread_free(avctx);
  1418. if (avctx->codec && avctx->codec->close)
  1419. avctx->codec->close(avctx);
  1420. avcodec_default_free_buffers(avctx);
  1421. avctx->coded_frame = NULL;
  1422. avctx->internal->byte_buffer_size = 0;
  1423. av_freep(&avctx->internal->byte_buffer);
  1424. av_freep(&avctx->internal);
  1425. }
  1426. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1427. av_opt_free(avctx->priv_data);
  1428. av_opt_free(avctx);
  1429. av_freep(&avctx->priv_data);
  1430. if (av_codec_is_encoder(avctx->codec))
  1431. av_freep(&avctx->extradata);
  1432. avctx->codec = NULL;
  1433. avctx->active_thread_type = 0;
  1434. entangled_thread_counter--;
  1435. /* Release any user-supplied mutex. */
  1436. if (ff_lockmgr_cb) {
  1437. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1438. }
  1439. return 0;
  1440. }
  1441. static enum CodecID remap_deprecated_codec_id(enum CodecID id)
  1442. {
  1443. switch(id){
  1444. //This is for future deprecatec codec ids, its empty since
  1445. //last major bump but will fill up again over time, please dont remove it
  1446. // case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
  1447. default : return id;
  1448. }
  1449. }
  1450. AVCodec *avcodec_find_encoder(enum CodecID id)
  1451. {
  1452. AVCodec *p, *experimental=NULL;
  1453. p = first_avcodec;
  1454. id= remap_deprecated_codec_id(id);
  1455. while (p) {
  1456. if (av_codec_is_encoder(p) && p->id == id) {
  1457. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1458. experimental = p;
  1459. } else
  1460. return p;
  1461. }
  1462. p = p->next;
  1463. }
  1464. return experimental;
  1465. }
  1466. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1467. {
  1468. AVCodec *p;
  1469. if (!name)
  1470. return NULL;
  1471. p = first_avcodec;
  1472. while (p) {
  1473. if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
  1474. return p;
  1475. p = p->next;
  1476. }
  1477. return NULL;
  1478. }
  1479. AVCodec *avcodec_find_decoder(enum CodecID id)
  1480. {
  1481. AVCodec *p, *experimental=NULL;
  1482. p = first_avcodec;
  1483. id= remap_deprecated_codec_id(id);
  1484. while (p) {
  1485. if (av_codec_is_decoder(p) && p->id == id) {
  1486. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1487. experimental = p;
  1488. } else
  1489. return p;
  1490. }
  1491. p = p->next;
  1492. }
  1493. return experimental;
  1494. }
  1495. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1496. {
  1497. AVCodec *p;
  1498. if (!name)
  1499. return NULL;
  1500. p = first_avcodec;
  1501. while (p) {
  1502. if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
  1503. return p;
  1504. p = p->next;
  1505. }
  1506. return NULL;
  1507. }
  1508. const char *avcodec_get_name(enum CodecID id)
  1509. {
  1510. AVCodec *codec;
  1511. #if !CONFIG_SMALL
  1512. switch (id) {
  1513. #include "libavcodec/codec_names.h"
  1514. }
  1515. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1516. #endif
  1517. codec = avcodec_find_decoder(id);
  1518. if (codec)
  1519. return codec->name;
  1520. codec = avcodec_find_encoder(id);
  1521. if (codec)
  1522. return codec->name;
  1523. return "unknown_codec";
  1524. }
  1525. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1526. {
  1527. int i, len, ret = 0;
  1528. for (i = 0; i < 4; i++) {
  1529. len = snprintf(buf, buf_size,
  1530. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1531. buf += len;
  1532. buf_size = buf_size > len ? buf_size - len : 0;
  1533. ret += len;
  1534. codec_tag>>=8;
  1535. }
  1536. return ret;
  1537. }
  1538. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1539. {
  1540. const char *codec_type;
  1541. const char *codec_name;
  1542. const char *profile = NULL;
  1543. AVCodec *p;
  1544. int bitrate;
  1545. AVRational display_aspect_ratio;
  1546. if (!buf || buf_size <= 0)
  1547. return;
  1548. codec_type = av_get_media_type_string(enc->codec_type);
  1549. codec_name = avcodec_get_name(enc->codec_id);
  1550. if (enc->profile != FF_PROFILE_UNKNOWN) {
  1551. if (enc->codec)
  1552. p = enc->codec;
  1553. else
  1554. p = encode ? avcodec_find_encoder(enc->codec_id) :
  1555. avcodec_find_decoder(enc->codec_id);
  1556. if (p)
  1557. profile = av_get_profile_name(p, enc->profile);
  1558. }
  1559. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  1560. codec_name, enc->mb_decision ? " (hq)" : "");
  1561. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1562. if (profile)
  1563. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1564. if (enc->codec_tag) {
  1565. char tag_buf[32];
  1566. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1567. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1568. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  1569. }
  1570. switch(enc->codec_type) {
  1571. case AVMEDIA_TYPE_VIDEO:
  1572. if (enc->pix_fmt != PIX_FMT_NONE) {
  1573. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1574. ", %s",
  1575. av_get_pix_fmt_name(enc->pix_fmt));
  1576. }
  1577. if (enc->width) {
  1578. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1579. ", %dx%d",
  1580. enc->width, enc->height);
  1581. if (enc->sample_aspect_ratio.num) {
  1582. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1583. enc->width*enc->sample_aspect_ratio.num,
  1584. enc->height*enc->sample_aspect_ratio.den,
  1585. 1024*1024);
  1586. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1587. " [SAR %d:%d DAR %d:%d]",
  1588. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1589. display_aspect_ratio.num, display_aspect_ratio.den);
  1590. }
  1591. if(av_log_get_level() >= AV_LOG_DEBUG){
  1592. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1593. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1594. ", %d/%d",
  1595. enc->time_base.num/g, enc->time_base.den/g);
  1596. }
  1597. }
  1598. if (encode) {
  1599. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1600. ", q=%d-%d", enc->qmin, enc->qmax);
  1601. }
  1602. break;
  1603. case AVMEDIA_TYPE_AUDIO:
  1604. if (enc->sample_rate) {
  1605. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1606. ", %d Hz", enc->sample_rate);
  1607. }
  1608. av_strlcat(buf, ", ", buf_size);
  1609. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1610. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1611. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1612. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1613. }
  1614. break;
  1615. default:
  1616. return;
  1617. }
  1618. if (encode) {
  1619. if (enc->flags & CODEC_FLAG_PASS1)
  1620. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1621. ", pass 1");
  1622. if (enc->flags & CODEC_FLAG_PASS2)
  1623. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1624. ", pass 2");
  1625. }
  1626. bitrate = get_bit_rate(enc);
  1627. if (bitrate != 0) {
  1628. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1629. ", %d kb/s", bitrate / 1000);
  1630. }
  1631. }
  1632. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1633. {
  1634. const AVProfile *p;
  1635. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1636. return NULL;
  1637. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1638. if (p->profile == profile)
  1639. return p->name;
  1640. return NULL;
  1641. }
  1642. unsigned avcodec_version( void )
  1643. {
  1644. // av_assert0(CODEC_ID_V410==164);
  1645. av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
  1646. av_assert0(CODEC_ID_ADPCM_G722==69660);
  1647. // av_assert0(CODEC_ID_BMV_AUDIO==86071);
  1648. av_assert0(CODEC_ID_SRT==94216);
  1649. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1650. return LIBAVCODEC_VERSION_INT;
  1651. }
  1652. const char *avcodec_configuration(void)
  1653. {
  1654. return FFMPEG_CONFIGURATION;
  1655. }
  1656. const char *avcodec_license(void)
  1657. {
  1658. #define LICENSE_PREFIX "libavcodec license: "
  1659. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1660. }
  1661. void avcodec_flush_buffers(AVCodecContext *avctx)
  1662. {
  1663. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1664. ff_thread_flush(avctx);
  1665. else if(avctx->codec->flush)
  1666. avctx->codec->flush(avctx);
  1667. avctx->pts_correction_last_pts =
  1668. avctx->pts_correction_last_dts = INT64_MIN;
  1669. }
  1670. static void video_free_buffers(AVCodecContext *s)
  1671. {
  1672. AVCodecInternal *avci = s->internal;
  1673. int i, j;
  1674. if (!avci->buffer)
  1675. return;
  1676. if (avci->buffer_count)
  1677. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1678. avci->buffer_count);
  1679. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1680. InternalBuffer *buf = &avci->buffer[i];
  1681. for(j=0; j<4; j++){
  1682. av_freep(&buf->base[j]);
  1683. buf->data[j]= NULL;
  1684. }
  1685. }
  1686. av_freep(&avci->buffer);
  1687. avci->buffer_count=0;
  1688. }
  1689. static void audio_free_buffers(AVCodecContext *avctx)
  1690. {
  1691. AVCodecInternal *avci = avctx->internal;
  1692. InternalBuffer *buf;
  1693. if (!avci->buffer)
  1694. return;
  1695. buf = avci->buffer;
  1696. if (buf->extended_data) {
  1697. av_free(buf->extended_data[0]);
  1698. if (buf->extended_data != buf->data)
  1699. av_freep(&buf->extended_data);
  1700. }
  1701. av_freep(&avci->buffer);
  1702. }
  1703. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1704. {
  1705. switch (avctx->codec_type) {
  1706. case AVMEDIA_TYPE_VIDEO:
  1707. video_free_buffers(avctx);
  1708. break;
  1709. case AVMEDIA_TYPE_AUDIO:
  1710. audio_free_buffers(avctx);
  1711. break;
  1712. default:
  1713. break;
  1714. }
  1715. }
  1716. int av_get_exact_bits_per_sample(enum CodecID codec_id)
  1717. {
  1718. switch(codec_id){
  1719. case CODEC_ID_ADPCM_CT:
  1720. case CODEC_ID_ADPCM_IMA_APC:
  1721. case CODEC_ID_ADPCM_IMA_EA_SEAD:
  1722. case CODEC_ID_ADPCM_IMA_WS:
  1723. case CODEC_ID_ADPCM_G722:
  1724. case CODEC_ID_ADPCM_YAMAHA:
  1725. return 4;
  1726. case CODEC_ID_PCM_ALAW:
  1727. case CODEC_ID_PCM_MULAW:
  1728. case CODEC_ID_PCM_S8:
  1729. case CODEC_ID_PCM_U8:
  1730. case CODEC_ID_PCM_ZORK:
  1731. return 8;
  1732. case CODEC_ID_PCM_S16BE:
  1733. case CODEC_ID_PCM_S16LE:
  1734. case CODEC_ID_PCM_S16LE_PLANAR:
  1735. case CODEC_ID_PCM_U16BE:
  1736. case CODEC_ID_PCM_U16LE:
  1737. return 16;
  1738. case CODEC_ID_PCM_S24DAUD:
  1739. case CODEC_ID_PCM_S24BE:
  1740. case CODEC_ID_PCM_S24LE:
  1741. case CODEC_ID_PCM_U24BE:
  1742. case CODEC_ID_PCM_U24LE:
  1743. return 24;
  1744. case CODEC_ID_PCM_S32BE:
  1745. case CODEC_ID_PCM_S32LE:
  1746. case CODEC_ID_PCM_U32BE:
  1747. case CODEC_ID_PCM_U32LE:
  1748. case CODEC_ID_PCM_F32BE:
  1749. case CODEC_ID_PCM_F32LE:
  1750. return 32;
  1751. case CODEC_ID_PCM_F64BE:
  1752. case CODEC_ID_PCM_F64LE:
  1753. return 64;
  1754. default:
  1755. return 0;
  1756. }
  1757. }
  1758. enum CodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  1759. {
  1760. static const enum CodecID map[AV_SAMPLE_FMT_NB][2] = {
  1761. [AV_SAMPLE_FMT_U8 ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
  1762. [AV_SAMPLE_FMT_S16 ] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
  1763. [AV_SAMPLE_FMT_S32 ] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
  1764. [AV_SAMPLE_FMT_FLT ] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
  1765. [AV_SAMPLE_FMT_DBL ] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
  1766. [AV_SAMPLE_FMT_U8P ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
  1767. [AV_SAMPLE_FMT_S16P] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
  1768. [AV_SAMPLE_FMT_S32P] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
  1769. [AV_SAMPLE_FMT_FLTP] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
  1770. [AV_SAMPLE_FMT_DBLP] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
  1771. };
  1772. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  1773. return CODEC_ID_NONE;
  1774. if (be < 0 || be > 1)
  1775. be = AV_NE(1, 0);
  1776. return map[fmt][be];
  1777. }
  1778. int av_get_bits_per_sample(enum CodecID codec_id)
  1779. {
  1780. switch (codec_id) {
  1781. case CODEC_ID_ADPCM_SBPRO_2:
  1782. return 2;
  1783. case CODEC_ID_ADPCM_SBPRO_3:
  1784. return 3;
  1785. case CODEC_ID_ADPCM_SBPRO_4:
  1786. case CODEC_ID_ADPCM_IMA_WAV:
  1787. case CODEC_ID_ADPCM_IMA_QT:
  1788. case CODEC_ID_ADPCM_SWF:
  1789. case CODEC_ID_ADPCM_MS:
  1790. return 4;
  1791. default:
  1792. return av_get_exact_bits_per_sample(codec_id);
  1793. }
  1794. }
  1795. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1796. {
  1797. int id, sr, ch, ba, tag, bps;
  1798. id = avctx->codec_id;
  1799. sr = avctx->sample_rate;
  1800. ch = avctx->channels;
  1801. ba = avctx->block_align;
  1802. tag = avctx->codec_tag;
  1803. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1804. /* codecs with an exact constant bits per sample */
  1805. if (bps > 0 && ch > 0 && frame_bytes > 0)
  1806. return (frame_bytes * 8) / (bps * ch);
  1807. bps = avctx->bits_per_coded_sample;
  1808. /* codecs with a fixed packet duration */
  1809. switch (id) {
  1810. case CODEC_ID_ADPCM_ADX: return 32;
  1811. case CODEC_ID_ADPCM_IMA_QT: return 64;
  1812. case CODEC_ID_ADPCM_EA_XAS: return 128;
  1813. case CODEC_ID_AMR_NB:
  1814. case CODEC_ID_GSM:
  1815. case CODEC_ID_QCELP:
  1816. case CODEC_ID_RA_144:
  1817. case CODEC_ID_RA_288: return 160;
  1818. case CODEC_ID_IMC: return 256;
  1819. case CODEC_ID_AMR_WB:
  1820. case CODEC_ID_GSM_MS: return 320;
  1821. case CODEC_ID_MP1: return 384;
  1822. case CODEC_ID_ATRAC1: return 512;
  1823. case CODEC_ID_ATRAC3: return 1024;
  1824. case CODEC_ID_MP2:
  1825. case CODEC_ID_MUSEPACK7: return 1152;
  1826. case CODEC_ID_AC3: return 1536;
  1827. }
  1828. if (sr > 0) {
  1829. /* calc from sample rate */
  1830. if (id == CODEC_ID_TTA)
  1831. return 256 * sr / 245;
  1832. if (ch > 0) {
  1833. /* calc from sample rate and channels */
  1834. if (id == CODEC_ID_BINKAUDIO_DCT)
  1835. return (480 << (sr / 22050)) / ch;
  1836. }
  1837. }
  1838. if (ba > 0) {
  1839. /* calc from block_align */
  1840. if (id == CODEC_ID_SIPR) {
  1841. switch (ba) {
  1842. case 20: return 160;
  1843. case 19: return 144;
  1844. case 29: return 288;
  1845. case 37: return 480;
  1846. }
  1847. }
  1848. }
  1849. if (frame_bytes > 0) {
  1850. /* calc from frame_bytes only */
  1851. if (id == CODEC_ID_TRUESPEECH)
  1852. return 240 * (frame_bytes / 32);
  1853. if (id == CODEC_ID_NELLYMOSER)
  1854. return 256 * (frame_bytes / 64);
  1855. if (bps > 0) {
  1856. /* calc from frame_bytes and bits_per_coded_sample */
  1857. if (id == CODEC_ID_ADPCM_G726)
  1858. return frame_bytes * 8 / bps;
  1859. }
  1860. if (ch > 0) {
  1861. /* calc from frame_bytes and channels */
  1862. switch (id) {
  1863. case CODEC_ID_ADPCM_4XM:
  1864. case CODEC_ID_ADPCM_IMA_ISS:
  1865. return (frame_bytes - 4 * ch) * 2 / ch;
  1866. case CODEC_ID_ADPCM_IMA_SMJPEG:
  1867. return (frame_bytes - 4) * 2 / ch;
  1868. case CODEC_ID_ADPCM_IMA_AMV:
  1869. return (frame_bytes - 8) * 2 / ch;
  1870. case CODEC_ID_ADPCM_XA:
  1871. return (frame_bytes / 128) * 224 / ch;
  1872. case CODEC_ID_INTERPLAY_DPCM:
  1873. return (frame_bytes - 6 - ch) / ch;
  1874. case CODEC_ID_ROQ_DPCM:
  1875. return (frame_bytes - 8) / ch;
  1876. case CODEC_ID_XAN_DPCM:
  1877. return (frame_bytes - 2 * ch) / ch;
  1878. case CODEC_ID_MACE3:
  1879. return 3 * frame_bytes / ch;
  1880. case CODEC_ID_MACE6:
  1881. return 6 * frame_bytes / ch;
  1882. case CODEC_ID_PCM_LXF:
  1883. return 2 * (frame_bytes / (5 * ch));
  1884. }
  1885. if (tag) {
  1886. /* calc from frame_bytes, channels, and codec_tag */
  1887. if (id == CODEC_ID_SOL_DPCM) {
  1888. if (tag == 3)
  1889. return frame_bytes / ch;
  1890. else
  1891. return frame_bytes * 2 / ch;
  1892. }
  1893. }
  1894. if (ba > 0) {
  1895. /* calc from frame_bytes, channels, and block_align */
  1896. int blocks = frame_bytes / ba;
  1897. switch (avctx->codec_id) {
  1898. case CODEC_ID_ADPCM_IMA_WAV:
  1899. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1900. case CODEC_ID_ADPCM_IMA_DK3:
  1901. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1902. case CODEC_ID_ADPCM_IMA_DK4:
  1903. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1904. case CODEC_ID_ADPCM_MS:
  1905. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1906. }
  1907. }
  1908. if (bps > 0) {
  1909. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1910. switch (avctx->codec_id) {
  1911. case CODEC_ID_PCM_DVD:
  1912. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1913. case CODEC_ID_PCM_BLURAY:
  1914. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  1915. case CODEC_ID_S302M:
  1916. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1917. }
  1918. }
  1919. }
  1920. }
  1921. return 0;
  1922. }
  1923. #if !HAVE_THREADS
  1924. int ff_thread_init(AVCodecContext *s){
  1925. return -1;
  1926. }
  1927. #endif
  1928. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1929. {
  1930. unsigned int n = 0;
  1931. while(v >= 0xff) {
  1932. *s++ = 0xff;
  1933. v -= 0xff;
  1934. n++;
  1935. }
  1936. *s = v;
  1937. n++;
  1938. return n;
  1939. }
  1940. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1941. int i;
  1942. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1943. return i;
  1944. }
  1945. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1946. {
  1947. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1948. "version to the newest one from Git. If the problem still "
  1949. "occurs, it means that your file has a feature which has not "
  1950. "been implemented.\n", feature);
  1951. if(want_sample)
  1952. av_log_ask_for_sample(avc, NULL);
  1953. }
  1954. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1955. {
  1956. va_list argument_list;
  1957. va_start(argument_list, msg);
  1958. if (msg)
  1959. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1960. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1961. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1962. "and contact the ffmpeg-devel mailing list.\n");
  1963. va_end(argument_list);
  1964. }
  1965. static AVHWAccel *first_hwaccel = NULL;
  1966. void av_register_hwaccel(AVHWAccel *hwaccel)
  1967. {
  1968. AVHWAccel **p = &first_hwaccel;
  1969. while (*p)
  1970. p = &(*p)->next;
  1971. *p = hwaccel;
  1972. hwaccel->next = NULL;
  1973. }
  1974. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1975. {
  1976. return hwaccel ? hwaccel->next : first_hwaccel;
  1977. }
  1978. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1979. {
  1980. AVHWAccel *hwaccel=NULL;
  1981. while((hwaccel= av_hwaccel_next(hwaccel))){
  1982. if ( hwaccel->id == codec_id
  1983. && hwaccel->pix_fmt == pix_fmt)
  1984. return hwaccel;
  1985. }
  1986. return NULL;
  1987. }
  1988. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1989. {
  1990. if (ff_lockmgr_cb) {
  1991. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1992. return -1;
  1993. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1994. return -1;
  1995. }
  1996. ff_lockmgr_cb = cb;
  1997. if (ff_lockmgr_cb) {
  1998. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1999. return -1;
  2000. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2001. return -1;
  2002. }
  2003. return 0;
  2004. }
  2005. int avpriv_lock_avformat(void)
  2006. {
  2007. if (ff_lockmgr_cb) {
  2008. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2009. return -1;
  2010. }
  2011. return 0;
  2012. }
  2013. int avpriv_unlock_avformat(void)
  2014. {
  2015. if (ff_lockmgr_cb) {
  2016. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2017. return -1;
  2018. }
  2019. return 0;
  2020. }
  2021. unsigned int avpriv_toupper4(unsigned int x)
  2022. {
  2023. return toupper( x &0xFF)
  2024. + (toupper((x>>8 )&0xFF)<<8 )
  2025. + (toupper((x>>16)&0xFF)<<16)
  2026. + (toupper((x>>24)&0xFF)<<24);
  2027. }
  2028. #if !HAVE_THREADS
  2029. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  2030. {
  2031. f->owner = avctx;
  2032. ff_init_buffer_info(avctx, f);
  2033. return avctx->get_buffer(avctx, f);
  2034. }
  2035. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  2036. {
  2037. f->owner->release_buffer(f->owner, f);
  2038. }
  2039. void ff_thread_finish_setup(AVCodecContext *avctx)
  2040. {
  2041. }
  2042. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  2043. {
  2044. }
  2045. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  2046. {
  2047. }
  2048. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2049. {
  2050. return 1;
  2051. }
  2052. #endif
  2053. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  2054. {
  2055. AVCodec *c= avcodec_find_decoder(codec_id);
  2056. if(!c)
  2057. c= avcodec_find_encoder(codec_id);
  2058. if(c)
  2059. return c->type;
  2060. if (codec_id <= CODEC_ID_NONE)
  2061. return AVMEDIA_TYPE_UNKNOWN;
  2062. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  2063. return AVMEDIA_TYPE_VIDEO;
  2064. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  2065. return AVMEDIA_TYPE_AUDIO;
  2066. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  2067. return AVMEDIA_TYPE_SUBTITLE;
  2068. return AVMEDIA_TYPE_UNKNOWN;
  2069. }
  2070. int avcodec_is_open(AVCodecContext *s)
  2071. {
  2072. return !!s->internal;
  2073. }