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.

2848 lines
91KB

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