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.

2855 lines
91KB

  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 "config.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/bprint.h"
  30. #include "libavutil/channel_layout.h"
  31. #include "libavutil/crc.h"
  32. #include "libavutil/mathematics.h"
  33. #include "libavutil/pixdesc.h"
  34. #include "libavutil/imgutils.h"
  35. #include "libavutil/samplefmt.h"
  36. #include "libavutil/dict.h"
  37. #include "libavutil/avassert.h"
  38. #include "avcodec.h"
  39. #include "dsputil.h"
  40. #include "libavutil/opt.h"
  41. #include "thread.h"
  42. #include "frame_thread_encoder.h"
  43. #include "internal.h"
  44. #include "bytestream.h"
  45. #include <stdlib.h>
  46. #include <stdarg.h>
  47. #include <limits.h>
  48. #include <float.h>
  49. #if HAVE_ICONV
  50. # include <iconv.h>
  51. #endif
  52. volatile int ff_avcodec_locked;
  53. static int volatile entangled_thread_counter = 0;
  54. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  55. static void *codec_mutex;
  56. static void *avformat_mutex;
  57. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  58. {
  59. if (min_size < *size)
  60. return ptr;
  61. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  62. ptr = av_realloc(ptr, min_size);
  63. /* we could set this to the unmodified min_size but this is safer
  64. * if the user lost the ptr and uses NULL now
  65. */
  66. if (!ptr)
  67. min_size = 0;
  68. *size = min_size;
  69. return ptr;
  70. }
  71. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  72. {
  73. void **p = ptr;
  74. if (min_size < *size)
  75. return 0;
  76. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  77. av_free(*p);
  78. *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  79. if (!*p)
  80. min_size = 0;
  81. *size = min_size;
  82. return 1;
  83. }
  84. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  85. {
  86. ff_fast_malloc(ptr, size, min_size, 0);
  87. }
  88. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  89. {
  90. uint8_t **p = ptr;
  91. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  92. av_freep(p);
  93. *size = 0;
  94. return;
  95. }
  96. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  97. memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  98. }
  99. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  100. {
  101. uint8_t **p = ptr;
  102. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  103. av_freep(p);
  104. *size = 0;
  105. return;
  106. }
  107. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  108. memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  109. }
  110. /* encoder management */
  111. static AVCodec *first_avcodec = NULL;
  112. AVCodec *av_codec_next(const AVCodec *c)
  113. {
  114. if (c)
  115. return c->next;
  116. else
  117. return first_avcodec;
  118. }
  119. static void avcodec_init(void)
  120. {
  121. static int initialized = 0;
  122. if (initialized != 0)
  123. return;
  124. initialized = 1;
  125. ff_dsputil_static_init();
  126. }
  127. int av_codec_is_encoder(const AVCodec *codec)
  128. {
  129. return codec && (codec->encode_sub || codec->encode2);
  130. }
  131. int av_codec_is_decoder(const AVCodec *codec)
  132. {
  133. return codec && codec->decode;
  134. }
  135. void avcodec_register(AVCodec *codec)
  136. {
  137. AVCodec **p;
  138. avcodec_init();
  139. p = &first_avcodec;
  140. while (*p != NULL)
  141. p = &(*p)->next;
  142. *p = codec;
  143. codec->next = NULL;
  144. if (codec->init_static_data)
  145. codec->init_static_data(codec);
  146. }
  147. unsigned avcodec_get_edge_width(void)
  148. {
  149. return EDGE_WIDTH;
  150. }
  151. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  152. {
  153. s->coded_width = width;
  154. s->coded_height = height;
  155. s->width = -((-width ) >> s->lowres);
  156. s->height = -((-height) >> s->lowres);
  157. }
  158. #define INTERNAL_BUFFER_SIZE (32 + 1)
  159. #if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
  160. # define STRIDE_ALIGN 16
  161. #else
  162. # define STRIDE_ALIGN 8
  163. #endif
  164. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  165. int linesize_align[AV_NUM_DATA_POINTERS])
  166. {
  167. int i;
  168. int w_align = 1;
  169. int h_align = 1;
  170. switch (s->pix_fmt) {
  171. case AV_PIX_FMT_YUV420P:
  172. case AV_PIX_FMT_YUYV422:
  173. case AV_PIX_FMT_UYVY422:
  174. case AV_PIX_FMT_YUV422P:
  175. case AV_PIX_FMT_YUV440P:
  176. case AV_PIX_FMT_YUV444P:
  177. case AV_PIX_FMT_GBRP:
  178. case AV_PIX_FMT_GRAY8:
  179. case AV_PIX_FMT_GRAY16BE:
  180. case AV_PIX_FMT_GRAY16LE:
  181. case AV_PIX_FMT_YUVJ420P:
  182. case AV_PIX_FMT_YUVJ422P:
  183. case AV_PIX_FMT_YUVJ440P:
  184. case AV_PIX_FMT_YUVJ444P:
  185. case AV_PIX_FMT_YUVA420P:
  186. case AV_PIX_FMT_YUVA422P:
  187. case AV_PIX_FMT_YUVA444P:
  188. case AV_PIX_FMT_YUV420P9LE:
  189. case AV_PIX_FMT_YUV420P9BE:
  190. case AV_PIX_FMT_YUV420P10LE:
  191. case AV_PIX_FMT_YUV420P10BE:
  192. case AV_PIX_FMT_YUV420P12LE:
  193. case AV_PIX_FMT_YUV420P12BE:
  194. case AV_PIX_FMT_YUV420P14LE:
  195. case AV_PIX_FMT_YUV420P14BE:
  196. case AV_PIX_FMT_YUV422P9LE:
  197. case AV_PIX_FMT_YUV422P9BE:
  198. case AV_PIX_FMT_YUV422P10LE:
  199. case AV_PIX_FMT_YUV422P10BE:
  200. case AV_PIX_FMT_YUV422P12LE:
  201. case AV_PIX_FMT_YUV422P12BE:
  202. case AV_PIX_FMT_YUV422P14LE:
  203. case AV_PIX_FMT_YUV422P14BE:
  204. case AV_PIX_FMT_YUV444P9LE:
  205. case AV_PIX_FMT_YUV444P9BE:
  206. case AV_PIX_FMT_YUV444P10LE:
  207. case AV_PIX_FMT_YUV444P10BE:
  208. case AV_PIX_FMT_YUV444P12LE:
  209. case AV_PIX_FMT_YUV444P12BE:
  210. case AV_PIX_FMT_YUV444P14LE:
  211. case AV_PIX_FMT_YUV444P14BE:
  212. case AV_PIX_FMT_GBRP9LE:
  213. case AV_PIX_FMT_GBRP9BE:
  214. case AV_PIX_FMT_GBRP10LE:
  215. case AV_PIX_FMT_GBRP10BE:
  216. case AV_PIX_FMT_GBRP12LE:
  217. case AV_PIX_FMT_GBRP12BE:
  218. case AV_PIX_FMT_GBRP14LE:
  219. case AV_PIX_FMT_GBRP14BE:
  220. w_align = 16; //FIXME assume 16 pixel per macroblock
  221. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  222. break;
  223. case AV_PIX_FMT_YUV411P:
  224. case AV_PIX_FMT_UYYVYY411:
  225. w_align = 32;
  226. h_align = 8;
  227. break;
  228. case AV_PIX_FMT_YUV410P:
  229. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  230. w_align = 64;
  231. h_align = 64;
  232. }
  233. break;
  234. case AV_PIX_FMT_RGB555:
  235. if (s->codec_id == AV_CODEC_ID_RPZA) {
  236. w_align = 4;
  237. h_align = 4;
  238. }
  239. break;
  240. case AV_PIX_FMT_PAL8:
  241. case AV_PIX_FMT_BGR8:
  242. case AV_PIX_FMT_RGB8:
  243. if (s->codec_id == AV_CODEC_ID_SMC ||
  244. s->codec_id == AV_CODEC_ID_CINEPAK) {
  245. w_align = 4;
  246. h_align = 4;
  247. }
  248. break;
  249. case AV_PIX_FMT_BGR24:
  250. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  251. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  252. w_align = 4;
  253. h_align = 4;
  254. }
  255. break;
  256. case AV_PIX_FMT_RGB24:
  257. if (s->codec_id == AV_CODEC_ID_CINEPAK) {
  258. w_align = 4;
  259. h_align = 4;
  260. }
  261. break;
  262. default:
  263. w_align = 1;
  264. h_align = 1;
  265. break;
  266. }
  267. if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
  268. w_align = FFMAX(w_align, 8);
  269. }
  270. *width = FFALIGN(*width, w_align);
  271. *height = FFALIGN(*height, h_align);
  272. if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
  273. // some of the optimized chroma MC reads one line too much
  274. // which is also done in mpeg decoders with lowres > 0
  275. *height += 2;
  276. for (i = 0; i < 4; i++)
  277. linesize_align[i] = STRIDE_ALIGN;
  278. }
  279. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  280. {
  281. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  282. int chroma_shift = desc->log2_chroma_w;
  283. int linesize_align[AV_NUM_DATA_POINTERS];
  284. int align;
  285. avcodec_align_dimensions2(s, width, height, linesize_align);
  286. align = FFMAX(linesize_align[0], linesize_align[3]);
  287. linesize_align[1] <<= chroma_shift;
  288. linesize_align[2] <<= chroma_shift;
  289. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  290. *width = FFALIGN(*width, align);
  291. }
  292. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  293. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  294. int buf_size, int align)
  295. {
  296. int ch, planar, needed_size, ret = 0;
  297. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  298. frame->nb_samples, sample_fmt,
  299. align);
  300. if (buf_size < needed_size)
  301. return AVERROR(EINVAL);
  302. planar = av_sample_fmt_is_planar(sample_fmt);
  303. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  304. if (!(frame->extended_data = av_mallocz(nb_channels *
  305. sizeof(*frame->extended_data))))
  306. return AVERROR(ENOMEM);
  307. } else {
  308. frame->extended_data = frame->data;
  309. }
  310. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  311. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  312. sample_fmt, align)) < 0) {
  313. if (frame->extended_data != frame->data)
  314. av_freep(&frame->extended_data);
  315. return ret;
  316. }
  317. if (frame->extended_data != frame->data) {
  318. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  319. frame->data[ch] = frame->extended_data[ch];
  320. }
  321. return ret;
  322. }
  323. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  324. {
  325. AVCodecInternal *avci = avctx->internal;
  326. int buf_size, ret;
  327. av_freep(&avci->audio_data);
  328. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  329. frame->nb_samples, avctx->sample_fmt,
  330. 0);
  331. if (buf_size < 0)
  332. return AVERROR(EINVAL);
  333. frame->data[0] = av_mallocz(buf_size);
  334. if (!frame->data[0])
  335. return AVERROR(ENOMEM);
  336. ret = avcodec_fill_audio_frame(frame, avctx->channels, avctx->sample_fmt,
  337. frame->data[0], buf_size, 0);
  338. if (ret < 0) {
  339. av_freep(&frame->data[0]);
  340. return ret;
  341. }
  342. avci->audio_data = frame->data[0];
  343. if (avctx->debug & FF_DEBUG_BUFFERS)
  344. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  345. "internal audio buffer used\n", frame);
  346. return 0;
  347. }
  348. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  349. {
  350. int i;
  351. int w = s->width;
  352. int h = s->height;
  353. InternalBuffer *buf;
  354. AVCodecInternal *avci = s->internal;
  355. if (pic->data[0] != NULL) {
  356. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  357. return -1;
  358. }
  359. if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  360. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  361. return -1;
  362. }
  363. if (av_image_check_size(w, h, 0, s) || s->pix_fmt<0) {
  364. av_log(s, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
  365. return -1;
  366. }
  367. if (!avci->buffer) {
  368. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
  369. sizeof(InternalBuffer));
  370. }
  371. buf = &avci->buffer[avci->buffer_count];
  372. if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
  373. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  374. av_freep(&buf->base[i]);
  375. buf->data[i] = NULL;
  376. }
  377. }
  378. if (!buf->base[0]) {
  379. int h_chroma_shift, v_chroma_shift;
  380. int size[4] = { 0 };
  381. int tmpsize;
  382. int unaligned;
  383. AVPicture picture;
  384. int stride_align[AV_NUM_DATA_POINTERS];
  385. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  386. const int pixel_size = desc->comp[0].step_minus1 + 1;
  387. av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift,
  388. &v_chroma_shift);
  389. avcodec_align_dimensions2(s, &w, &h, stride_align);
  390. if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
  391. w += EDGE_WIDTH * 2;
  392. h += EDGE_WIDTH * 2;
  393. }
  394. do {
  395. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  396. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  397. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  398. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  399. w += w & ~(w - 1);
  400. unaligned = 0;
  401. for (i = 0; i < 4; i++)
  402. unaligned |= picture.linesize[i] % stride_align[i];
  403. } while (unaligned);
  404. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  405. if (tmpsize < 0)
  406. return -1;
  407. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  408. size[i] = picture.data[i + 1] - picture.data[i];
  409. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  410. memset(buf->base, 0, sizeof(buf->base));
  411. memset(buf->data, 0, sizeof(buf->data));
  412. for (i = 0; i < 4 && size[i]; i++) {
  413. const int h_shift = i == 0 ? 0 : h_chroma_shift;
  414. const int v_shift = i == 0 ? 0 : v_chroma_shift;
  415. buf->linesize[i] = picture.linesize[i];
  416. buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
  417. if (buf->base[i] == NULL)
  418. return AVERROR(ENOMEM);
  419. // no edge if EDGE EMU or not planar YUV
  420. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
  421. buf->data[i] = buf->base[i];
  422. else
  423. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
  424. }
  425. for (; i < AV_NUM_DATA_POINTERS; i++) {
  426. buf->base[i] = buf->data[i] = NULL;
  427. buf->linesize[i] = 0;
  428. }
  429. if (size[1] && !size[2])
  430. avpriv_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
  431. buf->width = s->width;
  432. buf->height = s->height;
  433. buf->pix_fmt = s->pix_fmt;
  434. }
  435. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  436. pic->base[i] = buf->base[i];
  437. pic->data[i] = buf->data[i];
  438. pic->linesize[i] = buf->linesize[i];
  439. }
  440. pic->extended_data = pic->data;
  441. avci->buffer_count++;
  442. if (s->debug & FF_DEBUG_BUFFERS)
  443. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  444. "buffers used\n", pic, avci->buffer_count);
  445. return 0;
  446. }
  447. void avpriv_color_frame(AVFrame *frame, const int c[4])
  448. {
  449. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  450. int p, y, x;
  451. av_assert0(desc->flags & PIX_FMT_PLANAR);
  452. for (p = 0; p<desc->nb_components; p++) {
  453. uint8_t *dst = frame->data[p];
  454. int is_chroma = p == 1 || p == 2;
  455. int bytes = -((-frame->width) >> (is_chroma ? desc->log2_chroma_w : 0));
  456. for (y = 0; y<-((-frame->height) >> (is_chroma ? desc->log2_chroma_h : 0)); y++){
  457. if (desc->comp[0].depth_minus1 >= 8) {
  458. for (x = 0; x<bytes; x++)
  459. ((uint16_t*)dst)[x] = c[p];
  460. }else
  461. memset(dst, c[p], bytes);
  462. dst += frame->linesize[p];
  463. }
  464. }
  465. }
  466. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  467. {
  468. frame->type = FF_BUFFER_TYPE_INTERNAL;
  469. switch (avctx->codec_type) {
  470. case AVMEDIA_TYPE_VIDEO:
  471. return video_get_buffer(avctx, frame);
  472. case AVMEDIA_TYPE_AUDIO:
  473. return audio_get_buffer(avctx, frame);
  474. default:
  475. return -1;
  476. }
  477. }
  478. void ff_init_buffer_info(AVCodecContext *s, AVFrame *frame)
  479. {
  480. if (s->pkt) {
  481. frame->pkt_pts = s->pkt->pts;
  482. frame->pkt_pos = s->pkt->pos;
  483. frame->pkt_duration = s->pkt->duration;
  484. frame->pkt_size = s->pkt->size;
  485. } else {
  486. frame->pkt_pts = AV_NOPTS_VALUE;
  487. frame->pkt_pos = -1;
  488. frame->pkt_duration = 0;
  489. frame->pkt_size = -1;
  490. }
  491. frame->reordered_opaque = s->reordered_opaque;
  492. switch (s->codec->type) {
  493. case AVMEDIA_TYPE_VIDEO:
  494. frame->width = s->width;
  495. frame->height = s->height;
  496. frame->format = s->pix_fmt;
  497. frame->sample_aspect_ratio = s->sample_aspect_ratio;
  498. break;
  499. case AVMEDIA_TYPE_AUDIO:
  500. frame->sample_rate = s->sample_rate;
  501. frame->format = s->sample_fmt;
  502. frame->channel_layout = s->channel_layout;
  503. frame->channels = s->channels;
  504. break;
  505. }
  506. }
  507. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  508. {
  509. ff_init_buffer_info(avctx, frame);
  510. return avctx->get_buffer(avctx, frame);
  511. }
  512. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  513. {
  514. int i;
  515. InternalBuffer *buf, *last;
  516. AVCodecInternal *avci = s->internal;
  517. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  518. assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
  519. assert(avci->buffer_count);
  520. if (avci->buffer) {
  521. buf = NULL; /* avoids warning */
  522. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  523. buf = &avci->buffer[i];
  524. if (buf->data[0] == pic->data[0])
  525. break;
  526. }
  527. av_assert0(i < avci->buffer_count);
  528. avci->buffer_count--;
  529. last = &avci->buffer[avci->buffer_count];
  530. if (buf != last)
  531. FFSWAP(InternalBuffer, *buf, *last);
  532. }
  533. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  534. pic->data[i] = NULL;
  535. // pic->base[i]=NULL;
  536. if (s->debug & FF_DEBUG_BUFFERS)
  537. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  538. "buffers used\n", pic, avci->buffer_count);
  539. }
  540. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  541. {
  542. AVFrame temp_pic;
  543. int i, ret;
  544. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  545. if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
  546. av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  547. pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
  548. s->release_buffer(s, pic);
  549. }
  550. ff_init_buffer_info(s, pic);
  551. /* If no picture return a new buffer */
  552. if (pic->data[0] == NULL) {
  553. /* We will copy from buffer, so must be readable */
  554. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  555. return ff_get_buffer(s, pic);
  556. }
  557. assert(s->pix_fmt == pic->format);
  558. /* If internal buffer type return the same buffer */
  559. if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
  560. return 0;
  561. }
  562. /*
  563. * Not internal type and reget_buffer not overridden, emulate cr buffer
  564. */
  565. temp_pic = *pic;
  566. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  567. pic->data[i] = pic->base[i] = NULL;
  568. pic->opaque = NULL;
  569. /* Allocate new frame */
  570. if ((ret = ff_get_buffer(s, pic)))
  571. return ret;
  572. /* Copy image data from old buffer to new buffer */
  573. av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
  574. s->height);
  575. s->release_buffer(s, &temp_pic); // Release old frame
  576. return 0;
  577. }
  578. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  579. {
  580. int i;
  581. for (i = 0; i < count; i++) {
  582. int r = func(c, (char *)arg + i * size);
  583. if (ret)
  584. ret[i] = r;
  585. }
  586. return 0;
  587. }
  588. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  589. {
  590. int i;
  591. for (i = 0; i < count; i++) {
  592. int r = func(c, arg, i, 0);
  593. if (ret)
  594. ret[i] = r;
  595. }
  596. return 0;
  597. }
  598. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  599. {
  600. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  601. return desc->flags & PIX_FMT_HWACCEL;
  602. }
  603. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  604. {
  605. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  606. ++fmt;
  607. return fmt[0];
  608. }
  609. void avcodec_get_frame_defaults(AVFrame *frame)
  610. {
  611. #if LIBAVCODEC_VERSION_MAJOR >= 55
  612. // extended_data should explicitly be freed when needed, this code is unsafe currently
  613. // also this is not compatible to the <55 ABI/API
  614. if (frame->extended_data != frame->data && 0)
  615. av_freep(&frame->extended_data);
  616. #endif
  617. memset(frame, 0, sizeof(AVFrame));
  618. frame->pts =
  619. frame->pkt_dts =
  620. frame->pkt_pts =
  621. frame->best_effort_timestamp = AV_NOPTS_VALUE;
  622. frame->pkt_duration = 0;
  623. frame->pkt_pos = -1;
  624. frame->pkt_size = -1;
  625. frame->key_frame = 1;
  626. frame->sample_aspect_ratio = (AVRational) {0, 1 };
  627. frame->format = -1; /* unknown */
  628. frame->extended_data = frame->data;
  629. }
  630. AVFrame *avcodec_alloc_frame(void)
  631. {
  632. AVFrame *frame = av_malloc(sizeof(AVFrame));
  633. if (frame == NULL)
  634. return NULL;
  635. frame->extended_data = NULL;
  636. avcodec_get_frame_defaults(frame);
  637. return frame;
  638. }
  639. void avcodec_free_frame(AVFrame **frame)
  640. {
  641. AVFrame *f;
  642. if (!frame || !*frame)
  643. return;
  644. f = *frame;
  645. if (f->extended_data != f->data)
  646. av_freep(&f->extended_data);
  647. av_freep(frame);
  648. }
  649. #define MAKE_ACCESSORS(str, name, type, field) \
  650. type av_##name##_get_##field(const str *s) { return s->field; } \
  651. void av_##name##_set_##field(str *s, type v) { s->field = v; }
  652. MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
  653. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
  654. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
  655. MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
  656. MAKE_ACCESSORS(AVFrame, frame, int, channels)
  657. MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
  658. MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
  659. MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
  660. MAKE_ACCESSORS(AVFrame, frame, int, pkt_size)
  661. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  662. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  663. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  664. {
  665. memset(sub, 0, sizeof(*sub));
  666. sub->pts = AV_NOPTS_VALUE;
  667. }
  668. static int get_bit_rate(AVCodecContext *ctx)
  669. {
  670. int bit_rate;
  671. int bits_per_sample;
  672. switch (ctx->codec_type) {
  673. case AVMEDIA_TYPE_VIDEO:
  674. case AVMEDIA_TYPE_DATA:
  675. case AVMEDIA_TYPE_SUBTITLE:
  676. case AVMEDIA_TYPE_ATTACHMENT:
  677. bit_rate = ctx->bit_rate;
  678. break;
  679. case AVMEDIA_TYPE_AUDIO:
  680. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  681. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  682. break;
  683. default:
  684. bit_rate = 0;
  685. break;
  686. }
  687. return bit_rate;
  688. }
  689. #if FF_API_AVCODEC_OPEN
  690. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  691. {
  692. return avcodec_open2(avctx, codec, NULL);
  693. }
  694. #endif
  695. int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  696. {
  697. int ret = 0;
  698. ff_unlock_avcodec();
  699. ret = avcodec_open2(avctx, codec, options);
  700. ff_lock_avcodec(avctx);
  701. return ret;
  702. }
  703. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  704. {
  705. int ret = 0;
  706. AVDictionary *tmp = NULL;
  707. if (avcodec_is_open(avctx))
  708. return 0;
  709. if ((!codec && !avctx->codec)) {
  710. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  711. return AVERROR(EINVAL);
  712. }
  713. if ((codec && avctx->codec && codec != avctx->codec)) {
  714. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  715. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  716. return AVERROR(EINVAL);
  717. }
  718. if (!codec)
  719. codec = avctx->codec;
  720. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  721. return AVERROR(EINVAL);
  722. if (options)
  723. av_dict_copy(&tmp, *options, 0);
  724. ret = ff_lock_avcodec(avctx);
  725. if (ret < 0)
  726. return ret;
  727. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  728. if (!avctx->internal) {
  729. ret = AVERROR(ENOMEM);
  730. goto end;
  731. }
  732. if (codec->priv_data_size > 0) {
  733. if (!avctx->priv_data) {
  734. avctx->priv_data = av_mallocz(codec->priv_data_size);
  735. if (!avctx->priv_data) {
  736. ret = AVERROR(ENOMEM);
  737. goto end;
  738. }
  739. if (codec->priv_class) {
  740. *(const AVClass **)avctx->priv_data = codec->priv_class;
  741. av_opt_set_defaults(avctx->priv_data);
  742. }
  743. }
  744. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  745. goto free_and_end;
  746. } else {
  747. avctx->priv_data = NULL;
  748. }
  749. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  750. goto free_and_end;
  751. //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
  752. if (!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == AV_CODEC_ID_H264)){
  753. if (avctx->coded_width && avctx->coded_height)
  754. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  755. else if (avctx->width && avctx->height)
  756. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  757. }
  758. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  759. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  760. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  761. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  762. avcodec_set_dimensions(avctx, 0, 0);
  763. }
  764. /* if the decoder init function was already called previously,
  765. * free the already allocated subtitle_header before overwriting it */
  766. if (av_codec_is_decoder(codec))
  767. av_freep(&avctx->subtitle_header);
  768. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  769. ret = AVERROR(EINVAL);
  770. goto free_and_end;
  771. }
  772. avctx->codec = codec;
  773. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  774. avctx->codec_id == AV_CODEC_ID_NONE) {
  775. avctx->codec_type = codec->type;
  776. avctx->codec_id = codec->id;
  777. }
  778. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  779. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  780. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  781. ret = AVERROR(EINVAL);
  782. goto free_and_end;
  783. }
  784. avctx->frame_number = 0;
  785. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  786. if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
  787. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  788. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  789. AVCodec *codec2;
  790. av_log(NULL, AV_LOG_ERROR,
  791. "The %s '%s' is experimental but experimental codecs are not enabled, "
  792. "add '-strict %d' if you want to use it.\n",
  793. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  794. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  795. if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
  796. av_log(NULL, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  797. codec_string, codec2->name);
  798. ret = AVERROR_EXPERIMENTAL;
  799. goto free_and_end;
  800. }
  801. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  802. (!avctx->time_base.num || !avctx->time_base.den)) {
  803. avctx->time_base.num = 1;
  804. avctx->time_base.den = avctx->sample_rate;
  805. }
  806. if (!HAVE_THREADS)
  807. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  808. if (HAVE_THREADS) {
  809. ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  810. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  811. ff_lock_avcodec(avctx);
  812. if (ret < 0)
  813. goto free_and_end;
  814. }
  815. if (HAVE_THREADS && !avctx->thread_opaque
  816. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  817. ret = ff_thread_init(avctx);
  818. if (ret < 0) {
  819. goto free_and_end;
  820. }
  821. }
  822. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  823. avctx->thread_count = 1;
  824. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  825. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  826. avctx->codec->max_lowres);
  827. ret = AVERROR(EINVAL);
  828. goto free_and_end;
  829. }
  830. if (av_codec_is_encoder(avctx->codec)) {
  831. int i;
  832. if (avctx->codec->sample_fmts) {
  833. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  834. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  835. break;
  836. if (avctx->channels == 1 &&
  837. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  838. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  839. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  840. break;
  841. }
  842. }
  843. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  844. char buf[128];
  845. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  846. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  847. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  848. ret = AVERROR(EINVAL);
  849. goto free_and_end;
  850. }
  851. }
  852. if (avctx->codec->pix_fmts) {
  853. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  854. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  855. break;
  856. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  857. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  858. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  859. char buf[128];
  860. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  861. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  862. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  863. ret = AVERROR(EINVAL);
  864. goto free_and_end;
  865. }
  866. }
  867. if (avctx->codec->supported_samplerates) {
  868. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  869. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  870. break;
  871. if (avctx->codec->supported_samplerates[i] == 0) {
  872. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  873. avctx->sample_rate);
  874. ret = AVERROR(EINVAL);
  875. goto free_and_end;
  876. }
  877. }
  878. if (avctx->codec->channel_layouts) {
  879. if (!avctx->channel_layout) {
  880. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  881. } else {
  882. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  883. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  884. break;
  885. if (avctx->codec->channel_layouts[i] == 0) {
  886. char buf[512];
  887. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  888. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  889. ret = AVERROR(EINVAL);
  890. goto free_and_end;
  891. }
  892. }
  893. }
  894. if (avctx->channel_layout && avctx->channels) {
  895. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  896. if (channels != avctx->channels) {
  897. char buf[512];
  898. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  899. av_log(avctx, AV_LOG_ERROR,
  900. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  901. buf, channels, avctx->channels);
  902. ret = AVERROR(EINVAL);
  903. goto free_and_end;
  904. }
  905. } else if (avctx->channel_layout) {
  906. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  907. }
  908. if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
  909. avctx->codec_id != AV_CODEC_ID_PNG // For mplayer
  910. ) {
  911. if (avctx->width <= 0 || avctx->height <= 0) {
  912. av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
  913. ret = AVERROR(EINVAL);
  914. goto free_and_end;
  915. }
  916. }
  917. if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
  918. && avctx->bit_rate>0 && avctx->bit_rate<1000) {
  919. av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extreemly low, did you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
  920. }
  921. if (!avctx->rc_initial_buffer_occupancy)
  922. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  923. }
  924. avctx->pts_correction_num_faulty_pts =
  925. avctx->pts_correction_num_faulty_dts = 0;
  926. avctx->pts_correction_last_pts =
  927. avctx->pts_correction_last_dts = INT64_MIN;
  928. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  929. || avctx->internal->frame_thread_encoder)) {
  930. ret = avctx->codec->init(avctx);
  931. if (ret < 0) {
  932. goto free_and_end;
  933. }
  934. }
  935. ret=0;
  936. if (av_codec_is_decoder(avctx->codec)) {
  937. if (!avctx->bit_rate)
  938. avctx->bit_rate = get_bit_rate(avctx);
  939. /* validate channel layout from the decoder */
  940. if (avctx->channel_layout) {
  941. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  942. if (!avctx->channels)
  943. avctx->channels = channels;
  944. else if (channels != avctx->channels) {
  945. char buf[512];
  946. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  947. av_log(avctx, AV_LOG_WARNING,
  948. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  949. "ignoring specified channel layout\n",
  950. buf, channels, avctx->channels);
  951. avctx->channel_layout = 0;
  952. }
  953. }
  954. if (avctx->channels && avctx->channels < 0 ||
  955. avctx->channels > FF_SANE_NB_CHANNELS) {
  956. ret = AVERROR(EINVAL);
  957. goto free_and_end;
  958. }
  959. if (avctx->sub_charenc) {
  960. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  961. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  962. "supported with subtitles codecs\n");
  963. ret = AVERROR(EINVAL);
  964. goto free_and_end;
  965. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  966. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  967. "subtitles character encoding will be ignored\n",
  968. avctx->codec_descriptor->name);
  969. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  970. } else {
  971. /* input character encoding is set for a text based subtitle
  972. * codec at this point */
  973. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  974. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  975. if (!HAVE_ICONV && avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  976. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  977. "conversion needs a libavcodec built with iconv support "
  978. "for this codec\n");
  979. ret = AVERROR(ENOSYS);
  980. goto free_and_end;
  981. }
  982. }
  983. }
  984. }
  985. end:
  986. ff_unlock_avcodec();
  987. if (options) {
  988. av_dict_free(options);
  989. *options = tmp;
  990. }
  991. return ret;
  992. free_and_end:
  993. av_dict_free(&tmp);
  994. av_freep(&avctx->priv_data);
  995. av_freep(&avctx->internal);
  996. avctx->codec = NULL;
  997. goto end;
  998. }
  999. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
  1000. {
  1001. if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  1002. av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
  1003. return AVERROR(EINVAL);
  1004. }
  1005. if (avctx) {
  1006. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  1007. if (!avpkt->data || avpkt->size < size) {
  1008. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  1009. avpkt->data = avctx->internal->byte_buffer;
  1010. avpkt->size = avctx->internal->byte_buffer_size;
  1011. avpkt->destruct = NULL;
  1012. }
  1013. }
  1014. if (avpkt->data) {
  1015. void *destruct = avpkt->destruct;
  1016. if (avpkt->size < size) {
  1017. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
  1018. return AVERROR(EINVAL);
  1019. }
  1020. av_init_packet(avpkt);
  1021. avpkt->destruct = destruct;
  1022. avpkt->size = size;
  1023. return 0;
  1024. } else {
  1025. int ret = av_new_packet(avpkt, size);
  1026. if (ret < 0)
  1027. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
  1028. return ret;
  1029. }
  1030. }
  1031. int ff_alloc_packet(AVPacket *avpkt, int size)
  1032. {
  1033. return ff_alloc_packet2(NULL, avpkt, size);
  1034. }
  1035. /**
  1036. * Pad last frame with silence.
  1037. */
  1038. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1039. {
  1040. AVFrame *frame = NULL;
  1041. uint8_t *buf = NULL;
  1042. int ret;
  1043. if (!(frame = avcodec_alloc_frame()))
  1044. return AVERROR(ENOMEM);
  1045. *frame = *src;
  1046. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  1047. s->frame_size, s->sample_fmt, 0)) < 0)
  1048. goto fail;
  1049. if (!(buf = av_malloc(ret))) {
  1050. ret = AVERROR(ENOMEM);
  1051. goto fail;
  1052. }
  1053. frame->nb_samples = s->frame_size;
  1054. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  1055. buf, ret, 0)) < 0)
  1056. goto fail;
  1057. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1058. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1059. goto fail;
  1060. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1061. frame->nb_samples - src->nb_samples,
  1062. s->channels, s->sample_fmt)) < 0)
  1063. goto fail;
  1064. *dst = frame;
  1065. return 0;
  1066. fail:
  1067. if (frame->extended_data != frame->data)
  1068. av_freep(&frame->extended_data);
  1069. av_freep(&buf);
  1070. av_freep(&frame);
  1071. return ret;
  1072. }
  1073. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1074. AVPacket *avpkt,
  1075. const AVFrame *frame,
  1076. int *got_packet_ptr)
  1077. {
  1078. AVFrame tmp;
  1079. AVFrame *padded_frame = NULL;
  1080. int ret;
  1081. AVPacket user_pkt = *avpkt;
  1082. int needs_realloc = !user_pkt.data;
  1083. *got_packet_ptr = 0;
  1084. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1085. av_free_packet(avpkt);
  1086. av_init_packet(avpkt);
  1087. return 0;
  1088. }
  1089. /* ensure that extended_data is properly set */
  1090. if (frame && !frame->extended_data) {
  1091. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1092. avctx->channels > AV_NUM_DATA_POINTERS) {
  1093. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1094. "with more than %d channels, but extended_data is not set.\n",
  1095. AV_NUM_DATA_POINTERS);
  1096. return AVERROR(EINVAL);
  1097. }
  1098. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1099. tmp = *frame;
  1100. tmp.extended_data = tmp.data;
  1101. frame = &tmp;
  1102. }
  1103. /* check for valid frame size */
  1104. if (frame) {
  1105. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1106. if (frame->nb_samples > avctx->frame_size) {
  1107. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1108. return AVERROR(EINVAL);
  1109. }
  1110. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1111. if (frame->nb_samples < avctx->frame_size &&
  1112. !avctx->internal->last_audio_frame) {
  1113. ret = pad_last_frame(avctx, &padded_frame, frame);
  1114. if (ret < 0)
  1115. return ret;
  1116. frame = padded_frame;
  1117. avctx->internal->last_audio_frame = 1;
  1118. }
  1119. if (frame->nb_samples != avctx->frame_size) {
  1120. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1121. ret = AVERROR(EINVAL);
  1122. goto end;
  1123. }
  1124. }
  1125. }
  1126. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1127. if (!ret) {
  1128. if (*got_packet_ptr) {
  1129. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1130. if (avpkt->pts == AV_NOPTS_VALUE)
  1131. avpkt->pts = frame->pts;
  1132. if (!avpkt->duration)
  1133. avpkt->duration = ff_samples_to_time_base(avctx,
  1134. frame->nb_samples);
  1135. }
  1136. avpkt->dts = avpkt->pts;
  1137. } else {
  1138. avpkt->size = 0;
  1139. }
  1140. }
  1141. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1142. needs_realloc = 0;
  1143. if (user_pkt.data) {
  1144. if (user_pkt.size >= avpkt->size) {
  1145. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1146. } else {
  1147. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1148. avpkt->size = user_pkt.size;
  1149. ret = -1;
  1150. }
  1151. avpkt->data = user_pkt.data;
  1152. avpkt->destruct = user_pkt.destruct;
  1153. } else {
  1154. if (av_dup_packet(avpkt) < 0) {
  1155. ret = AVERROR(ENOMEM);
  1156. }
  1157. }
  1158. }
  1159. if (!ret) {
  1160. if (needs_realloc && avpkt->data) {
  1161. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1162. if (new_data)
  1163. avpkt->data = new_data;
  1164. }
  1165. avctx->frame_number++;
  1166. }
  1167. if (ret < 0 || !*got_packet_ptr) {
  1168. av_free_packet(avpkt);
  1169. av_init_packet(avpkt);
  1170. goto end;
  1171. }
  1172. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1173. * this needs to be moved to the encoders, but for now we can do it
  1174. * here to simplify things */
  1175. avpkt->flags |= AV_PKT_FLAG_KEY;
  1176. end:
  1177. if (padded_frame) {
  1178. av_freep(&padded_frame->data[0]);
  1179. if (padded_frame->extended_data != padded_frame->data)
  1180. av_freep(&padded_frame->extended_data);
  1181. av_freep(&padded_frame);
  1182. }
  1183. return ret;
  1184. }
  1185. #if FF_API_OLD_ENCODE_AUDIO
  1186. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1187. uint8_t *buf, int buf_size,
  1188. const short *samples)
  1189. {
  1190. AVPacket pkt;
  1191. AVFrame frame0 = { { 0 } };
  1192. AVFrame *frame;
  1193. int ret, samples_size, got_packet;
  1194. av_init_packet(&pkt);
  1195. pkt.data = buf;
  1196. pkt.size = buf_size;
  1197. if (samples) {
  1198. frame = &frame0;
  1199. avcodec_get_frame_defaults(frame);
  1200. if (avctx->frame_size) {
  1201. frame->nb_samples = avctx->frame_size;
  1202. } else {
  1203. /* if frame_size is not set, the number of samples must be
  1204. * calculated from the buffer size */
  1205. int64_t nb_samples;
  1206. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1207. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1208. "support this codec\n");
  1209. return AVERROR(EINVAL);
  1210. }
  1211. nb_samples = (int64_t)buf_size * 8 /
  1212. (av_get_bits_per_sample(avctx->codec_id) *
  1213. avctx->channels);
  1214. if (nb_samples >= INT_MAX)
  1215. return AVERROR(EINVAL);
  1216. frame->nb_samples = nb_samples;
  1217. }
  1218. /* it is assumed that the samples buffer is large enough based on the
  1219. * relevant parameters */
  1220. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1221. frame->nb_samples,
  1222. avctx->sample_fmt, 1);
  1223. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1224. avctx->sample_fmt,
  1225. (const uint8_t *)samples,
  1226. samples_size, 1)) < 0)
  1227. return ret;
  1228. /* fabricate frame pts from sample count.
  1229. * this is needed because the avcodec_encode_audio() API does not have
  1230. * a way for the user to provide pts */
  1231. if (avctx->sample_rate && avctx->time_base.num)
  1232. frame->pts = ff_samples_to_time_base(avctx,
  1233. avctx->internal->sample_count);
  1234. else
  1235. frame->pts = AV_NOPTS_VALUE;
  1236. avctx->internal->sample_count += frame->nb_samples;
  1237. } else {
  1238. frame = NULL;
  1239. }
  1240. got_packet = 0;
  1241. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1242. if (!ret && got_packet && avctx->coded_frame) {
  1243. avctx->coded_frame->pts = pkt.pts;
  1244. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1245. }
  1246. /* free any side data since we cannot return it */
  1247. ff_packet_free_side_data(&pkt);
  1248. if (frame && frame->extended_data != frame->data)
  1249. av_freep(&frame->extended_data);
  1250. return ret ? ret : pkt.size;
  1251. }
  1252. #endif
  1253. #if FF_API_OLD_ENCODE_VIDEO
  1254. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1255. const AVFrame *pict)
  1256. {
  1257. AVPacket pkt;
  1258. int ret, got_packet = 0;
  1259. if (buf_size < FF_MIN_BUFFER_SIZE) {
  1260. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1261. return -1;
  1262. }
  1263. av_init_packet(&pkt);
  1264. pkt.data = buf;
  1265. pkt.size = buf_size;
  1266. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1267. if (!ret && got_packet && avctx->coded_frame) {
  1268. avctx->coded_frame->pts = pkt.pts;
  1269. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1270. }
  1271. /* free any side data since we cannot return it */
  1272. if (pkt.side_data_elems > 0) {
  1273. int i;
  1274. for (i = 0; i < pkt.side_data_elems; i++)
  1275. av_free(pkt.side_data[i].data);
  1276. av_freep(&pkt.side_data);
  1277. pkt.side_data_elems = 0;
  1278. }
  1279. return ret ? ret : pkt.size;
  1280. }
  1281. #endif
  1282. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1283. AVPacket *avpkt,
  1284. const AVFrame *frame,
  1285. int *got_packet_ptr)
  1286. {
  1287. int ret;
  1288. AVPacket user_pkt = *avpkt;
  1289. int needs_realloc = !user_pkt.data;
  1290. *got_packet_ptr = 0;
  1291. if(HAVE_THREADS && avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1292. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1293. if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
  1294. avctx->stats_out[0] = '\0';
  1295. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1296. av_free_packet(avpkt);
  1297. av_init_packet(avpkt);
  1298. avpkt->size = 0;
  1299. return 0;
  1300. }
  1301. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1302. return AVERROR(EINVAL);
  1303. av_assert0(avctx->codec->encode2);
  1304. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1305. av_assert0(ret <= 0);
  1306. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1307. needs_realloc = 0;
  1308. if (user_pkt.data) {
  1309. if (user_pkt.size >= avpkt->size) {
  1310. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1311. } else {
  1312. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1313. avpkt->size = user_pkt.size;
  1314. ret = -1;
  1315. }
  1316. avpkt->data = user_pkt.data;
  1317. avpkt->destruct = user_pkt.destruct;
  1318. } else {
  1319. if (av_dup_packet(avpkt) < 0) {
  1320. ret = AVERROR(ENOMEM);
  1321. }
  1322. }
  1323. }
  1324. if (!ret) {
  1325. if (!*got_packet_ptr)
  1326. avpkt->size = 0;
  1327. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1328. avpkt->pts = avpkt->dts = frame->pts;
  1329. if (needs_realloc && avpkt->data &&
  1330. avpkt->destruct == av_destruct_packet) {
  1331. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1332. if (new_data)
  1333. avpkt->data = new_data;
  1334. }
  1335. avctx->frame_number++;
  1336. }
  1337. if (ret < 0 || !*got_packet_ptr)
  1338. av_free_packet(avpkt);
  1339. emms_c();
  1340. return ret;
  1341. }
  1342. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1343. const AVSubtitle *sub)
  1344. {
  1345. int ret;
  1346. if (sub->start_display_time) {
  1347. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1348. return -1;
  1349. }
  1350. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1351. avctx->frame_number++;
  1352. return ret;
  1353. }
  1354. /**
  1355. * Attempt to guess proper monotonic timestamps for decoded video frames
  1356. * which might have incorrect times. Input timestamps may wrap around, in
  1357. * which case the output will as well.
  1358. *
  1359. * @param pts the pts field of the decoded AVPacket, as passed through
  1360. * AVFrame.pkt_pts
  1361. * @param dts the dts field of the decoded AVPacket
  1362. * @return one of the input values, may be AV_NOPTS_VALUE
  1363. */
  1364. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1365. int64_t reordered_pts, int64_t dts)
  1366. {
  1367. int64_t pts = AV_NOPTS_VALUE;
  1368. if (dts != AV_NOPTS_VALUE) {
  1369. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1370. ctx->pts_correction_last_dts = dts;
  1371. }
  1372. if (reordered_pts != AV_NOPTS_VALUE) {
  1373. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1374. ctx->pts_correction_last_pts = reordered_pts;
  1375. }
  1376. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1377. && reordered_pts != AV_NOPTS_VALUE)
  1378. pts = reordered_pts;
  1379. else
  1380. pts = dts;
  1381. return pts;
  1382. }
  1383. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1384. {
  1385. int size = 0;
  1386. const uint8_t *data;
  1387. uint32_t flags;
  1388. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1389. return;
  1390. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1391. if (!data || size < 4)
  1392. return;
  1393. flags = bytestream_get_le32(&data);
  1394. size -= 4;
  1395. if (size < 4) /* Required for any of the changes */
  1396. return;
  1397. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1398. avctx->channels = bytestream_get_le32(&data);
  1399. size -= 4;
  1400. }
  1401. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1402. if (size < 8)
  1403. return;
  1404. avctx->channel_layout = bytestream_get_le64(&data);
  1405. size -= 8;
  1406. }
  1407. if (size < 4)
  1408. return;
  1409. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1410. avctx->sample_rate = bytestream_get_le32(&data);
  1411. size -= 4;
  1412. }
  1413. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1414. if (size < 8)
  1415. return;
  1416. avctx->width = bytestream_get_le32(&data);
  1417. avctx->height = bytestream_get_le32(&data);
  1418. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1419. size -= 8;
  1420. }
  1421. }
  1422. static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
  1423. {
  1424. int size, ret = 0;
  1425. const uint8_t *side_metadata;
  1426. const uint8_t *end;
  1427. av_dict_free(&avctx->metadata);
  1428. side_metadata = av_packet_get_side_data(avctx->pkt,
  1429. AV_PKT_DATA_STRINGS_METADATA, &size);
  1430. if (!side_metadata)
  1431. goto end;
  1432. end = side_metadata + size;
  1433. while (side_metadata < end) {
  1434. const uint8_t *key = side_metadata;
  1435. const uint8_t *val = side_metadata + strlen(key) + 1;
  1436. int ret = av_dict_set(&frame->metadata, key, val, 0);
  1437. if (ret < 0)
  1438. break;
  1439. side_metadata = val + strlen(val) + 1;
  1440. }
  1441. end:
  1442. avctx->metadata = frame->metadata;
  1443. return ret;
  1444. }
  1445. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1446. int *got_picture_ptr,
  1447. const AVPacket *avpkt)
  1448. {
  1449. int ret;
  1450. // copy to ensure we do not change avpkt
  1451. AVPacket tmp = *avpkt;
  1452. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1453. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1454. return AVERROR(EINVAL);
  1455. }
  1456. *got_picture_ptr = 0;
  1457. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1458. return AVERROR(EINVAL);
  1459. avcodec_get_frame_defaults(picture);
  1460. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1461. int did_split = av_packet_split_side_data(&tmp);
  1462. apply_param_change(avctx, &tmp);
  1463. avctx->pkt = &tmp;
  1464. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1465. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1466. &tmp);
  1467. else {
  1468. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1469. &tmp);
  1470. picture->pkt_dts = avpkt->dts;
  1471. if(!avctx->has_b_frames){
  1472. picture->pkt_pos = avpkt->pos;
  1473. }
  1474. //FIXME these should be under if(!avctx->has_b_frames)
  1475. /* get_buffer is supposed to set frame parameters */
  1476. if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
  1477. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1478. if (!picture->width) picture->width = avctx->width;
  1479. if (!picture->height) picture->height = avctx->height;
  1480. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  1481. }
  1482. }
  1483. add_metadata_from_side_data(avctx, picture);
  1484. emms_c(); //needed to avoid an emms_c() call before every return;
  1485. avctx->pkt = NULL;
  1486. if (did_split) {
  1487. ff_packet_free_side_data(&tmp);
  1488. if(ret == tmp.size)
  1489. ret = avpkt->size;
  1490. }
  1491. if (*got_picture_ptr){
  1492. avctx->frame_number++;
  1493. picture->best_effort_timestamp = guess_correct_pts(avctx,
  1494. picture->pkt_pts,
  1495. picture->pkt_dts);
  1496. }
  1497. } else
  1498. ret = 0;
  1499. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1500. * make sure it's set correctly */
  1501. picture->extended_data = picture->data;
  1502. return ret;
  1503. }
  1504. #if FF_API_OLD_DECODE_AUDIO
  1505. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1506. int *frame_size_ptr,
  1507. AVPacket *avpkt)
  1508. {
  1509. AVFrame frame = { { 0 } };
  1510. int ret, got_frame = 0;
  1511. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1512. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1513. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1514. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1515. "avcodec_decode_audio4()\n");
  1516. avctx->get_buffer = avcodec_default_get_buffer;
  1517. avctx->release_buffer = avcodec_default_release_buffer;
  1518. }
  1519. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1520. if (ret >= 0 && got_frame) {
  1521. int ch, plane_size;
  1522. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1523. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1524. frame.nb_samples,
  1525. avctx->sample_fmt, 1);
  1526. if (*frame_size_ptr < data_size) {
  1527. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1528. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1529. return AVERROR(EINVAL);
  1530. }
  1531. memcpy(samples, frame.extended_data[0], plane_size);
  1532. if (planar && avctx->channels > 1) {
  1533. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1534. for (ch = 1; ch < avctx->channels; ch++) {
  1535. memcpy(out, frame.extended_data[ch], plane_size);
  1536. out += plane_size;
  1537. }
  1538. }
  1539. *frame_size_ptr = data_size;
  1540. } else {
  1541. *frame_size_ptr = 0;
  1542. }
  1543. return ret;
  1544. }
  1545. #endif
  1546. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1547. AVFrame *frame,
  1548. int *got_frame_ptr,
  1549. const AVPacket *avpkt)
  1550. {
  1551. int planar, channels;
  1552. int ret = 0;
  1553. *got_frame_ptr = 0;
  1554. if (!avpkt->data && avpkt->size) {
  1555. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1556. return AVERROR(EINVAL);
  1557. }
  1558. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1559. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1560. return AVERROR(EINVAL);
  1561. }
  1562. avcodec_get_frame_defaults(frame);
  1563. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1564. uint8_t *side;
  1565. int side_size;
  1566. // copy to ensure we do not change avpkt
  1567. AVPacket tmp = *avpkt;
  1568. int did_split = av_packet_split_side_data(&tmp);
  1569. apply_param_change(avctx, &tmp);
  1570. avctx->pkt = &tmp;
  1571. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1572. if (ret >= 0 && *got_frame_ptr) {
  1573. avctx->frame_number++;
  1574. frame->pkt_dts = avpkt->dts;
  1575. frame->best_effort_timestamp = guess_correct_pts(avctx,
  1576. frame->pkt_pts,
  1577. frame->pkt_dts);
  1578. if (frame->format == AV_SAMPLE_FMT_NONE)
  1579. frame->format = avctx->sample_fmt;
  1580. if (!frame->channel_layout)
  1581. frame->channel_layout = avctx->channel_layout;
  1582. if (!frame->channels)
  1583. frame->channels = avctx->channels;
  1584. if (!frame->sample_rate)
  1585. frame->sample_rate = avctx->sample_rate;
  1586. }
  1587. add_metadata_from_side_data(avctx, frame);
  1588. side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  1589. if(side && side_size>=10) {
  1590. avctx->internal->skip_samples = AV_RL32(side);
  1591. av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
  1592. avctx->internal->skip_samples);
  1593. }
  1594. if (avctx->internal->skip_samples && *got_frame_ptr) {
  1595. if(frame->nb_samples <= avctx->internal->skip_samples){
  1596. *got_frame_ptr = 0;
  1597. avctx->internal->skip_samples -= frame->nb_samples;
  1598. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  1599. avctx->internal->skip_samples);
  1600. } else {
  1601. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  1602. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  1603. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1604. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  1605. (AVRational){1, avctx->sample_rate},
  1606. avctx->pkt_timebase);
  1607. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  1608. frame->pkt_pts += diff_ts;
  1609. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  1610. frame->pkt_dts += diff_ts;
  1611. if (frame->pkt_duration >= diff_ts)
  1612. frame->pkt_duration -= diff_ts;
  1613. } else {
  1614. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  1615. }
  1616. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  1617. avctx->internal->skip_samples, frame->nb_samples);
  1618. frame->nb_samples -= avctx->internal->skip_samples;
  1619. avctx->internal->skip_samples = 0;
  1620. }
  1621. }
  1622. avctx->pkt = NULL;
  1623. if (did_split) {
  1624. ff_packet_free_side_data(&tmp);
  1625. if(ret == tmp.size)
  1626. ret = avpkt->size;
  1627. }
  1628. }
  1629. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1630. * make sure it's set correctly; assume decoders that actually use
  1631. * extended_data are doing it correctly */
  1632. if (*got_frame_ptr) {
  1633. planar = av_sample_fmt_is_planar(frame->format);
  1634. channels = frame->channels;
  1635. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1636. frame->extended_data = frame->data;
  1637. } else {
  1638. frame->extended_data = NULL;
  1639. }
  1640. return ret;
  1641. }
  1642. #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
  1643. static int recode_subtitle(AVCodecContext *avctx,
  1644. AVPacket *outpkt, const AVPacket *inpkt)
  1645. {
  1646. #if HAVE_ICONV
  1647. iconv_t cd = (iconv_t)-1;
  1648. int ret = 0;
  1649. char *inb, *outb;
  1650. size_t inl, outl;
  1651. AVPacket tmp;
  1652. #endif
  1653. if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER)
  1654. return 0;
  1655. #if HAVE_ICONV
  1656. cd = iconv_open("UTF-8", avctx->sub_charenc);
  1657. if (cd == (iconv_t)-1) {
  1658. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  1659. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  1660. ret = AVERROR(errno);
  1661. goto end;
  1662. }
  1663. inb = inpkt->data;
  1664. inl = inpkt->size;
  1665. if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
  1666. av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
  1667. ret = AVERROR(ENOMEM);
  1668. goto end;
  1669. }
  1670. ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
  1671. if (ret < 0)
  1672. goto end;
  1673. outpkt->data = tmp.data;
  1674. outpkt->size = tmp.size;
  1675. outb = outpkt->data;
  1676. outl = outpkt->size;
  1677. if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
  1678. iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
  1679. outl >= outpkt->size || inl != 0) {
  1680. av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
  1681. "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
  1682. av_free_packet(&tmp);
  1683. ret = AVERROR(errno);
  1684. goto end;
  1685. }
  1686. outpkt->size -= outl;
  1687. outpkt->data[outpkt->size - 1] = '\0';
  1688. end:
  1689. if (cd != (iconv_t)-1)
  1690. iconv_close(cd);
  1691. return ret;
  1692. #else
  1693. av_assert0(!"requesting subtitles recoding without iconv");
  1694. #endif
  1695. }
  1696. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1697. int *got_sub_ptr,
  1698. AVPacket *avpkt)
  1699. {
  1700. int ret = 0;
  1701. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  1702. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  1703. return AVERROR(EINVAL);
  1704. }
  1705. *got_sub_ptr = 0;
  1706. avcodec_get_subtitle_defaults(sub);
  1707. if (avpkt->size) {
  1708. AVPacket pkt_recoded;
  1709. AVPacket tmp = *avpkt;
  1710. int did_split = av_packet_split_side_data(&tmp);
  1711. //apply_param_change(avctx, &tmp);
  1712. pkt_recoded = tmp;
  1713. ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
  1714. if (ret < 0) {
  1715. *got_sub_ptr = 0;
  1716. } else {
  1717. avctx->pkt = &pkt_recoded;
  1718. if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  1719. sub->pts = av_rescale_q(avpkt->pts,
  1720. avctx->pkt_timebase, AV_TIME_BASE_Q);
  1721. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
  1722. if (tmp.data != pkt_recoded.data)
  1723. av_free(pkt_recoded.data);
  1724. sub->format = !(avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB);
  1725. avctx->pkt = NULL;
  1726. }
  1727. if (did_split) {
  1728. ff_packet_free_side_data(&tmp);
  1729. if(ret == tmp.size)
  1730. ret = avpkt->size;
  1731. }
  1732. if (*got_sub_ptr)
  1733. avctx->frame_number++;
  1734. }
  1735. return ret;
  1736. }
  1737. void avsubtitle_free(AVSubtitle *sub)
  1738. {
  1739. int i;
  1740. for (i = 0; i < sub->num_rects; i++) {
  1741. av_freep(&sub->rects[i]->pict.data[0]);
  1742. av_freep(&sub->rects[i]->pict.data[1]);
  1743. av_freep(&sub->rects[i]->pict.data[2]);
  1744. av_freep(&sub->rects[i]->pict.data[3]);
  1745. av_freep(&sub->rects[i]->text);
  1746. av_freep(&sub->rects[i]->ass);
  1747. av_freep(&sub->rects[i]);
  1748. }
  1749. av_freep(&sub->rects);
  1750. memset(sub, 0, sizeof(AVSubtitle));
  1751. }
  1752. av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
  1753. {
  1754. int ret = 0;
  1755. ff_unlock_avcodec();
  1756. ret = avcodec_close(avctx);
  1757. ff_lock_avcodec(NULL);
  1758. return ret;
  1759. }
  1760. av_cold int avcodec_close(AVCodecContext *avctx)
  1761. {
  1762. int ret = ff_lock_avcodec(avctx);
  1763. if (ret < 0)
  1764. return ret;
  1765. if (avcodec_is_open(avctx)) {
  1766. if (HAVE_THREADS && avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  1767. ff_unlock_avcodec();
  1768. ff_frame_thread_encoder_free(avctx);
  1769. ff_lock_avcodec(avctx);
  1770. }
  1771. if (HAVE_THREADS && avctx->thread_opaque)
  1772. ff_thread_free(avctx);
  1773. if (avctx->codec && avctx->codec->close)
  1774. avctx->codec->close(avctx);
  1775. avcodec_default_free_buffers(avctx);
  1776. avctx->coded_frame = NULL;
  1777. avctx->internal->byte_buffer_size = 0;
  1778. av_freep(&avctx->internal->byte_buffer);
  1779. av_freep(&avctx->internal);
  1780. av_dict_free(&avctx->metadata);
  1781. }
  1782. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1783. av_opt_free(avctx->priv_data);
  1784. av_opt_free(avctx);
  1785. av_freep(&avctx->priv_data);
  1786. if (av_codec_is_encoder(avctx->codec))
  1787. av_freep(&avctx->extradata);
  1788. avctx->codec = NULL;
  1789. avctx->active_thread_type = 0;
  1790. ff_unlock_avcodec();
  1791. return 0;
  1792. }
  1793. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  1794. {
  1795. switch(id){
  1796. //This is for future deprecatec codec ids, its empty since
  1797. //last major bump but will fill up again over time, please don't remove it
  1798. // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
  1799. case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
  1800. case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
  1801. default : return id;
  1802. }
  1803. }
  1804. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  1805. {
  1806. AVCodec *p, *experimental = NULL;
  1807. p = first_avcodec;
  1808. id= remap_deprecated_codec_id(id);
  1809. while (p) {
  1810. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  1811. p->id == id) {
  1812. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1813. experimental = p;
  1814. } else
  1815. return p;
  1816. }
  1817. p = p->next;
  1818. }
  1819. return experimental;
  1820. }
  1821. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1822. {
  1823. return find_encdec(id, 1);
  1824. }
  1825. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1826. {
  1827. AVCodec *p;
  1828. if (!name)
  1829. return NULL;
  1830. p = first_avcodec;
  1831. while (p) {
  1832. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1833. return p;
  1834. p = p->next;
  1835. }
  1836. return NULL;
  1837. }
  1838. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1839. {
  1840. return find_encdec(id, 0);
  1841. }
  1842. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1843. {
  1844. AVCodec *p;
  1845. if (!name)
  1846. return NULL;
  1847. p = first_avcodec;
  1848. while (p) {
  1849. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1850. return p;
  1851. p = p->next;
  1852. }
  1853. return NULL;
  1854. }
  1855. const char *avcodec_get_name(enum AVCodecID id)
  1856. {
  1857. const AVCodecDescriptor *cd;
  1858. AVCodec *codec;
  1859. if (id == AV_CODEC_ID_NONE)
  1860. return "none";
  1861. cd = avcodec_descriptor_get(id);
  1862. if (cd)
  1863. return cd->name;
  1864. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1865. codec = avcodec_find_decoder(id);
  1866. if (codec)
  1867. return codec->name;
  1868. codec = avcodec_find_encoder(id);
  1869. if (codec)
  1870. return codec->name;
  1871. return "unknown_codec";
  1872. }
  1873. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1874. {
  1875. int i, len, ret = 0;
  1876. #define IS_PRINT(x) \
  1877. (((x) >= '0' && (x) <= '9') || \
  1878. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1879. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  1880. for (i = 0; i < 4; i++) {
  1881. len = snprintf(buf, buf_size,
  1882. IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1883. buf += len;
  1884. buf_size = buf_size > len ? buf_size - len : 0;
  1885. ret += len;
  1886. codec_tag >>= 8;
  1887. }
  1888. return ret;
  1889. }
  1890. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1891. {
  1892. const char *codec_type;
  1893. const char *codec_name;
  1894. const char *profile = NULL;
  1895. const AVCodec *p;
  1896. int bitrate;
  1897. AVRational display_aspect_ratio;
  1898. if (!buf || buf_size <= 0)
  1899. return;
  1900. codec_type = av_get_media_type_string(enc->codec_type);
  1901. codec_name = avcodec_get_name(enc->codec_id);
  1902. if (enc->profile != FF_PROFILE_UNKNOWN) {
  1903. if (enc->codec)
  1904. p = enc->codec;
  1905. else
  1906. p = encode ? avcodec_find_encoder(enc->codec_id) :
  1907. avcodec_find_decoder(enc->codec_id);
  1908. if (p)
  1909. profile = av_get_profile_name(p, enc->profile);
  1910. }
  1911. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  1912. codec_name, enc->mb_decision ? " (hq)" : "");
  1913. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1914. if (profile)
  1915. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1916. if (enc->codec_tag) {
  1917. char tag_buf[32];
  1918. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1919. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1920. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  1921. }
  1922. switch (enc->codec_type) {
  1923. case AVMEDIA_TYPE_VIDEO:
  1924. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  1925. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1926. ", %s",
  1927. av_get_pix_fmt_name(enc->pix_fmt));
  1928. if (enc->bits_per_raw_sample &&
  1929. enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
  1930. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1931. " (%d bpc)", enc->bits_per_raw_sample);
  1932. }
  1933. if (enc->width) {
  1934. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1935. ", %dx%d",
  1936. enc->width, enc->height);
  1937. if (enc->sample_aspect_ratio.num) {
  1938. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1939. enc->width * enc->sample_aspect_ratio.num,
  1940. enc->height * enc->sample_aspect_ratio.den,
  1941. 1024 * 1024);
  1942. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1943. " [SAR %d:%d DAR %d:%d]",
  1944. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1945. display_aspect_ratio.num, display_aspect_ratio.den);
  1946. }
  1947. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1948. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1949. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1950. ", %d/%d",
  1951. enc->time_base.num / g, enc->time_base.den / g);
  1952. }
  1953. }
  1954. if (encode) {
  1955. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1956. ", q=%d-%d", enc->qmin, enc->qmax);
  1957. }
  1958. break;
  1959. case AVMEDIA_TYPE_AUDIO:
  1960. if (enc->sample_rate) {
  1961. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1962. ", %d Hz", enc->sample_rate);
  1963. }
  1964. av_strlcat(buf, ", ", buf_size);
  1965. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1966. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1967. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1968. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1969. }
  1970. break;
  1971. case AVMEDIA_TYPE_DATA:
  1972. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1973. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1974. if (g)
  1975. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1976. ", %d/%d",
  1977. enc->time_base.num / g, enc->time_base.den / g);
  1978. }
  1979. break;
  1980. default:
  1981. return;
  1982. }
  1983. if (encode) {
  1984. if (enc->flags & CODEC_FLAG_PASS1)
  1985. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1986. ", pass 1");
  1987. if (enc->flags & CODEC_FLAG_PASS2)
  1988. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1989. ", pass 2");
  1990. }
  1991. bitrate = get_bit_rate(enc);
  1992. if (bitrate != 0) {
  1993. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1994. ", %d kb/s", bitrate / 1000);
  1995. }
  1996. }
  1997. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1998. {
  1999. const AVProfile *p;
  2000. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  2001. return NULL;
  2002. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  2003. if (p->profile == profile)
  2004. return p->name;
  2005. return NULL;
  2006. }
  2007. unsigned avcodec_version(void)
  2008. {
  2009. // av_assert0(AV_CODEC_ID_V410==164);
  2010. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  2011. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  2012. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  2013. av_assert0(AV_CODEC_ID_SRT==94216);
  2014. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  2015. return LIBAVCODEC_VERSION_INT;
  2016. }
  2017. const char *avcodec_configuration(void)
  2018. {
  2019. return FFMPEG_CONFIGURATION;
  2020. }
  2021. const char *avcodec_license(void)
  2022. {
  2023. #define LICENSE_PREFIX "libavcodec license: "
  2024. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  2025. }
  2026. void avcodec_flush_buffers(AVCodecContext *avctx)
  2027. {
  2028. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2029. ff_thread_flush(avctx);
  2030. else if (avctx->codec->flush)
  2031. avctx->codec->flush(avctx);
  2032. avctx->pts_correction_last_pts =
  2033. avctx->pts_correction_last_dts = INT64_MIN;
  2034. }
  2035. static void video_free_buffers(AVCodecContext *s)
  2036. {
  2037. AVCodecInternal *avci = s->internal;
  2038. int i, j;
  2039. if (!avci->buffer)
  2040. return;
  2041. if (avci->buffer_count)
  2042. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  2043. avci->buffer_count);
  2044. for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
  2045. InternalBuffer *buf = &avci->buffer[i];
  2046. for (j = 0; j < 4; j++) {
  2047. av_freep(&buf->base[j]);
  2048. buf->data[j] = NULL;
  2049. }
  2050. }
  2051. av_freep(&avci->buffer);
  2052. avci->buffer_count = 0;
  2053. }
  2054. static void audio_free_buffers(AVCodecContext *avctx)
  2055. {
  2056. AVCodecInternal *avci = avctx->internal;
  2057. av_freep(&avci->audio_data);
  2058. }
  2059. void avcodec_default_free_buffers(AVCodecContext *avctx)
  2060. {
  2061. switch (avctx->codec_type) {
  2062. case AVMEDIA_TYPE_VIDEO:
  2063. video_free_buffers(avctx);
  2064. break;
  2065. case AVMEDIA_TYPE_AUDIO:
  2066. audio_free_buffers(avctx);
  2067. break;
  2068. default:
  2069. break;
  2070. }
  2071. }
  2072. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  2073. {
  2074. switch (codec_id) {
  2075. case AV_CODEC_ID_8SVX_EXP:
  2076. case AV_CODEC_ID_8SVX_FIB:
  2077. case AV_CODEC_ID_ADPCM_CT:
  2078. case AV_CODEC_ID_ADPCM_IMA_APC:
  2079. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  2080. case AV_CODEC_ID_ADPCM_IMA_OKI:
  2081. case AV_CODEC_ID_ADPCM_IMA_WS:
  2082. case AV_CODEC_ID_ADPCM_G722:
  2083. case AV_CODEC_ID_ADPCM_YAMAHA:
  2084. return 4;
  2085. case AV_CODEC_ID_PCM_ALAW:
  2086. case AV_CODEC_ID_PCM_MULAW:
  2087. case AV_CODEC_ID_PCM_S8:
  2088. case AV_CODEC_ID_PCM_S8_PLANAR:
  2089. case AV_CODEC_ID_PCM_U8:
  2090. case AV_CODEC_ID_PCM_ZORK:
  2091. return 8;
  2092. case AV_CODEC_ID_PCM_S16BE:
  2093. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  2094. case AV_CODEC_ID_PCM_S16LE:
  2095. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  2096. case AV_CODEC_ID_PCM_U16BE:
  2097. case AV_CODEC_ID_PCM_U16LE:
  2098. return 16;
  2099. case AV_CODEC_ID_PCM_S24DAUD:
  2100. case AV_CODEC_ID_PCM_S24BE:
  2101. case AV_CODEC_ID_PCM_S24LE:
  2102. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  2103. case AV_CODEC_ID_PCM_U24BE:
  2104. case AV_CODEC_ID_PCM_U24LE:
  2105. return 24;
  2106. case AV_CODEC_ID_PCM_S32BE:
  2107. case AV_CODEC_ID_PCM_S32LE:
  2108. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  2109. case AV_CODEC_ID_PCM_U32BE:
  2110. case AV_CODEC_ID_PCM_U32LE:
  2111. case AV_CODEC_ID_PCM_F32BE:
  2112. case AV_CODEC_ID_PCM_F32LE:
  2113. return 32;
  2114. case AV_CODEC_ID_PCM_F64BE:
  2115. case AV_CODEC_ID_PCM_F64LE:
  2116. return 64;
  2117. default:
  2118. return 0;
  2119. }
  2120. }
  2121. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  2122. {
  2123. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  2124. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2125. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2126. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2127. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2128. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2129. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2130. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2131. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2132. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2133. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2134. };
  2135. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  2136. return AV_CODEC_ID_NONE;
  2137. if (be < 0 || be > 1)
  2138. be = AV_NE(1, 0);
  2139. return map[fmt][be];
  2140. }
  2141. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2142. {
  2143. switch (codec_id) {
  2144. case AV_CODEC_ID_ADPCM_SBPRO_2:
  2145. return 2;
  2146. case AV_CODEC_ID_ADPCM_SBPRO_3:
  2147. return 3;
  2148. case AV_CODEC_ID_ADPCM_SBPRO_4:
  2149. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2150. case AV_CODEC_ID_ADPCM_IMA_QT:
  2151. case AV_CODEC_ID_ADPCM_SWF:
  2152. case AV_CODEC_ID_ADPCM_MS:
  2153. return 4;
  2154. default:
  2155. return av_get_exact_bits_per_sample(codec_id);
  2156. }
  2157. }
  2158. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2159. {
  2160. int id, sr, ch, ba, tag, bps;
  2161. id = avctx->codec_id;
  2162. sr = avctx->sample_rate;
  2163. ch = avctx->channels;
  2164. ba = avctx->block_align;
  2165. tag = avctx->codec_tag;
  2166. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  2167. /* codecs with an exact constant bits per sample */
  2168. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  2169. return (frame_bytes * 8LL) / (bps * ch);
  2170. bps = avctx->bits_per_coded_sample;
  2171. /* codecs with a fixed packet duration */
  2172. switch (id) {
  2173. case AV_CODEC_ID_ADPCM_ADX: return 32;
  2174. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  2175. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  2176. case AV_CODEC_ID_AMR_NB:
  2177. case AV_CODEC_ID_EVRC:
  2178. case AV_CODEC_ID_GSM:
  2179. case AV_CODEC_ID_QCELP:
  2180. case AV_CODEC_ID_RA_288: return 160;
  2181. case AV_CODEC_ID_AMR_WB:
  2182. case AV_CODEC_ID_GSM_MS: return 320;
  2183. case AV_CODEC_ID_MP1: return 384;
  2184. case AV_CODEC_ID_ATRAC1: return 512;
  2185. case AV_CODEC_ID_ATRAC3: return 1024;
  2186. case AV_CODEC_ID_MP2:
  2187. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2188. case AV_CODEC_ID_AC3: return 1536;
  2189. }
  2190. if (sr > 0) {
  2191. /* calc from sample rate */
  2192. if (id == AV_CODEC_ID_TTA)
  2193. return 256 * sr / 245;
  2194. if (ch > 0) {
  2195. /* calc from sample rate and channels */
  2196. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2197. return (480 << (sr / 22050)) / ch;
  2198. }
  2199. }
  2200. if (ba > 0) {
  2201. /* calc from block_align */
  2202. if (id == AV_CODEC_ID_SIPR) {
  2203. switch (ba) {
  2204. case 20: return 160;
  2205. case 19: return 144;
  2206. case 29: return 288;
  2207. case 37: return 480;
  2208. }
  2209. } else if (id == AV_CODEC_ID_ILBC) {
  2210. switch (ba) {
  2211. case 38: return 160;
  2212. case 50: return 240;
  2213. }
  2214. }
  2215. }
  2216. if (frame_bytes > 0) {
  2217. /* calc from frame_bytes only */
  2218. if (id == AV_CODEC_ID_TRUESPEECH)
  2219. return 240 * (frame_bytes / 32);
  2220. if (id == AV_CODEC_ID_NELLYMOSER)
  2221. return 256 * (frame_bytes / 64);
  2222. if (id == AV_CODEC_ID_RA_144)
  2223. return 160 * (frame_bytes / 20);
  2224. if (id == AV_CODEC_ID_G723_1)
  2225. return 240 * (frame_bytes / 24);
  2226. if (bps > 0) {
  2227. /* calc from frame_bytes and bits_per_coded_sample */
  2228. if (id == AV_CODEC_ID_ADPCM_G726)
  2229. return frame_bytes * 8 / bps;
  2230. }
  2231. if (ch > 0) {
  2232. /* calc from frame_bytes and channels */
  2233. switch (id) {
  2234. case AV_CODEC_ID_ADPCM_AFC:
  2235. return frame_bytes / (9 * ch) * 16;
  2236. case AV_CODEC_ID_ADPCM_4XM:
  2237. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2238. return (frame_bytes - 4 * ch) * 2 / ch;
  2239. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2240. return (frame_bytes - 4) * 2 / ch;
  2241. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2242. return (frame_bytes - 8) * 2 / ch;
  2243. case AV_CODEC_ID_ADPCM_XA:
  2244. return (frame_bytes / 128) * 224 / ch;
  2245. case AV_CODEC_ID_INTERPLAY_DPCM:
  2246. return (frame_bytes - 6 - ch) / ch;
  2247. case AV_CODEC_ID_ROQ_DPCM:
  2248. return (frame_bytes - 8) / ch;
  2249. case AV_CODEC_ID_XAN_DPCM:
  2250. return (frame_bytes - 2 * ch) / ch;
  2251. case AV_CODEC_ID_MACE3:
  2252. return 3 * frame_bytes / ch;
  2253. case AV_CODEC_ID_MACE6:
  2254. return 6 * frame_bytes / ch;
  2255. case AV_CODEC_ID_PCM_LXF:
  2256. return 2 * (frame_bytes / (5 * ch));
  2257. case AV_CODEC_ID_IAC:
  2258. case AV_CODEC_ID_IMC:
  2259. return 4 * frame_bytes / ch;
  2260. }
  2261. if (tag) {
  2262. /* calc from frame_bytes, channels, and codec_tag */
  2263. if (id == AV_CODEC_ID_SOL_DPCM) {
  2264. if (tag == 3)
  2265. return frame_bytes / ch;
  2266. else
  2267. return frame_bytes * 2 / ch;
  2268. }
  2269. }
  2270. if (ba > 0) {
  2271. /* calc from frame_bytes, channels, and block_align */
  2272. int blocks = frame_bytes / ba;
  2273. switch (avctx->codec_id) {
  2274. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2275. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  2276. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2277. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2278. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2279. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2280. case AV_CODEC_ID_ADPCM_MS:
  2281. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2282. }
  2283. }
  2284. if (bps > 0) {
  2285. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2286. switch (avctx->codec_id) {
  2287. case AV_CODEC_ID_PCM_DVD:
  2288. if(bps<4)
  2289. return 0;
  2290. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2291. case AV_CODEC_ID_PCM_BLURAY:
  2292. if(bps<4)
  2293. return 0;
  2294. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2295. case AV_CODEC_ID_S302M:
  2296. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2297. }
  2298. }
  2299. }
  2300. }
  2301. return 0;
  2302. }
  2303. #if !HAVE_THREADS
  2304. int ff_thread_init(AVCodecContext *s)
  2305. {
  2306. return -1;
  2307. }
  2308. #endif
  2309. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2310. {
  2311. unsigned int n = 0;
  2312. while (v >= 0xff) {
  2313. *s++ = 0xff;
  2314. v -= 0xff;
  2315. n++;
  2316. }
  2317. *s = v;
  2318. n++;
  2319. return n;
  2320. }
  2321. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2322. {
  2323. int i;
  2324. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2325. return i;
  2326. }
  2327. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2328. {
  2329. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  2330. "version to the newest one from Git. If the problem still "
  2331. "occurs, it means that your file has a feature which has not "
  2332. "been implemented.\n", feature);
  2333. if(want_sample)
  2334. av_log_ask_for_sample(avc, NULL);
  2335. }
  2336. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2337. {
  2338. va_list argument_list;
  2339. va_start(argument_list, msg);
  2340. if (msg)
  2341. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2342. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2343. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  2344. "and contact the ffmpeg-devel mailing list.\n");
  2345. va_end(argument_list);
  2346. }
  2347. static AVHWAccel *first_hwaccel = NULL;
  2348. void av_register_hwaccel(AVHWAccel *hwaccel)
  2349. {
  2350. AVHWAccel **p = &first_hwaccel;
  2351. while (*p)
  2352. p = &(*p)->next;
  2353. *p = hwaccel;
  2354. hwaccel->next = NULL;
  2355. }
  2356. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  2357. {
  2358. return hwaccel ? hwaccel->next : first_hwaccel;
  2359. }
  2360. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
  2361. {
  2362. AVHWAccel *hwaccel = NULL;
  2363. while ((hwaccel = av_hwaccel_next(hwaccel)))
  2364. if (hwaccel->id == codec_id
  2365. && hwaccel->pix_fmt == pix_fmt)
  2366. return hwaccel;
  2367. return NULL;
  2368. }
  2369. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2370. {
  2371. if (ff_lockmgr_cb) {
  2372. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  2373. return -1;
  2374. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  2375. return -1;
  2376. }
  2377. ff_lockmgr_cb = cb;
  2378. if (ff_lockmgr_cb) {
  2379. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  2380. return -1;
  2381. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2382. return -1;
  2383. }
  2384. return 0;
  2385. }
  2386. int ff_lock_avcodec(AVCodecContext *log_ctx)
  2387. {
  2388. if (ff_lockmgr_cb) {
  2389. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  2390. return -1;
  2391. }
  2392. entangled_thread_counter++;
  2393. if (entangled_thread_counter != 1) {
  2394. av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
  2395. ff_avcodec_locked = 1;
  2396. ff_unlock_avcodec();
  2397. return AVERROR(EINVAL);
  2398. }
  2399. av_assert0(!ff_avcodec_locked);
  2400. ff_avcodec_locked = 1;
  2401. return 0;
  2402. }
  2403. int ff_unlock_avcodec(void)
  2404. {
  2405. av_assert0(ff_avcodec_locked);
  2406. ff_avcodec_locked = 0;
  2407. entangled_thread_counter--;
  2408. if (ff_lockmgr_cb) {
  2409. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
  2410. return -1;
  2411. }
  2412. return 0;
  2413. }
  2414. int avpriv_lock_avformat(void)
  2415. {
  2416. if (ff_lockmgr_cb) {
  2417. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2418. return -1;
  2419. }
  2420. return 0;
  2421. }
  2422. int avpriv_unlock_avformat(void)
  2423. {
  2424. if (ff_lockmgr_cb) {
  2425. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2426. return -1;
  2427. }
  2428. return 0;
  2429. }
  2430. unsigned int avpriv_toupper4(unsigned int x)
  2431. {
  2432. return toupper(x & 0xFF)
  2433. + (toupper((x >> 8) & 0xFF) << 8)
  2434. + (toupper((x >> 16) & 0xFF) << 16)
  2435. + (toupper((x >> 24) & 0xFF) << 24);
  2436. }
  2437. #if !HAVE_THREADS
  2438. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  2439. {
  2440. f->owner = avctx;
  2441. return ff_get_buffer(avctx, f);
  2442. }
  2443. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  2444. {
  2445. f->owner->release_buffer(f->owner, f);
  2446. }
  2447. void ff_thread_finish_setup(AVCodecContext *avctx)
  2448. {
  2449. }
  2450. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  2451. {
  2452. }
  2453. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  2454. {
  2455. }
  2456. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2457. {
  2458. return 1;
  2459. }
  2460. #endif
  2461. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  2462. {
  2463. AVCodec *c= avcodec_find_decoder(codec_id);
  2464. if(!c)
  2465. c= avcodec_find_encoder(codec_id);
  2466. if(c)
  2467. return c->type;
  2468. if (codec_id <= AV_CODEC_ID_NONE)
  2469. return AVMEDIA_TYPE_UNKNOWN;
  2470. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  2471. return AVMEDIA_TYPE_VIDEO;
  2472. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  2473. return AVMEDIA_TYPE_AUDIO;
  2474. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  2475. return AVMEDIA_TYPE_SUBTITLE;
  2476. return AVMEDIA_TYPE_UNKNOWN;
  2477. }
  2478. int avcodec_is_open(AVCodecContext *s)
  2479. {
  2480. return !!s->internal;
  2481. }
  2482. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
  2483. {
  2484. int ret;
  2485. char *str;
  2486. ret = av_bprint_finalize(buf, &str);
  2487. if (ret < 0)
  2488. return ret;
  2489. avctx->extradata = str;
  2490. /* Note: the string is NUL terminated (so extradata can be read as a
  2491. * string), but the ending character is not accounted in the size (in
  2492. * binary formats you are likely not supposed to mux that character). When
  2493. * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
  2494. * zeros. */
  2495. avctx->extradata_size = buf->len;
  2496. return 0;
  2497. }