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.

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