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.

2868 lines
92KB

  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 CONFIG_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. av_frame_set_pkt_pos (frame, s->pkt->pos);
  483. av_frame_set_pkt_duration(frame, s->pkt->duration);
  484. av_frame_set_pkt_size (frame, s->pkt->size);
  485. } else {
  486. frame->pkt_pts = AV_NOPTS_VALUE;
  487. av_frame_set_pkt_pos (frame, -1);
  488. av_frame_set_pkt_duration(frame, 0);
  489. av_frame_set_pkt_size (frame, -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. av_frame_set_channels(frame, 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 = AV_NOPTS_VALUE;
  621. av_frame_set_best_effort_timestamp(frame, AV_NOPTS_VALUE);
  622. av_frame_set_pkt_duration (frame, 0);
  623. av_frame_set_pkt_pos (frame, -1);
  624. av_frame_set_pkt_size (frame, -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. AVDictionary **ff_frame_get_metadatap(AVFrame *frame) {return &frame->metadata;};
  662. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  663. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  664. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  665. {
  666. memset(sub, 0, sizeof(*sub));
  667. sub->pts = AV_NOPTS_VALUE;
  668. }
  669. static int get_bit_rate(AVCodecContext *ctx)
  670. {
  671. int bit_rate;
  672. int bits_per_sample;
  673. switch (ctx->codec_type) {
  674. case AVMEDIA_TYPE_VIDEO:
  675. case AVMEDIA_TYPE_DATA:
  676. case AVMEDIA_TYPE_SUBTITLE:
  677. case AVMEDIA_TYPE_ATTACHMENT:
  678. bit_rate = ctx->bit_rate;
  679. break;
  680. case AVMEDIA_TYPE_AUDIO:
  681. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  682. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  683. break;
  684. default:
  685. bit_rate = 0;
  686. break;
  687. }
  688. return bit_rate;
  689. }
  690. #if FF_API_AVCODEC_OPEN
  691. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  692. {
  693. return avcodec_open2(avctx, codec, NULL);
  694. }
  695. #endif
  696. int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  697. {
  698. int ret = 0;
  699. ff_unlock_avcodec();
  700. ret = avcodec_open2(avctx, codec, options);
  701. ff_lock_avcodec(avctx);
  702. return ret;
  703. }
  704. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  705. {
  706. int ret = 0;
  707. AVDictionary *tmp = NULL;
  708. if (avcodec_is_open(avctx))
  709. return 0;
  710. if ((!codec && !avctx->codec)) {
  711. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  712. return AVERROR(EINVAL);
  713. }
  714. if ((codec && avctx->codec && codec != avctx->codec)) {
  715. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  716. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  717. return AVERROR(EINVAL);
  718. }
  719. if (!codec)
  720. codec = avctx->codec;
  721. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  722. return AVERROR(EINVAL);
  723. if (options)
  724. av_dict_copy(&tmp, *options, 0);
  725. ret = ff_lock_avcodec(avctx);
  726. if (ret < 0)
  727. return ret;
  728. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  729. if (!avctx->internal) {
  730. ret = AVERROR(ENOMEM);
  731. goto end;
  732. }
  733. if (codec->priv_data_size > 0) {
  734. if (!avctx->priv_data) {
  735. avctx->priv_data = av_mallocz(codec->priv_data_size);
  736. if (!avctx->priv_data) {
  737. ret = AVERROR(ENOMEM);
  738. goto end;
  739. }
  740. if (codec->priv_class) {
  741. *(const AVClass **)avctx->priv_data = codec->priv_class;
  742. av_opt_set_defaults(avctx->priv_data);
  743. }
  744. }
  745. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  746. goto free_and_end;
  747. } else {
  748. avctx->priv_data = NULL;
  749. }
  750. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  751. goto free_and_end;
  752. //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
  753. if (!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == AV_CODEC_ID_H264)){
  754. if (avctx->coded_width && avctx->coded_height)
  755. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  756. else if (avctx->width && avctx->height)
  757. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  758. }
  759. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  760. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  761. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  762. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  763. avcodec_set_dimensions(avctx, 0, 0);
  764. }
  765. /* if the decoder init function was already called previously,
  766. * free the already allocated subtitle_header before overwriting it */
  767. if (av_codec_is_decoder(codec))
  768. av_freep(&avctx->subtitle_header);
  769. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  770. ret = AVERROR(EINVAL);
  771. goto free_and_end;
  772. }
  773. avctx->codec = codec;
  774. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  775. avctx->codec_id == AV_CODEC_ID_NONE) {
  776. avctx->codec_type = codec->type;
  777. avctx->codec_id = codec->id;
  778. }
  779. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  780. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  781. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  782. ret = AVERROR(EINVAL);
  783. goto free_and_end;
  784. }
  785. avctx->frame_number = 0;
  786. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  787. if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
  788. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  789. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  790. AVCodec *codec2;
  791. av_log(NULL, AV_LOG_ERROR,
  792. "The %s '%s' is experimental but experimental codecs are not enabled, "
  793. "add '-strict %d' if you want to use it.\n",
  794. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  795. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  796. if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
  797. av_log(NULL, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  798. codec_string, codec2->name);
  799. ret = AVERROR_EXPERIMENTAL;
  800. goto free_and_end;
  801. }
  802. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  803. (!avctx->time_base.num || !avctx->time_base.den)) {
  804. avctx->time_base.num = 1;
  805. avctx->time_base.den = avctx->sample_rate;
  806. }
  807. if (!HAVE_THREADS)
  808. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  809. if (HAVE_THREADS) {
  810. ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  811. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  812. ff_lock_avcodec(avctx);
  813. if (ret < 0)
  814. goto free_and_end;
  815. }
  816. if (HAVE_THREADS && !avctx->thread_opaque
  817. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  818. ret = ff_thread_init(avctx);
  819. if (ret < 0) {
  820. goto free_and_end;
  821. }
  822. }
  823. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  824. avctx->thread_count = 1;
  825. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  826. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  827. avctx->codec->max_lowres);
  828. ret = AVERROR(EINVAL);
  829. goto free_and_end;
  830. }
  831. if (av_codec_is_encoder(avctx->codec)) {
  832. int i;
  833. if (avctx->codec->sample_fmts) {
  834. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  835. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  836. break;
  837. if (avctx->channels == 1 &&
  838. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  839. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  840. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  841. break;
  842. }
  843. }
  844. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  845. char buf[128];
  846. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  847. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  848. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  849. ret = AVERROR(EINVAL);
  850. goto free_and_end;
  851. }
  852. }
  853. if (avctx->codec->pix_fmts) {
  854. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  855. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  856. break;
  857. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  858. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  859. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  860. char buf[128];
  861. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  862. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  863. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  864. ret = AVERROR(EINVAL);
  865. goto free_and_end;
  866. }
  867. }
  868. if (avctx->codec->supported_samplerates) {
  869. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  870. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  871. break;
  872. if (avctx->codec->supported_samplerates[i] == 0) {
  873. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  874. avctx->sample_rate);
  875. ret = AVERROR(EINVAL);
  876. goto free_and_end;
  877. }
  878. }
  879. if (avctx->codec->channel_layouts) {
  880. if (!avctx->channel_layout) {
  881. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  882. } else {
  883. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  884. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  885. break;
  886. if (avctx->codec->channel_layouts[i] == 0) {
  887. char buf[512];
  888. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  889. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  890. ret = AVERROR(EINVAL);
  891. goto free_and_end;
  892. }
  893. }
  894. }
  895. if (avctx->channel_layout && avctx->channels) {
  896. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  897. if (channels != avctx->channels) {
  898. char buf[512];
  899. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  900. av_log(avctx, AV_LOG_ERROR,
  901. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  902. buf, channels, avctx->channels);
  903. ret = AVERROR(EINVAL);
  904. goto free_and_end;
  905. }
  906. } else if (avctx->channel_layout) {
  907. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  908. }
  909. if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
  910. avctx->codec_id != AV_CODEC_ID_PNG // For mplayer
  911. ) {
  912. if (avctx->width <= 0 || avctx->height <= 0) {
  913. av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
  914. ret = AVERROR(EINVAL);
  915. goto free_and_end;
  916. }
  917. }
  918. if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
  919. && avctx->bit_rate>0 && avctx->bit_rate<1000) {
  920. av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extreemly low, did you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
  921. }
  922. if (!avctx->rc_initial_buffer_occupancy)
  923. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  924. }
  925. avctx->pts_correction_num_faulty_pts =
  926. avctx->pts_correction_num_faulty_dts = 0;
  927. avctx->pts_correction_last_pts =
  928. avctx->pts_correction_last_dts = INT64_MIN;
  929. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  930. || avctx->internal->frame_thread_encoder)) {
  931. ret = avctx->codec->init(avctx);
  932. if (ret < 0) {
  933. goto free_and_end;
  934. }
  935. }
  936. ret=0;
  937. if (av_codec_is_decoder(avctx->codec)) {
  938. if (!avctx->bit_rate)
  939. avctx->bit_rate = get_bit_rate(avctx);
  940. /* validate channel layout from the decoder */
  941. if (avctx->channel_layout) {
  942. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  943. if (!avctx->channels)
  944. avctx->channels = channels;
  945. else if (channels != avctx->channels) {
  946. char buf[512];
  947. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  948. av_log(avctx, AV_LOG_WARNING,
  949. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  950. "ignoring specified channel layout\n",
  951. buf, channels, avctx->channels);
  952. avctx->channel_layout = 0;
  953. }
  954. }
  955. if (avctx->channels && avctx->channels < 0 ||
  956. avctx->channels > FF_SANE_NB_CHANNELS) {
  957. ret = AVERROR(EINVAL);
  958. goto free_and_end;
  959. }
  960. if (avctx->sub_charenc) {
  961. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  962. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  963. "supported with subtitles codecs\n");
  964. ret = AVERROR(EINVAL);
  965. goto free_and_end;
  966. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  967. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  968. "subtitles character encoding will be ignored\n",
  969. avctx->codec_descriptor->name);
  970. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  971. } else {
  972. /* input character encoding is set for a text based subtitle
  973. * codec at this point */
  974. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  975. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  976. if (!CONFIG_ICONV && avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  977. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  978. "conversion needs a libavcodec built with iconv support "
  979. "for this codec\n");
  980. ret = AVERROR(ENOSYS);
  981. goto free_and_end;
  982. }
  983. }
  984. }
  985. }
  986. end:
  987. ff_unlock_avcodec();
  988. if (options) {
  989. av_dict_free(options);
  990. *options = tmp;
  991. }
  992. return ret;
  993. free_and_end:
  994. av_dict_free(&tmp);
  995. av_freep(&avctx->priv_data);
  996. av_freep(&avctx->internal);
  997. avctx->codec = NULL;
  998. goto end;
  999. }
  1000. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
  1001. {
  1002. if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  1003. av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
  1004. return AVERROR(EINVAL);
  1005. }
  1006. if (avctx) {
  1007. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  1008. if (!avpkt->data || avpkt->size < size) {
  1009. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  1010. avpkt->data = avctx->internal->byte_buffer;
  1011. avpkt->size = avctx->internal->byte_buffer_size;
  1012. avpkt->destruct = NULL;
  1013. }
  1014. }
  1015. if (avpkt->data) {
  1016. AVBufferRef *buf = avpkt->buf;
  1017. #if FF_API_DESTRUCT_PACKET
  1018. void *destruct = avpkt->destruct;
  1019. #endif
  1020. if (avpkt->size < size) {
  1021. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
  1022. return AVERROR(EINVAL);
  1023. }
  1024. av_init_packet(avpkt);
  1025. #if FF_API_DESTRUCT_PACKET
  1026. avpkt->destruct = destruct;
  1027. #endif
  1028. avpkt->buf = buf;
  1029. avpkt->size = size;
  1030. return 0;
  1031. } else {
  1032. int ret = av_new_packet(avpkt, size);
  1033. if (ret < 0)
  1034. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
  1035. return ret;
  1036. }
  1037. }
  1038. int ff_alloc_packet(AVPacket *avpkt, int size)
  1039. {
  1040. return ff_alloc_packet2(NULL, avpkt, size);
  1041. }
  1042. /**
  1043. * Pad last frame with silence.
  1044. */
  1045. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1046. {
  1047. AVFrame *frame = NULL;
  1048. uint8_t *buf = NULL;
  1049. int ret;
  1050. if (!(frame = avcodec_alloc_frame()))
  1051. return AVERROR(ENOMEM);
  1052. *frame = *src;
  1053. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  1054. s->frame_size, s->sample_fmt, 0)) < 0)
  1055. goto fail;
  1056. if (!(buf = av_malloc(ret))) {
  1057. ret = AVERROR(ENOMEM);
  1058. goto fail;
  1059. }
  1060. frame->nb_samples = s->frame_size;
  1061. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  1062. buf, ret, 0)) < 0)
  1063. goto fail;
  1064. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1065. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1066. goto fail;
  1067. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1068. frame->nb_samples - src->nb_samples,
  1069. s->channels, s->sample_fmt)) < 0)
  1070. goto fail;
  1071. *dst = frame;
  1072. return 0;
  1073. fail:
  1074. if (frame->extended_data != frame->data)
  1075. av_freep(&frame->extended_data);
  1076. av_freep(&buf);
  1077. av_freep(&frame);
  1078. return ret;
  1079. }
  1080. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1081. AVPacket *avpkt,
  1082. const AVFrame *frame,
  1083. int *got_packet_ptr)
  1084. {
  1085. AVFrame tmp;
  1086. AVFrame *padded_frame = NULL;
  1087. int ret;
  1088. AVPacket user_pkt = *avpkt;
  1089. int needs_realloc = !user_pkt.data;
  1090. *got_packet_ptr = 0;
  1091. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1092. av_free_packet(avpkt);
  1093. av_init_packet(avpkt);
  1094. return 0;
  1095. }
  1096. /* ensure that extended_data is properly set */
  1097. if (frame && !frame->extended_data) {
  1098. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1099. avctx->channels > AV_NUM_DATA_POINTERS) {
  1100. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1101. "with more than %d channels, but extended_data is not set.\n",
  1102. AV_NUM_DATA_POINTERS);
  1103. return AVERROR(EINVAL);
  1104. }
  1105. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1106. tmp = *frame;
  1107. tmp.extended_data = tmp.data;
  1108. frame = &tmp;
  1109. }
  1110. /* check for valid frame size */
  1111. if (frame) {
  1112. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1113. if (frame->nb_samples > avctx->frame_size) {
  1114. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1115. return AVERROR(EINVAL);
  1116. }
  1117. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1118. if (frame->nb_samples < avctx->frame_size &&
  1119. !avctx->internal->last_audio_frame) {
  1120. ret = pad_last_frame(avctx, &padded_frame, frame);
  1121. if (ret < 0)
  1122. return ret;
  1123. frame = padded_frame;
  1124. avctx->internal->last_audio_frame = 1;
  1125. }
  1126. if (frame->nb_samples != avctx->frame_size) {
  1127. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1128. ret = AVERROR(EINVAL);
  1129. goto end;
  1130. }
  1131. }
  1132. }
  1133. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1134. if (!ret) {
  1135. if (*got_packet_ptr) {
  1136. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1137. if (avpkt->pts == AV_NOPTS_VALUE)
  1138. avpkt->pts = frame->pts;
  1139. if (!avpkt->duration)
  1140. avpkt->duration = ff_samples_to_time_base(avctx,
  1141. frame->nb_samples);
  1142. }
  1143. avpkt->dts = avpkt->pts;
  1144. } else {
  1145. avpkt->size = 0;
  1146. }
  1147. }
  1148. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1149. needs_realloc = 0;
  1150. if (user_pkt.data) {
  1151. if (user_pkt.size >= avpkt->size) {
  1152. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1153. } else {
  1154. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1155. avpkt->size = user_pkt.size;
  1156. ret = -1;
  1157. }
  1158. avpkt->buf = user_pkt.buf;
  1159. avpkt->data = user_pkt.data;
  1160. avpkt->destruct = user_pkt.destruct;
  1161. } else {
  1162. if (av_dup_packet(avpkt) < 0) {
  1163. ret = AVERROR(ENOMEM);
  1164. }
  1165. }
  1166. }
  1167. if (!ret) {
  1168. if (needs_realloc && avpkt->data) {
  1169. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1170. if (ret >= 0)
  1171. avpkt->data = avpkt->buf->data;
  1172. }
  1173. avctx->frame_number++;
  1174. }
  1175. if (ret < 0 || !*got_packet_ptr) {
  1176. av_free_packet(avpkt);
  1177. av_init_packet(avpkt);
  1178. goto end;
  1179. }
  1180. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1181. * this needs to be moved to the encoders, but for now we can do it
  1182. * here to simplify things */
  1183. avpkt->flags |= AV_PKT_FLAG_KEY;
  1184. end:
  1185. if (padded_frame) {
  1186. av_freep(&padded_frame->data[0]);
  1187. if (padded_frame->extended_data != padded_frame->data)
  1188. av_freep(&padded_frame->extended_data);
  1189. av_freep(&padded_frame);
  1190. }
  1191. return ret;
  1192. }
  1193. #if FF_API_OLD_ENCODE_AUDIO
  1194. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1195. uint8_t *buf, int buf_size,
  1196. const short *samples)
  1197. {
  1198. AVPacket pkt;
  1199. AVFrame frame0 = { { 0 } };
  1200. AVFrame *frame;
  1201. int ret, samples_size, got_packet;
  1202. av_init_packet(&pkt);
  1203. pkt.data = buf;
  1204. pkt.size = buf_size;
  1205. if (samples) {
  1206. frame = &frame0;
  1207. avcodec_get_frame_defaults(frame);
  1208. if (avctx->frame_size) {
  1209. frame->nb_samples = avctx->frame_size;
  1210. } else {
  1211. /* if frame_size is not set, the number of samples must be
  1212. * calculated from the buffer size */
  1213. int64_t nb_samples;
  1214. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1215. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1216. "support this codec\n");
  1217. return AVERROR(EINVAL);
  1218. }
  1219. nb_samples = (int64_t)buf_size * 8 /
  1220. (av_get_bits_per_sample(avctx->codec_id) *
  1221. avctx->channels);
  1222. if (nb_samples >= INT_MAX)
  1223. return AVERROR(EINVAL);
  1224. frame->nb_samples = nb_samples;
  1225. }
  1226. /* it is assumed that the samples buffer is large enough based on the
  1227. * relevant parameters */
  1228. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1229. frame->nb_samples,
  1230. avctx->sample_fmt, 1);
  1231. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1232. avctx->sample_fmt,
  1233. (const uint8_t *)samples,
  1234. samples_size, 1)) < 0)
  1235. return ret;
  1236. /* fabricate frame pts from sample count.
  1237. * this is needed because the avcodec_encode_audio() API does not have
  1238. * a way for the user to provide pts */
  1239. if (avctx->sample_rate && avctx->time_base.num)
  1240. frame->pts = ff_samples_to_time_base(avctx,
  1241. avctx->internal->sample_count);
  1242. else
  1243. frame->pts = AV_NOPTS_VALUE;
  1244. avctx->internal->sample_count += frame->nb_samples;
  1245. } else {
  1246. frame = NULL;
  1247. }
  1248. got_packet = 0;
  1249. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1250. if (!ret && got_packet && avctx->coded_frame) {
  1251. avctx->coded_frame->pts = pkt.pts;
  1252. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1253. }
  1254. /* free any side data since we cannot return it */
  1255. ff_packet_free_side_data(&pkt);
  1256. if (frame && frame->extended_data != frame->data)
  1257. av_freep(&frame->extended_data);
  1258. return ret ? ret : pkt.size;
  1259. }
  1260. #endif
  1261. #if FF_API_OLD_ENCODE_VIDEO
  1262. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1263. const AVFrame *pict)
  1264. {
  1265. AVPacket pkt;
  1266. int ret, got_packet = 0;
  1267. if (buf_size < FF_MIN_BUFFER_SIZE) {
  1268. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1269. return -1;
  1270. }
  1271. av_init_packet(&pkt);
  1272. pkt.data = buf;
  1273. pkt.size = buf_size;
  1274. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1275. if (!ret && got_packet && avctx->coded_frame) {
  1276. avctx->coded_frame->pts = pkt.pts;
  1277. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1278. }
  1279. /* free any side data since we cannot return it */
  1280. if (pkt.side_data_elems > 0) {
  1281. int i;
  1282. for (i = 0; i < pkt.side_data_elems; i++)
  1283. av_free(pkt.side_data[i].data);
  1284. av_freep(&pkt.side_data);
  1285. pkt.side_data_elems = 0;
  1286. }
  1287. return ret ? ret : pkt.size;
  1288. }
  1289. #endif
  1290. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1291. AVPacket *avpkt,
  1292. const AVFrame *frame,
  1293. int *got_packet_ptr)
  1294. {
  1295. int ret;
  1296. AVPacket user_pkt = *avpkt;
  1297. int needs_realloc = !user_pkt.data;
  1298. *got_packet_ptr = 0;
  1299. if(HAVE_THREADS && avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1300. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1301. if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
  1302. avctx->stats_out[0] = '\0';
  1303. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1304. av_free_packet(avpkt);
  1305. av_init_packet(avpkt);
  1306. avpkt->size = 0;
  1307. return 0;
  1308. }
  1309. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1310. return AVERROR(EINVAL);
  1311. av_assert0(avctx->codec->encode2);
  1312. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1313. av_assert0(ret <= 0);
  1314. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1315. needs_realloc = 0;
  1316. if (user_pkt.data) {
  1317. if (user_pkt.size >= avpkt->size) {
  1318. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1319. } else {
  1320. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1321. avpkt->size = user_pkt.size;
  1322. ret = -1;
  1323. }
  1324. avpkt->buf = user_pkt.buf;
  1325. avpkt->data = user_pkt.data;
  1326. avpkt->destruct = user_pkt.destruct;
  1327. } else {
  1328. if (av_dup_packet(avpkt) < 0) {
  1329. ret = AVERROR(ENOMEM);
  1330. }
  1331. }
  1332. }
  1333. if (!ret) {
  1334. if (!*got_packet_ptr)
  1335. avpkt->size = 0;
  1336. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1337. avpkt->pts = avpkt->dts = frame->pts;
  1338. if (needs_realloc && avpkt->data) {
  1339. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1340. if (ret >= 0)
  1341. avpkt->data = avpkt->buf->data;
  1342. }
  1343. avctx->frame_number++;
  1344. }
  1345. if (ret < 0 || !*got_packet_ptr)
  1346. av_free_packet(avpkt);
  1347. emms_c();
  1348. return ret;
  1349. }
  1350. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1351. const AVSubtitle *sub)
  1352. {
  1353. int ret;
  1354. if (sub->start_display_time) {
  1355. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1356. return -1;
  1357. }
  1358. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1359. avctx->frame_number++;
  1360. return ret;
  1361. }
  1362. /**
  1363. * Attempt to guess proper monotonic timestamps for decoded video frames
  1364. * which might have incorrect times. Input timestamps may wrap around, in
  1365. * which case the output will as well.
  1366. *
  1367. * @param pts the pts field of the decoded AVPacket, as passed through
  1368. * AVFrame.pkt_pts
  1369. * @param dts the dts field of the decoded AVPacket
  1370. * @return one of the input values, may be AV_NOPTS_VALUE
  1371. */
  1372. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1373. int64_t reordered_pts, int64_t dts)
  1374. {
  1375. int64_t pts = AV_NOPTS_VALUE;
  1376. if (dts != AV_NOPTS_VALUE) {
  1377. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1378. ctx->pts_correction_last_dts = dts;
  1379. }
  1380. if (reordered_pts != AV_NOPTS_VALUE) {
  1381. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1382. ctx->pts_correction_last_pts = reordered_pts;
  1383. }
  1384. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1385. && reordered_pts != AV_NOPTS_VALUE)
  1386. pts = reordered_pts;
  1387. else
  1388. pts = dts;
  1389. return pts;
  1390. }
  1391. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1392. {
  1393. int size = 0;
  1394. const uint8_t *data;
  1395. uint32_t flags;
  1396. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1397. return;
  1398. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1399. if (!data || size < 4)
  1400. return;
  1401. flags = bytestream_get_le32(&data);
  1402. size -= 4;
  1403. if (size < 4) /* Required for any of the changes */
  1404. return;
  1405. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1406. avctx->channels = bytestream_get_le32(&data);
  1407. size -= 4;
  1408. }
  1409. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1410. if (size < 8)
  1411. return;
  1412. avctx->channel_layout = bytestream_get_le64(&data);
  1413. size -= 8;
  1414. }
  1415. if (size < 4)
  1416. return;
  1417. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1418. avctx->sample_rate = bytestream_get_le32(&data);
  1419. size -= 4;
  1420. }
  1421. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1422. if (size < 8)
  1423. return;
  1424. avctx->width = bytestream_get_le32(&data);
  1425. avctx->height = bytestream_get_le32(&data);
  1426. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1427. size -= 8;
  1428. }
  1429. }
  1430. static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
  1431. {
  1432. int size, ret = 0;
  1433. const uint8_t *side_metadata;
  1434. const uint8_t *end;
  1435. av_dict_free(&avctx->metadata);
  1436. side_metadata = av_packet_get_side_data(avctx->pkt,
  1437. AV_PKT_DATA_STRINGS_METADATA, &size);
  1438. if (!side_metadata)
  1439. goto end;
  1440. end = side_metadata + size;
  1441. while (side_metadata < end) {
  1442. const uint8_t *key = side_metadata;
  1443. const uint8_t *val = side_metadata + strlen(key) + 1;
  1444. int ret = av_dict_set(ff_frame_get_metadatap(frame), key, val, 0);
  1445. if (ret < 0)
  1446. break;
  1447. side_metadata = val + strlen(val) + 1;
  1448. }
  1449. end:
  1450. avctx->metadata = av_frame_get_metadata(frame);
  1451. return ret;
  1452. }
  1453. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1454. int *got_picture_ptr,
  1455. const AVPacket *avpkt)
  1456. {
  1457. int ret;
  1458. // copy to ensure we do not change avpkt
  1459. AVPacket tmp = *avpkt;
  1460. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1461. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1462. return AVERROR(EINVAL);
  1463. }
  1464. *got_picture_ptr = 0;
  1465. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1466. return AVERROR(EINVAL);
  1467. avcodec_get_frame_defaults(picture);
  1468. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1469. int did_split = av_packet_split_side_data(&tmp);
  1470. apply_param_change(avctx, &tmp);
  1471. avctx->pkt = &tmp;
  1472. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1473. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1474. &tmp);
  1475. else {
  1476. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1477. &tmp);
  1478. picture->pkt_dts = avpkt->dts;
  1479. if(!avctx->has_b_frames){
  1480. av_frame_set_pkt_pos(picture, avpkt->pos);
  1481. }
  1482. //FIXME these should be under if(!avctx->has_b_frames)
  1483. /* get_buffer is supposed to set frame parameters */
  1484. if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
  1485. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1486. if (!picture->width) picture->width = avctx->width;
  1487. if (!picture->height) picture->height = avctx->height;
  1488. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  1489. }
  1490. }
  1491. add_metadata_from_side_data(avctx, picture);
  1492. emms_c(); //needed to avoid an emms_c() call before every return;
  1493. avctx->pkt = NULL;
  1494. if (did_split) {
  1495. ff_packet_free_side_data(&tmp);
  1496. if(ret == tmp.size)
  1497. ret = avpkt->size;
  1498. }
  1499. if (*got_picture_ptr){
  1500. avctx->frame_number++;
  1501. av_frame_set_best_effort_timestamp(picture,
  1502. guess_correct_pts(avctx,
  1503. picture->pkt_pts,
  1504. picture->pkt_dts));
  1505. }
  1506. } else
  1507. ret = 0;
  1508. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1509. * make sure it's set correctly */
  1510. picture->extended_data = picture->data;
  1511. return ret;
  1512. }
  1513. #if FF_API_OLD_DECODE_AUDIO
  1514. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1515. int *frame_size_ptr,
  1516. AVPacket *avpkt)
  1517. {
  1518. AVFrame frame = { { 0 } };
  1519. int ret, got_frame = 0;
  1520. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1521. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1522. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1523. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1524. "avcodec_decode_audio4()\n");
  1525. avctx->get_buffer = avcodec_default_get_buffer;
  1526. avctx->release_buffer = avcodec_default_release_buffer;
  1527. }
  1528. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1529. if (ret >= 0 && got_frame) {
  1530. int ch, plane_size;
  1531. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1532. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1533. frame.nb_samples,
  1534. avctx->sample_fmt, 1);
  1535. if (*frame_size_ptr < data_size) {
  1536. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1537. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1538. return AVERROR(EINVAL);
  1539. }
  1540. memcpy(samples, frame.extended_data[0], plane_size);
  1541. if (planar && avctx->channels > 1) {
  1542. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1543. for (ch = 1; ch < avctx->channels; ch++) {
  1544. memcpy(out, frame.extended_data[ch], plane_size);
  1545. out += plane_size;
  1546. }
  1547. }
  1548. *frame_size_ptr = data_size;
  1549. } else {
  1550. *frame_size_ptr = 0;
  1551. }
  1552. return ret;
  1553. }
  1554. #endif
  1555. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1556. AVFrame *frame,
  1557. int *got_frame_ptr,
  1558. const AVPacket *avpkt)
  1559. {
  1560. int planar, channels;
  1561. int ret = 0;
  1562. *got_frame_ptr = 0;
  1563. if (!avpkt->data && avpkt->size) {
  1564. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1565. return AVERROR(EINVAL);
  1566. }
  1567. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1568. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1569. return AVERROR(EINVAL);
  1570. }
  1571. avcodec_get_frame_defaults(frame);
  1572. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1573. uint8_t *side;
  1574. int side_size;
  1575. // copy to ensure we do not change avpkt
  1576. AVPacket tmp = *avpkt;
  1577. int did_split = av_packet_split_side_data(&tmp);
  1578. apply_param_change(avctx, &tmp);
  1579. avctx->pkt = &tmp;
  1580. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1581. if (ret >= 0 && *got_frame_ptr) {
  1582. avctx->frame_number++;
  1583. frame->pkt_dts = avpkt->dts;
  1584. av_frame_set_best_effort_timestamp(frame,
  1585. guess_correct_pts(avctx,
  1586. frame->pkt_pts,
  1587. frame->pkt_dts));
  1588. if (frame->format == AV_SAMPLE_FMT_NONE)
  1589. frame->format = avctx->sample_fmt;
  1590. if (!frame->channel_layout)
  1591. frame->channel_layout = avctx->channel_layout;
  1592. if (!av_frame_get_channels(frame))
  1593. av_frame_set_channels(frame, avctx->channels);
  1594. if (!frame->sample_rate)
  1595. frame->sample_rate = avctx->sample_rate;
  1596. }
  1597. add_metadata_from_side_data(avctx, frame);
  1598. side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  1599. if(side && side_size>=10) {
  1600. avctx->internal->skip_samples = AV_RL32(side);
  1601. av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
  1602. avctx->internal->skip_samples);
  1603. }
  1604. if (avctx->internal->skip_samples && *got_frame_ptr) {
  1605. if(frame->nb_samples <= avctx->internal->skip_samples){
  1606. *got_frame_ptr = 0;
  1607. avctx->internal->skip_samples -= frame->nb_samples;
  1608. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  1609. avctx->internal->skip_samples);
  1610. } else {
  1611. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  1612. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  1613. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1614. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  1615. (AVRational){1, avctx->sample_rate},
  1616. avctx->pkt_timebase);
  1617. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  1618. frame->pkt_pts += diff_ts;
  1619. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  1620. frame->pkt_dts += diff_ts;
  1621. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  1622. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  1623. } else {
  1624. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  1625. }
  1626. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  1627. avctx->internal->skip_samples, frame->nb_samples);
  1628. frame->nb_samples -= avctx->internal->skip_samples;
  1629. avctx->internal->skip_samples = 0;
  1630. }
  1631. }
  1632. avctx->pkt = NULL;
  1633. if (did_split) {
  1634. ff_packet_free_side_data(&tmp);
  1635. if(ret == tmp.size)
  1636. ret = avpkt->size;
  1637. }
  1638. }
  1639. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1640. * make sure it's set correctly; assume decoders that actually use
  1641. * extended_data are doing it correctly */
  1642. if (*got_frame_ptr) {
  1643. planar = av_sample_fmt_is_planar(frame->format);
  1644. channels = av_frame_get_channels(frame);
  1645. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1646. frame->extended_data = frame->data;
  1647. } else {
  1648. frame->extended_data = NULL;
  1649. }
  1650. return ret;
  1651. }
  1652. #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
  1653. static int recode_subtitle(AVCodecContext *avctx,
  1654. AVPacket *outpkt, const AVPacket *inpkt)
  1655. {
  1656. #if CONFIG_ICONV
  1657. iconv_t cd = (iconv_t)-1;
  1658. int ret = 0;
  1659. char *inb, *outb;
  1660. size_t inl, outl;
  1661. AVPacket tmp;
  1662. #endif
  1663. if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER)
  1664. return 0;
  1665. #if CONFIG_ICONV
  1666. cd = iconv_open("UTF-8", avctx->sub_charenc);
  1667. if (cd == (iconv_t)-1) {
  1668. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  1669. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  1670. ret = AVERROR(errno);
  1671. goto end;
  1672. }
  1673. inb = inpkt->data;
  1674. inl = inpkt->size;
  1675. if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
  1676. av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
  1677. ret = AVERROR(ENOMEM);
  1678. goto end;
  1679. }
  1680. ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
  1681. if (ret < 0)
  1682. goto end;
  1683. outpkt->data = tmp.data;
  1684. outpkt->size = tmp.size;
  1685. outb = outpkt->data;
  1686. outl = outpkt->size;
  1687. if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
  1688. iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
  1689. outl >= outpkt->size || inl != 0) {
  1690. av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
  1691. "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
  1692. av_free_packet(&tmp);
  1693. ret = AVERROR(errno);
  1694. goto end;
  1695. }
  1696. outpkt->size -= outl;
  1697. outpkt->data[outpkt->size - 1] = '\0';
  1698. end:
  1699. if (cd != (iconv_t)-1)
  1700. iconv_close(cd);
  1701. return ret;
  1702. #else
  1703. av_assert0(!"requesting subtitles recoding without iconv");
  1704. #endif
  1705. }
  1706. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1707. int *got_sub_ptr,
  1708. AVPacket *avpkt)
  1709. {
  1710. int ret = 0;
  1711. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  1712. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  1713. return AVERROR(EINVAL);
  1714. }
  1715. *got_sub_ptr = 0;
  1716. avcodec_get_subtitle_defaults(sub);
  1717. if (avpkt->size) {
  1718. AVPacket pkt_recoded;
  1719. AVPacket tmp = *avpkt;
  1720. int did_split = av_packet_split_side_data(&tmp);
  1721. //apply_param_change(avctx, &tmp);
  1722. pkt_recoded = tmp;
  1723. ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
  1724. if (ret < 0) {
  1725. *got_sub_ptr = 0;
  1726. } else {
  1727. avctx->pkt = &pkt_recoded;
  1728. if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  1729. sub->pts = av_rescale_q(avpkt->pts,
  1730. avctx->pkt_timebase, AV_TIME_BASE_Q);
  1731. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
  1732. av_assert1((ret >= 0) >= !!*got_sub_ptr &&
  1733. !!*got_sub_ptr >= !!sub->num_rects);
  1734. if (tmp.data != pkt_recoded.data)
  1735. av_free(pkt_recoded.data);
  1736. sub->format = !(avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB);
  1737. avctx->pkt = NULL;
  1738. }
  1739. if (did_split) {
  1740. ff_packet_free_side_data(&tmp);
  1741. if(ret == tmp.size)
  1742. ret = avpkt->size;
  1743. }
  1744. if (*got_sub_ptr)
  1745. avctx->frame_number++;
  1746. }
  1747. return ret;
  1748. }
  1749. void avsubtitle_free(AVSubtitle *sub)
  1750. {
  1751. int i;
  1752. for (i = 0; i < sub->num_rects; i++) {
  1753. av_freep(&sub->rects[i]->pict.data[0]);
  1754. av_freep(&sub->rects[i]->pict.data[1]);
  1755. av_freep(&sub->rects[i]->pict.data[2]);
  1756. av_freep(&sub->rects[i]->pict.data[3]);
  1757. av_freep(&sub->rects[i]->text);
  1758. av_freep(&sub->rects[i]->ass);
  1759. av_freep(&sub->rects[i]);
  1760. }
  1761. av_freep(&sub->rects);
  1762. memset(sub, 0, sizeof(AVSubtitle));
  1763. }
  1764. av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
  1765. {
  1766. int ret = 0;
  1767. ff_unlock_avcodec();
  1768. ret = avcodec_close(avctx);
  1769. ff_lock_avcodec(NULL);
  1770. return ret;
  1771. }
  1772. av_cold int avcodec_close(AVCodecContext *avctx)
  1773. {
  1774. int ret = ff_lock_avcodec(avctx);
  1775. if (ret < 0)
  1776. return ret;
  1777. if (avcodec_is_open(avctx)) {
  1778. if (HAVE_THREADS && avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  1779. ff_unlock_avcodec();
  1780. ff_frame_thread_encoder_free(avctx);
  1781. ff_lock_avcodec(avctx);
  1782. }
  1783. if (HAVE_THREADS && avctx->thread_opaque)
  1784. ff_thread_free(avctx);
  1785. if (avctx->codec && avctx->codec->close)
  1786. avctx->codec->close(avctx);
  1787. avcodec_default_free_buffers(avctx);
  1788. avctx->coded_frame = NULL;
  1789. avctx->internal->byte_buffer_size = 0;
  1790. av_freep(&avctx->internal->byte_buffer);
  1791. av_freep(&avctx->internal);
  1792. av_dict_free(&avctx->metadata);
  1793. }
  1794. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1795. av_opt_free(avctx->priv_data);
  1796. av_opt_free(avctx);
  1797. av_freep(&avctx->priv_data);
  1798. if (av_codec_is_encoder(avctx->codec))
  1799. av_freep(&avctx->extradata);
  1800. avctx->codec = NULL;
  1801. avctx->active_thread_type = 0;
  1802. ff_unlock_avcodec();
  1803. return 0;
  1804. }
  1805. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  1806. {
  1807. switch(id){
  1808. //This is for future deprecatec codec ids, its empty since
  1809. //last major bump but will fill up again over time, please don't remove it
  1810. // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
  1811. case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
  1812. case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
  1813. default : return id;
  1814. }
  1815. }
  1816. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  1817. {
  1818. AVCodec *p, *experimental = NULL;
  1819. p = first_avcodec;
  1820. id= remap_deprecated_codec_id(id);
  1821. while (p) {
  1822. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  1823. p->id == id) {
  1824. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1825. experimental = p;
  1826. } else
  1827. return p;
  1828. }
  1829. p = p->next;
  1830. }
  1831. return experimental;
  1832. }
  1833. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1834. {
  1835. return find_encdec(id, 1);
  1836. }
  1837. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1838. {
  1839. AVCodec *p;
  1840. if (!name)
  1841. return NULL;
  1842. p = first_avcodec;
  1843. while (p) {
  1844. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1845. return p;
  1846. p = p->next;
  1847. }
  1848. return NULL;
  1849. }
  1850. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1851. {
  1852. return find_encdec(id, 0);
  1853. }
  1854. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1855. {
  1856. AVCodec *p;
  1857. if (!name)
  1858. return NULL;
  1859. p = first_avcodec;
  1860. while (p) {
  1861. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1862. return p;
  1863. p = p->next;
  1864. }
  1865. return NULL;
  1866. }
  1867. const char *avcodec_get_name(enum AVCodecID id)
  1868. {
  1869. const AVCodecDescriptor *cd;
  1870. AVCodec *codec;
  1871. if (id == AV_CODEC_ID_NONE)
  1872. return "none";
  1873. cd = avcodec_descriptor_get(id);
  1874. if (cd)
  1875. return cd->name;
  1876. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1877. codec = avcodec_find_decoder(id);
  1878. if (codec)
  1879. return codec->name;
  1880. codec = avcodec_find_encoder(id);
  1881. if (codec)
  1882. return codec->name;
  1883. return "unknown_codec";
  1884. }
  1885. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1886. {
  1887. int i, len, ret = 0;
  1888. #define TAG_PRINT(x) \
  1889. (((x) >= '0' && (x) <= '9') || \
  1890. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1891. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  1892. for (i = 0; i < 4; i++) {
  1893. len = snprintf(buf, buf_size,
  1894. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  1895. buf += len;
  1896. buf_size = buf_size > len ? buf_size - len : 0;
  1897. ret += len;
  1898. codec_tag >>= 8;
  1899. }
  1900. return ret;
  1901. }
  1902. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1903. {
  1904. const char *codec_type;
  1905. const char *codec_name;
  1906. const char *profile = NULL;
  1907. const AVCodec *p;
  1908. int bitrate;
  1909. AVRational display_aspect_ratio;
  1910. if (!buf || buf_size <= 0)
  1911. return;
  1912. codec_type = av_get_media_type_string(enc->codec_type);
  1913. codec_name = avcodec_get_name(enc->codec_id);
  1914. if (enc->profile != FF_PROFILE_UNKNOWN) {
  1915. if (enc->codec)
  1916. p = enc->codec;
  1917. else
  1918. p = encode ? avcodec_find_encoder(enc->codec_id) :
  1919. avcodec_find_decoder(enc->codec_id);
  1920. if (p)
  1921. profile = av_get_profile_name(p, enc->profile);
  1922. }
  1923. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  1924. codec_name, enc->mb_decision ? " (hq)" : "");
  1925. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1926. if (profile)
  1927. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1928. if (enc->codec_tag) {
  1929. char tag_buf[32];
  1930. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1931. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1932. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  1933. }
  1934. switch (enc->codec_type) {
  1935. case AVMEDIA_TYPE_VIDEO:
  1936. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  1937. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1938. ", %s",
  1939. av_get_pix_fmt_name(enc->pix_fmt));
  1940. if (enc->bits_per_raw_sample &&
  1941. enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
  1942. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1943. " (%d bpc)", enc->bits_per_raw_sample);
  1944. }
  1945. if (enc->width) {
  1946. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1947. ", %dx%d",
  1948. enc->width, enc->height);
  1949. if (enc->sample_aspect_ratio.num) {
  1950. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1951. enc->width * enc->sample_aspect_ratio.num,
  1952. enc->height * enc->sample_aspect_ratio.den,
  1953. 1024 * 1024);
  1954. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1955. " [SAR %d:%d DAR %d:%d]",
  1956. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1957. display_aspect_ratio.num, display_aspect_ratio.den);
  1958. }
  1959. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1960. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1961. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1962. ", %d/%d",
  1963. enc->time_base.num / g, enc->time_base.den / g);
  1964. }
  1965. }
  1966. if (encode) {
  1967. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1968. ", q=%d-%d", enc->qmin, enc->qmax);
  1969. }
  1970. break;
  1971. case AVMEDIA_TYPE_AUDIO:
  1972. if (enc->sample_rate) {
  1973. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1974. ", %d Hz", enc->sample_rate);
  1975. }
  1976. av_strlcat(buf, ", ", buf_size);
  1977. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1978. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1979. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1980. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1981. }
  1982. break;
  1983. case AVMEDIA_TYPE_DATA:
  1984. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1985. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1986. if (g)
  1987. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1988. ", %d/%d",
  1989. enc->time_base.num / g, enc->time_base.den / g);
  1990. }
  1991. break;
  1992. default:
  1993. return;
  1994. }
  1995. if (encode) {
  1996. if (enc->flags & CODEC_FLAG_PASS1)
  1997. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1998. ", pass 1");
  1999. if (enc->flags & CODEC_FLAG_PASS2)
  2000. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2001. ", pass 2");
  2002. }
  2003. bitrate = get_bit_rate(enc);
  2004. if (bitrate != 0) {
  2005. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2006. ", %d kb/s", bitrate / 1000);
  2007. }
  2008. }
  2009. const char *av_get_profile_name(const AVCodec *codec, int profile)
  2010. {
  2011. const AVProfile *p;
  2012. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  2013. return NULL;
  2014. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  2015. if (p->profile == profile)
  2016. return p->name;
  2017. return NULL;
  2018. }
  2019. unsigned avcodec_version(void)
  2020. {
  2021. // av_assert0(AV_CODEC_ID_V410==164);
  2022. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  2023. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  2024. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  2025. av_assert0(AV_CODEC_ID_SRT==94216);
  2026. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  2027. return LIBAVCODEC_VERSION_INT;
  2028. }
  2029. const char *avcodec_configuration(void)
  2030. {
  2031. return FFMPEG_CONFIGURATION;
  2032. }
  2033. const char *avcodec_license(void)
  2034. {
  2035. #define LICENSE_PREFIX "libavcodec license: "
  2036. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  2037. }
  2038. void avcodec_flush_buffers(AVCodecContext *avctx)
  2039. {
  2040. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2041. ff_thread_flush(avctx);
  2042. else if (avctx->codec->flush)
  2043. avctx->codec->flush(avctx);
  2044. avctx->pts_correction_last_pts =
  2045. avctx->pts_correction_last_dts = INT64_MIN;
  2046. }
  2047. static void video_free_buffers(AVCodecContext *s)
  2048. {
  2049. AVCodecInternal *avci = s->internal;
  2050. int i, j;
  2051. if (!avci->buffer)
  2052. return;
  2053. if (avci->buffer_count)
  2054. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  2055. avci->buffer_count);
  2056. for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
  2057. InternalBuffer *buf = &avci->buffer[i];
  2058. for (j = 0; j < 4; j++) {
  2059. av_freep(&buf->base[j]);
  2060. buf->data[j] = NULL;
  2061. }
  2062. }
  2063. av_freep(&avci->buffer);
  2064. avci->buffer_count = 0;
  2065. }
  2066. static void audio_free_buffers(AVCodecContext *avctx)
  2067. {
  2068. AVCodecInternal *avci = avctx->internal;
  2069. av_freep(&avci->audio_data);
  2070. }
  2071. void avcodec_default_free_buffers(AVCodecContext *avctx)
  2072. {
  2073. switch (avctx->codec_type) {
  2074. case AVMEDIA_TYPE_VIDEO:
  2075. video_free_buffers(avctx);
  2076. break;
  2077. case AVMEDIA_TYPE_AUDIO:
  2078. audio_free_buffers(avctx);
  2079. break;
  2080. default:
  2081. break;
  2082. }
  2083. }
  2084. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  2085. {
  2086. switch (codec_id) {
  2087. case AV_CODEC_ID_8SVX_EXP:
  2088. case AV_CODEC_ID_8SVX_FIB:
  2089. case AV_CODEC_ID_ADPCM_CT:
  2090. case AV_CODEC_ID_ADPCM_IMA_APC:
  2091. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  2092. case AV_CODEC_ID_ADPCM_IMA_OKI:
  2093. case AV_CODEC_ID_ADPCM_IMA_WS:
  2094. case AV_CODEC_ID_ADPCM_G722:
  2095. case AV_CODEC_ID_ADPCM_YAMAHA:
  2096. return 4;
  2097. case AV_CODEC_ID_PCM_ALAW:
  2098. case AV_CODEC_ID_PCM_MULAW:
  2099. case AV_CODEC_ID_PCM_S8:
  2100. case AV_CODEC_ID_PCM_S8_PLANAR:
  2101. case AV_CODEC_ID_PCM_U8:
  2102. case AV_CODEC_ID_PCM_ZORK:
  2103. return 8;
  2104. case AV_CODEC_ID_PCM_S16BE:
  2105. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  2106. case AV_CODEC_ID_PCM_S16LE:
  2107. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  2108. case AV_CODEC_ID_PCM_U16BE:
  2109. case AV_CODEC_ID_PCM_U16LE:
  2110. return 16;
  2111. case AV_CODEC_ID_PCM_S24DAUD:
  2112. case AV_CODEC_ID_PCM_S24BE:
  2113. case AV_CODEC_ID_PCM_S24LE:
  2114. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  2115. case AV_CODEC_ID_PCM_U24BE:
  2116. case AV_CODEC_ID_PCM_U24LE:
  2117. return 24;
  2118. case AV_CODEC_ID_PCM_S32BE:
  2119. case AV_CODEC_ID_PCM_S32LE:
  2120. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  2121. case AV_CODEC_ID_PCM_U32BE:
  2122. case AV_CODEC_ID_PCM_U32LE:
  2123. case AV_CODEC_ID_PCM_F32BE:
  2124. case AV_CODEC_ID_PCM_F32LE:
  2125. return 32;
  2126. case AV_CODEC_ID_PCM_F64BE:
  2127. case AV_CODEC_ID_PCM_F64LE:
  2128. return 64;
  2129. default:
  2130. return 0;
  2131. }
  2132. }
  2133. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  2134. {
  2135. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  2136. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2137. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2138. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2139. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2140. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2141. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2142. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2143. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2144. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2145. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2146. };
  2147. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  2148. return AV_CODEC_ID_NONE;
  2149. if (be < 0 || be > 1)
  2150. be = AV_NE(1, 0);
  2151. return map[fmt][be];
  2152. }
  2153. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2154. {
  2155. switch (codec_id) {
  2156. case AV_CODEC_ID_ADPCM_SBPRO_2:
  2157. return 2;
  2158. case AV_CODEC_ID_ADPCM_SBPRO_3:
  2159. return 3;
  2160. case AV_CODEC_ID_ADPCM_SBPRO_4:
  2161. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2162. case AV_CODEC_ID_ADPCM_IMA_QT:
  2163. case AV_CODEC_ID_ADPCM_SWF:
  2164. case AV_CODEC_ID_ADPCM_MS:
  2165. return 4;
  2166. default:
  2167. return av_get_exact_bits_per_sample(codec_id);
  2168. }
  2169. }
  2170. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2171. {
  2172. int id, sr, ch, ba, tag, bps;
  2173. id = avctx->codec_id;
  2174. sr = avctx->sample_rate;
  2175. ch = avctx->channels;
  2176. ba = avctx->block_align;
  2177. tag = avctx->codec_tag;
  2178. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  2179. /* codecs with an exact constant bits per sample */
  2180. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  2181. return (frame_bytes * 8LL) / (bps * ch);
  2182. bps = avctx->bits_per_coded_sample;
  2183. /* codecs with a fixed packet duration */
  2184. switch (id) {
  2185. case AV_CODEC_ID_ADPCM_ADX: return 32;
  2186. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  2187. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  2188. case AV_CODEC_ID_AMR_NB:
  2189. case AV_CODEC_ID_EVRC:
  2190. case AV_CODEC_ID_GSM:
  2191. case AV_CODEC_ID_QCELP:
  2192. case AV_CODEC_ID_RA_288: return 160;
  2193. case AV_CODEC_ID_AMR_WB:
  2194. case AV_CODEC_ID_GSM_MS: return 320;
  2195. case AV_CODEC_ID_MP1: return 384;
  2196. case AV_CODEC_ID_ATRAC1: return 512;
  2197. case AV_CODEC_ID_ATRAC3: return 1024;
  2198. case AV_CODEC_ID_MP2:
  2199. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2200. case AV_CODEC_ID_AC3: return 1536;
  2201. }
  2202. if (sr > 0) {
  2203. /* calc from sample rate */
  2204. if (id == AV_CODEC_ID_TTA)
  2205. return 256 * sr / 245;
  2206. if (ch > 0) {
  2207. /* calc from sample rate and channels */
  2208. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2209. return (480 << (sr / 22050)) / ch;
  2210. }
  2211. }
  2212. if (ba > 0) {
  2213. /* calc from block_align */
  2214. if (id == AV_CODEC_ID_SIPR) {
  2215. switch (ba) {
  2216. case 20: return 160;
  2217. case 19: return 144;
  2218. case 29: return 288;
  2219. case 37: return 480;
  2220. }
  2221. } else if (id == AV_CODEC_ID_ILBC) {
  2222. switch (ba) {
  2223. case 38: return 160;
  2224. case 50: return 240;
  2225. }
  2226. }
  2227. }
  2228. if (frame_bytes > 0) {
  2229. /* calc from frame_bytes only */
  2230. if (id == AV_CODEC_ID_TRUESPEECH)
  2231. return 240 * (frame_bytes / 32);
  2232. if (id == AV_CODEC_ID_NELLYMOSER)
  2233. return 256 * (frame_bytes / 64);
  2234. if (id == AV_CODEC_ID_RA_144)
  2235. return 160 * (frame_bytes / 20);
  2236. if (id == AV_CODEC_ID_G723_1)
  2237. return 240 * (frame_bytes / 24);
  2238. if (bps > 0) {
  2239. /* calc from frame_bytes and bits_per_coded_sample */
  2240. if (id == AV_CODEC_ID_ADPCM_G726)
  2241. return frame_bytes * 8 / bps;
  2242. }
  2243. if (ch > 0) {
  2244. /* calc from frame_bytes and channels */
  2245. switch (id) {
  2246. case AV_CODEC_ID_ADPCM_AFC:
  2247. return frame_bytes / (9 * ch) * 16;
  2248. case AV_CODEC_ID_ADPCM_4XM:
  2249. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2250. return (frame_bytes - 4 * ch) * 2 / ch;
  2251. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2252. return (frame_bytes - 4) * 2 / ch;
  2253. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2254. return (frame_bytes - 8) * 2 / ch;
  2255. case AV_CODEC_ID_ADPCM_XA:
  2256. return (frame_bytes / 128) * 224 / ch;
  2257. case AV_CODEC_ID_INTERPLAY_DPCM:
  2258. return (frame_bytes - 6 - ch) / ch;
  2259. case AV_CODEC_ID_ROQ_DPCM:
  2260. return (frame_bytes - 8) / ch;
  2261. case AV_CODEC_ID_XAN_DPCM:
  2262. return (frame_bytes - 2 * ch) / ch;
  2263. case AV_CODEC_ID_MACE3:
  2264. return 3 * frame_bytes / ch;
  2265. case AV_CODEC_ID_MACE6:
  2266. return 6 * frame_bytes / ch;
  2267. case AV_CODEC_ID_PCM_LXF:
  2268. return 2 * (frame_bytes / (5 * ch));
  2269. case AV_CODEC_ID_IAC:
  2270. case AV_CODEC_ID_IMC:
  2271. return 4 * frame_bytes / ch;
  2272. }
  2273. if (tag) {
  2274. /* calc from frame_bytes, channels, and codec_tag */
  2275. if (id == AV_CODEC_ID_SOL_DPCM) {
  2276. if (tag == 3)
  2277. return frame_bytes / ch;
  2278. else
  2279. return frame_bytes * 2 / ch;
  2280. }
  2281. }
  2282. if (ba > 0) {
  2283. /* calc from frame_bytes, channels, and block_align */
  2284. int blocks = frame_bytes / ba;
  2285. switch (avctx->codec_id) {
  2286. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2287. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  2288. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2289. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2290. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2291. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2292. case AV_CODEC_ID_ADPCM_MS:
  2293. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2294. }
  2295. }
  2296. if (bps > 0) {
  2297. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2298. switch (avctx->codec_id) {
  2299. case AV_CODEC_ID_PCM_DVD:
  2300. if(bps<4)
  2301. return 0;
  2302. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2303. case AV_CODEC_ID_PCM_BLURAY:
  2304. if(bps<4)
  2305. return 0;
  2306. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2307. case AV_CODEC_ID_S302M:
  2308. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2309. }
  2310. }
  2311. }
  2312. }
  2313. return 0;
  2314. }
  2315. #if !HAVE_THREADS
  2316. int ff_thread_init(AVCodecContext *s)
  2317. {
  2318. return -1;
  2319. }
  2320. #endif
  2321. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2322. {
  2323. unsigned int n = 0;
  2324. while (v >= 0xff) {
  2325. *s++ = 0xff;
  2326. v -= 0xff;
  2327. n++;
  2328. }
  2329. *s = v;
  2330. n++;
  2331. return n;
  2332. }
  2333. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2334. {
  2335. int i;
  2336. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2337. return i;
  2338. }
  2339. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2340. {
  2341. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  2342. "version to the newest one from Git. If the problem still "
  2343. "occurs, it means that your file has a feature which has not "
  2344. "been implemented.\n", feature);
  2345. if(want_sample)
  2346. av_log_ask_for_sample(avc, NULL);
  2347. }
  2348. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2349. {
  2350. va_list argument_list;
  2351. va_start(argument_list, msg);
  2352. if (msg)
  2353. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2354. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2355. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  2356. "and contact the ffmpeg-devel mailing list.\n");
  2357. va_end(argument_list);
  2358. }
  2359. static AVHWAccel *first_hwaccel = NULL;
  2360. void av_register_hwaccel(AVHWAccel *hwaccel)
  2361. {
  2362. AVHWAccel **p = &first_hwaccel;
  2363. while (*p)
  2364. p = &(*p)->next;
  2365. *p = hwaccel;
  2366. hwaccel->next = NULL;
  2367. }
  2368. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  2369. {
  2370. return hwaccel ? hwaccel->next : first_hwaccel;
  2371. }
  2372. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
  2373. {
  2374. AVHWAccel *hwaccel = NULL;
  2375. while ((hwaccel = av_hwaccel_next(hwaccel)))
  2376. if (hwaccel->id == codec_id
  2377. && hwaccel->pix_fmt == pix_fmt)
  2378. return hwaccel;
  2379. return NULL;
  2380. }
  2381. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2382. {
  2383. if (ff_lockmgr_cb) {
  2384. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  2385. return -1;
  2386. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  2387. return -1;
  2388. }
  2389. ff_lockmgr_cb = cb;
  2390. if (ff_lockmgr_cb) {
  2391. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  2392. return -1;
  2393. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2394. return -1;
  2395. }
  2396. return 0;
  2397. }
  2398. int ff_lock_avcodec(AVCodecContext *log_ctx)
  2399. {
  2400. if (ff_lockmgr_cb) {
  2401. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  2402. return -1;
  2403. }
  2404. entangled_thread_counter++;
  2405. if (entangled_thread_counter != 1) {
  2406. av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
  2407. ff_avcodec_locked = 1;
  2408. ff_unlock_avcodec();
  2409. return AVERROR(EINVAL);
  2410. }
  2411. av_assert0(!ff_avcodec_locked);
  2412. ff_avcodec_locked = 1;
  2413. return 0;
  2414. }
  2415. int ff_unlock_avcodec(void)
  2416. {
  2417. av_assert0(ff_avcodec_locked);
  2418. ff_avcodec_locked = 0;
  2419. entangled_thread_counter--;
  2420. if (ff_lockmgr_cb) {
  2421. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
  2422. return -1;
  2423. }
  2424. return 0;
  2425. }
  2426. int avpriv_lock_avformat(void)
  2427. {
  2428. if (ff_lockmgr_cb) {
  2429. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2430. return -1;
  2431. }
  2432. return 0;
  2433. }
  2434. int avpriv_unlock_avformat(void)
  2435. {
  2436. if (ff_lockmgr_cb) {
  2437. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2438. return -1;
  2439. }
  2440. return 0;
  2441. }
  2442. unsigned int avpriv_toupper4(unsigned int x)
  2443. {
  2444. return av_toupper(x & 0xFF) +
  2445. (av_toupper((x >> 8) & 0xFF) << 8) +
  2446. (av_toupper((x >> 16) & 0xFF) << 16) +
  2447. (av_toupper((x >> 24) & 0xFF) << 24);
  2448. }
  2449. #if !HAVE_THREADS
  2450. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  2451. {
  2452. f->owner = avctx;
  2453. return ff_get_buffer(avctx, f);
  2454. }
  2455. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  2456. {
  2457. f->owner->release_buffer(f->owner, f);
  2458. }
  2459. void ff_thread_finish_setup(AVCodecContext *avctx)
  2460. {
  2461. }
  2462. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  2463. {
  2464. }
  2465. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  2466. {
  2467. }
  2468. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2469. {
  2470. return 1;
  2471. }
  2472. #endif
  2473. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  2474. {
  2475. AVCodec *c= avcodec_find_decoder(codec_id);
  2476. if(!c)
  2477. c= avcodec_find_encoder(codec_id);
  2478. if(c)
  2479. return c->type;
  2480. if (codec_id <= AV_CODEC_ID_NONE)
  2481. return AVMEDIA_TYPE_UNKNOWN;
  2482. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  2483. return AVMEDIA_TYPE_VIDEO;
  2484. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  2485. return AVMEDIA_TYPE_AUDIO;
  2486. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  2487. return AVMEDIA_TYPE_SUBTITLE;
  2488. return AVMEDIA_TYPE_UNKNOWN;
  2489. }
  2490. int avcodec_is_open(AVCodecContext *s)
  2491. {
  2492. return !!s->internal;
  2493. }
  2494. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
  2495. {
  2496. int ret;
  2497. char *str;
  2498. ret = av_bprint_finalize(buf, &str);
  2499. if (ret < 0)
  2500. return ret;
  2501. avctx->extradata = str;
  2502. /* Note: the string is NUL terminated (so extradata can be read as a
  2503. * string), but the ending character is not accounted in the size (in
  2504. * binary formats you are likely not supposed to mux that character). When
  2505. * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
  2506. * zeros. */
  2507. avctx->extradata_size = buf->len;
  2508. return 0;
  2509. }