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.

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