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.

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