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.

3560 lines
117KB

  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/atomic.h"
  28. #include "libavutil/attributes.h"
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/bprint.h"
  32. #include "libavutil/channel_layout.h"
  33. #include "libavutil/crc.h"
  34. #include "libavutil/frame.h"
  35. #include "libavutil/internal.h"
  36. #include "libavutil/mathematics.h"
  37. #include "libavutil/mem_internal.h"
  38. #include "libavutil/pixdesc.h"
  39. #include "libavutil/imgutils.h"
  40. #include "libavutil/samplefmt.h"
  41. #include "libavutil/dict.h"
  42. #include "libavutil/thread.h"
  43. #include "avcodec.h"
  44. #include "libavutil/opt.h"
  45. #include "me_cmp.h"
  46. #include "mpegvideo.h"
  47. #include "thread.h"
  48. #include "frame_thread_encoder.h"
  49. #include "internal.h"
  50. #include "raw.h"
  51. #include "bytestream.h"
  52. #include "version.h"
  53. #include <stdlib.h>
  54. #include <stdarg.h>
  55. #include <limits.h>
  56. #include <float.h>
  57. #if CONFIG_ICONV
  58. # include <iconv.h>
  59. #endif
  60. #include "libavutil/ffversion.h"
  61. const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
  62. #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
  63. static int default_lockmgr_cb(void **arg, enum AVLockOp op)
  64. {
  65. void * volatile * mutex = arg;
  66. int err;
  67. switch (op) {
  68. case AV_LOCK_CREATE:
  69. return 0;
  70. case AV_LOCK_OBTAIN:
  71. if (!*mutex) {
  72. pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t));
  73. if (!tmp)
  74. return AVERROR(ENOMEM);
  75. if ((err = pthread_mutex_init(tmp, NULL))) {
  76. av_free(tmp);
  77. return AVERROR(err);
  78. }
  79. if (avpriv_atomic_ptr_cas(mutex, NULL, tmp)) {
  80. pthread_mutex_destroy(tmp);
  81. av_free(tmp);
  82. }
  83. }
  84. if ((err = pthread_mutex_lock(*mutex)))
  85. return AVERROR(err);
  86. return 0;
  87. case AV_LOCK_RELEASE:
  88. if ((err = pthread_mutex_unlock(*mutex)))
  89. return AVERROR(err);
  90. return 0;
  91. case AV_LOCK_DESTROY:
  92. if (*mutex)
  93. pthread_mutex_destroy(*mutex);
  94. av_free(*mutex);
  95. avpriv_atomic_ptr_cas(mutex, *mutex, NULL);
  96. return 0;
  97. }
  98. return 1;
  99. }
  100. static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = default_lockmgr_cb;
  101. #else
  102. static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = NULL;
  103. #endif
  104. volatile int ff_avcodec_locked;
  105. static int volatile entangled_thread_counter = 0;
  106. static void *codec_mutex;
  107. static void *avformat_mutex;
  108. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  109. {
  110. uint8_t **p = ptr;
  111. if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  112. av_freep(p);
  113. *size = 0;
  114. return;
  115. }
  116. if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
  117. memset(*p + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  118. }
  119. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  120. {
  121. uint8_t **p = ptr;
  122. if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  123. av_freep(p);
  124. *size = 0;
  125. return;
  126. }
  127. if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
  128. memset(*p, 0, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
  129. }
  130. /* encoder management */
  131. static AVCodec *first_avcodec = NULL;
  132. static AVCodec **last_avcodec = &first_avcodec;
  133. AVCodec *av_codec_next(const AVCodec *c)
  134. {
  135. if (c)
  136. return c->next;
  137. else
  138. return first_avcodec;
  139. }
  140. static av_cold void avcodec_init(void)
  141. {
  142. static int initialized = 0;
  143. if (initialized != 0)
  144. return;
  145. initialized = 1;
  146. if (CONFIG_ME_CMP)
  147. ff_me_cmp_init_static();
  148. }
  149. int av_codec_is_encoder(const AVCodec *codec)
  150. {
  151. return codec && (codec->encode_sub || codec->encode2);
  152. }
  153. int av_codec_is_decoder(const AVCodec *codec)
  154. {
  155. return codec && codec->decode;
  156. }
  157. av_cold void avcodec_register(AVCodec *codec)
  158. {
  159. AVCodec **p;
  160. avcodec_init();
  161. p = last_avcodec;
  162. codec->next = NULL;
  163. while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, codec))
  164. p = &(*p)->next;
  165. last_avcodec = &codec->next;
  166. if (codec->init_static_data)
  167. codec->init_static_data(codec);
  168. }
  169. #if FF_API_EMU_EDGE
  170. unsigned avcodec_get_edge_width(void)
  171. {
  172. return EDGE_WIDTH;
  173. }
  174. #endif
  175. #if FF_API_SET_DIMENSIONS
  176. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  177. {
  178. int ret = ff_set_dimensions(s, width, height);
  179. if (ret < 0) {
  180. av_log(s, AV_LOG_WARNING, "Failed to set dimensions %d %d\n", width, height);
  181. }
  182. }
  183. #endif
  184. int ff_set_dimensions(AVCodecContext *s, int width, int height)
  185. {
  186. int ret = av_image_check_size(width, height, 0, s);
  187. if (ret < 0)
  188. width = height = 0;
  189. s->coded_width = width;
  190. s->coded_height = height;
  191. s->width = AV_CEIL_RSHIFT(width, s->lowres);
  192. s->height = AV_CEIL_RSHIFT(height, s->lowres);
  193. return ret;
  194. }
  195. int ff_set_sar(AVCodecContext *avctx, AVRational sar)
  196. {
  197. int ret = av_image_check_sar(avctx->width, avctx->height, sar);
  198. if (ret < 0) {
  199. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
  200. sar.num, sar.den);
  201. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  202. return ret;
  203. } else {
  204. avctx->sample_aspect_ratio = sar;
  205. }
  206. return 0;
  207. }
  208. int ff_side_data_update_matrix_encoding(AVFrame *frame,
  209. enum AVMatrixEncoding matrix_encoding)
  210. {
  211. AVFrameSideData *side_data;
  212. enum AVMatrixEncoding *data;
  213. side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
  214. if (!side_data)
  215. side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
  216. sizeof(enum AVMatrixEncoding));
  217. if (!side_data)
  218. return AVERROR(ENOMEM);
  219. data = (enum AVMatrixEncoding*)side_data->data;
  220. *data = matrix_encoding;
  221. return 0;
  222. }
  223. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  224. int linesize_align[AV_NUM_DATA_POINTERS])
  225. {
  226. int i;
  227. int w_align = 1;
  228. int h_align = 1;
  229. AVPixFmtDescriptor const *desc = av_pix_fmt_desc_get(s->pix_fmt);
  230. if (desc) {
  231. w_align = 1 << desc->log2_chroma_w;
  232. h_align = 1 << desc->log2_chroma_h;
  233. }
  234. switch (s->pix_fmt) {
  235. case AV_PIX_FMT_YUV420P:
  236. case AV_PIX_FMT_YUYV422:
  237. case AV_PIX_FMT_YVYU422:
  238. case AV_PIX_FMT_UYVY422:
  239. case AV_PIX_FMT_YUV422P:
  240. case AV_PIX_FMT_YUV440P:
  241. case AV_PIX_FMT_YUV444P:
  242. case AV_PIX_FMT_GBRP:
  243. case AV_PIX_FMT_GBRAP:
  244. case AV_PIX_FMT_GRAY8:
  245. case AV_PIX_FMT_GRAY16BE:
  246. case AV_PIX_FMT_GRAY16LE:
  247. case AV_PIX_FMT_YUVJ420P:
  248. case AV_PIX_FMT_YUVJ422P:
  249. case AV_PIX_FMT_YUVJ440P:
  250. case AV_PIX_FMT_YUVJ444P:
  251. case AV_PIX_FMT_YUVA420P:
  252. case AV_PIX_FMT_YUVA422P:
  253. case AV_PIX_FMT_YUVA444P:
  254. case AV_PIX_FMT_YUV420P9LE:
  255. case AV_PIX_FMT_YUV420P9BE:
  256. case AV_PIX_FMT_YUV420P10LE:
  257. case AV_PIX_FMT_YUV420P10BE:
  258. case AV_PIX_FMT_YUV420P12LE:
  259. case AV_PIX_FMT_YUV420P12BE:
  260. case AV_PIX_FMT_YUV420P14LE:
  261. case AV_PIX_FMT_YUV420P14BE:
  262. case AV_PIX_FMT_YUV420P16LE:
  263. case AV_PIX_FMT_YUV420P16BE:
  264. case AV_PIX_FMT_YUVA420P9LE:
  265. case AV_PIX_FMT_YUVA420P9BE:
  266. case AV_PIX_FMT_YUVA420P10LE:
  267. case AV_PIX_FMT_YUVA420P10BE:
  268. case AV_PIX_FMT_YUVA420P16LE:
  269. case AV_PIX_FMT_YUVA420P16BE:
  270. case AV_PIX_FMT_YUV422P9LE:
  271. case AV_PIX_FMT_YUV422P9BE:
  272. case AV_PIX_FMT_YUV422P10LE:
  273. case AV_PIX_FMT_YUV422P10BE:
  274. case AV_PIX_FMT_YUV422P12LE:
  275. case AV_PIX_FMT_YUV422P12BE:
  276. case AV_PIX_FMT_YUV422P14LE:
  277. case AV_PIX_FMT_YUV422P14BE:
  278. case AV_PIX_FMT_YUV422P16LE:
  279. case AV_PIX_FMT_YUV422P16BE:
  280. case AV_PIX_FMT_YUVA422P9LE:
  281. case AV_PIX_FMT_YUVA422P9BE:
  282. case AV_PIX_FMT_YUVA422P10LE:
  283. case AV_PIX_FMT_YUVA422P10BE:
  284. case AV_PIX_FMT_YUVA422P16LE:
  285. case AV_PIX_FMT_YUVA422P16BE:
  286. case AV_PIX_FMT_YUV440P10LE:
  287. case AV_PIX_FMT_YUV440P10BE:
  288. case AV_PIX_FMT_YUV440P12LE:
  289. case AV_PIX_FMT_YUV440P12BE:
  290. case AV_PIX_FMT_YUV444P9LE:
  291. case AV_PIX_FMT_YUV444P9BE:
  292. case AV_PIX_FMT_YUV444P10LE:
  293. case AV_PIX_FMT_YUV444P10BE:
  294. case AV_PIX_FMT_YUV444P12LE:
  295. case AV_PIX_FMT_YUV444P12BE:
  296. case AV_PIX_FMT_YUV444P14LE:
  297. case AV_PIX_FMT_YUV444P14BE:
  298. case AV_PIX_FMT_YUV444P16LE:
  299. case AV_PIX_FMT_YUV444P16BE:
  300. case AV_PIX_FMT_YUVA444P9LE:
  301. case AV_PIX_FMT_YUVA444P9BE:
  302. case AV_PIX_FMT_YUVA444P10LE:
  303. case AV_PIX_FMT_YUVA444P10BE:
  304. case AV_PIX_FMT_YUVA444P16LE:
  305. case AV_PIX_FMT_YUVA444P16BE:
  306. case AV_PIX_FMT_GBRP9LE:
  307. case AV_PIX_FMT_GBRP9BE:
  308. case AV_PIX_FMT_GBRP10LE:
  309. case AV_PIX_FMT_GBRP10BE:
  310. case AV_PIX_FMT_GBRP12LE:
  311. case AV_PIX_FMT_GBRP12BE:
  312. case AV_PIX_FMT_GBRP14LE:
  313. case AV_PIX_FMT_GBRP14BE:
  314. case AV_PIX_FMT_GBRP16LE:
  315. case AV_PIX_FMT_GBRP16BE:
  316. case AV_PIX_FMT_GBRAP12LE:
  317. case AV_PIX_FMT_GBRAP12BE:
  318. case AV_PIX_FMT_GBRAP16LE:
  319. case AV_PIX_FMT_GBRAP16BE:
  320. w_align = 16; //FIXME assume 16 pixel per macroblock
  321. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  322. break;
  323. case AV_PIX_FMT_YUV411P:
  324. case AV_PIX_FMT_YUVJ411P:
  325. case AV_PIX_FMT_UYYVYY411:
  326. w_align = 32;
  327. h_align = 16 * 2;
  328. break;
  329. case AV_PIX_FMT_YUV410P:
  330. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  331. w_align = 64;
  332. h_align = 64;
  333. }
  334. break;
  335. case AV_PIX_FMT_RGB555:
  336. if (s->codec_id == AV_CODEC_ID_RPZA) {
  337. w_align = 4;
  338. h_align = 4;
  339. }
  340. break;
  341. case AV_PIX_FMT_PAL8:
  342. case AV_PIX_FMT_BGR8:
  343. case AV_PIX_FMT_RGB8:
  344. if (s->codec_id == AV_CODEC_ID_SMC ||
  345. s->codec_id == AV_CODEC_ID_CINEPAK) {
  346. w_align = 4;
  347. h_align = 4;
  348. }
  349. if (s->codec_id == AV_CODEC_ID_JV) {
  350. w_align = 8;
  351. h_align = 8;
  352. }
  353. break;
  354. case AV_PIX_FMT_BGR24:
  355. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  356. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  357. w_align = 4;
  358. h_align = 4;
  359. }
  360. break;
  361. case AV_PIX_FMT_RGB24:
  362. if (s->codec_id == AV_CODEC_ID_CINEPAK) {
  363. w_align = 4;
  364. h_align = 4;
  365. }
  366. break;
  367. default:
  368. break;
  369. }
  370. if (s->codec_id == AV_CODEC_ID_IFF_ILBM) {
  371. w_align = FFMAX(w_align, 8);
  372. }
  373. *width = FFALIGN(*width, w_align);
  374. *height = FFALIGN(*height, h_align);
  375. if (s->codec_id == AV_CODEC_ID_H264 || s->lowres) {
  376. // some of the optimized chroma MC reads one line too much
  377. // which is also done in mpeg decoders with lowres > 0
  378. *height += 2;
  379. // H.264 uses edge emulation for out of frame motion vectors, for this
  380. // it requires a temporary area large enough to hold a 21x21 block,
  381. // increasing witdth ensure that the temporary area is large enough,
  382. // the next rounded up width is 32
  383. *width = FFMAX(*width, 32);
  384. }
  385. for (i = 0; i < 4; i++)
  386. linesize_align[i] = STRIDE_ALIGN;
  387. }
  388. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  389. {
  390. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  391. int chroma_shift = desc->log2_chroma_w;
  392. int linesize_align[AV_NUM_DATA_POINTERS];
  393. int align;
  394. avcodec_align_dimensions2(s, width, height, linesize_align);
  395. align = FFMAX(linesize_align[0], linesize_align[3]);
  396. linesize_align[1] <<= chroma_shift;
  397. linesize_align[2] <<= chroma_shift;
  398. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  399. *width = FFALIGN(*width, align);
  400. }
  401. int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
  402. {
  403. if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
  404. return AVERROR(EINVAL);
  405. pos--;
  406. *xpos = (pos&1) * 128;
  407. *ypos = ((pos>>1)^(pos<4)) * 128;
  408. return 0;
  409. }
  410. enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
  411. {
  412. int pos, xout, yout;
  413. for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
  414. if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
  415. return pos;
  416. }
  417. return AVCHROMA_LOC_UNSPECIFIED;
  418. }
  419. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  420. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  421. int buf_size, int align)
  422. {
  423. int ch, planar, needed_size, ret = 0;
  424. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  425. frame->nb_samples, sample_fmt,
  426. align);
  427. if (buf_size < needed_size)
  428. return AVERROR(EINVAL);
  429. planar = av_sample_fmt_is_planar(sample_fmt);
  430. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  431. if (!(frame->extended_data = av_mallocz_array(nb_channels,
  432. sizeof(*frame->extended_data))))
  433. return AVERROR(ENOMEM);
  434. } else {
  435. frame->extended_data = frame->data;
  436. }
  437. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  438. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  439. sample_fmt, align)) < 0) {
  440. if (frame->extended_data != frame->data)
  441. av_freep(&frame->extended_data);
  442. return ret;
  443. }
  444. if (frame->extended_data != frame->data) {
  445. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  446. frame->data[ch] = frame->extended_data[ch];
  447. }
  448. return ret;
  449. }
  450. static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
  451. {
  452. FramePool *pool = avctx->internal->pool;
  453. int i, ret;
  454. switch (avctx->codec_type) {
  455. case AVMEDIA_TYPE_VIDEO: {
  456. uint8_t *data[4];
  457. int linesize[4];
  458. int size[4] = { 0 };
  459. int w = frame->width;
  460. int h = frame->height;
  461. int tmpsize, unaligned;
  462. if (pool->format == frame->format &&
  463. pool->width == frame->width && pool->height == frame->height)
  464. return 0;
  465. avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
  466. do {
  467. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  468. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  469. ret = av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
  470. if (ret < 0)
  471. return ret;
  472. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  473. w += w & ~(w - 1);
  474. unaligned = 0;
  475. for (i = 0; i < 4; i++)
  476. unaligned |= linesize[i] % pool->stride_align[i];
  477. } while (unaligned);
  478. tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,
  479. NULL, linesize);
  480. if (tmpsize < 0)
  481. return -1;
  482. for (i = 0; i < 3 && data[i + 1]; i++)
  483. size[i] = data[i + 1] - data[i];
  484. size[i] = tmpsize - (data[i] - data[0]);
  485. for (i = 0; i < 4; i++) {
  486. av_buffer_pool_uninit(&pool->pools[i]);
  487. pool->linesize[i] = linesize[i];
  488. if (size[i]) {
  489. pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
  490. CONFIG_MEMORY_POISONING ?
  491. NULL :
  492. av_buffer_allocz);
  493. if (!pool->pools[i]) {
  494. ret = AVERROR(ENOMEM);
  495. goto fail;
  496. }
  497. }
  498. }
  499. pool->format = frame->format;
  500. pool->width = frame->width;
  501. pool->height = frame->height;
  502. break;
  503. }
  504. case AVMEDIA_TYPE_AUDIO: {
  505. int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
  506. int planar = av_sample_fmt_is_planar(frame->format);
  507. int planes = planar ? ch : 1;
  508. if (pool->format == frame->format && pool->planes == planes &&
  509. pool->channels == ch && frame->nb_samples == pool->samples)
  510. return 0;
  511. av_buffer_pool_uninit(&pool->pools[0]);
  512. ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
  513. frame->nb_samples, frame->format, 0);
  514. if (ret < 0)
  515. goto fail;
  516. pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
  517. if (!pool->pools[0]) {
  518. ret = AVERROR(ENOMEM);
  519. goto fail;
  520. }
  521. pool->format = frame->format;
  522. pool->planes = planes;
  523. pool->channels = ch;
  524. pool->samples = frame->nb_samples;
  525. break;
  526. }
  527. default: av_assert0(0);
  528. }
  529. return 0;
  530. fail:
  531. for (i = 0; i < 4; i++)
  532. av_buffer_pool_uninit(&pool->pools[i]);
  533. pool->format = -1;
  534. pool->planes = pool->channels = pool->samples = 0;
  535. pool->width = pool->height = 0;
  536. return ret;
  537. }
  538. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  539. {
  540. FramePool *pool = avctx->internal->pool;
  541. int planes = pool->planes;
  542. int i;
  543. frame->linesize[0] = pool->linesize[0];
  544. if (planes > AV_NUM_DATA_POINTERS) {
  545. frame->extended_data = av_mallocz_array(planes, sizeof(*frame->extended_data));
  546. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  547. frame->extended_buf = av_mallocz_array(frame->nb_extended_buf,
  548. sizeof(*frame->extended_buf));
  549. if (!frame->extended_data || !frame->extended_buf) {
  550. av_freep(&frame->extended_data);
  551. av_freep(&frame->extended_buf);
  552. return AVERROR(ENOMEM);
  553. }
  554. } else {
  555. frame->extended_data = frame->data;
  556. av_assert0(frame->nb_extended_buf == 0);
  557. }
  558. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  559. frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
  560. if (!frame->buf[i])
  561. goto fail;
  562. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  563. }
  564. for (i = 0; i < frame->nb_extended_buf; i++) {
  565. frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
  566. if (!frame->extended_buf[i])
  567. goto fail;
  568. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  569. }
  570. if (avctx->debug & FF_DEBUG_BUFFERS)
  571. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
  572. return 0;
  573. fail:
  574. av_frame_unref(frame);
  575. return AVERROR(ENOMEM);
  576. }
  577. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  578. {
  579. FramePool *pool = s->internal->pool;
  580. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
  581. int i;
  582. if (pic->data[0]) {
  583. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  584. return -1;
  585. }
  586. if (!desc) {
  587. av_log(s, AV_LOG_ERROR,
  588. "Unable to get pixel format descriptor for format %s\n",
  589. av_get_pix_fmt_name(pic->format));
  590. return AVERROR(EINVAL);
  591. }
  592. memset(pic->data, 0, sizeof(pic->data));
  593. pic->extended_data = pic->data;
  594. for (i = 0; i < 4 && pool->pools[i]; i++) {
  595. pic->linesize[i] = pool->linesize[i];
  596. pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
  597. if (!pic->buf[i])
  598. goto fail;
  599. pic->data[i] = pic->buf[i]->data;
  600. }
  601. for (; i < AV_NUM_DATA_POINTERS; i++) {
  602. pic->data[i] = NULL;
  603. pic->linesize[i] = 0;
  604. }
  605. if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
  606. desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
  607. avpriv_set_systematic_pal2((uint32_t *)pic->data[1], pic->format);
  608. if (s->debug & FF_DEBUG_BUFFERS)
  609. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
  610. return 0;
  611. fail:
  612. av_frame_unref(pic);
  613. return AVERROR(ENOMEM);
  614. }
  615. void ff_color_frame(AVFrame *frame, const int c[4])
  616. {
  617. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  618. int p, y, x;
  619. av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
  620. for (p = 0; p<desc->nb_components; p++) {
  621. uint8_t *dst = frame->data[p];
  622. int is_chroma = p == 1 || p == 2;
  623. int bytes = is_chroma ? AV_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
  624. int height = is_chroma ? AV_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
  625. for (y = 0; y < height; y++) {
  626. if (desc->comp[0].depth >= 9) {
  627. for (x = 0; x<bytes; x++)
  628. ((uint16_t*)dst)[x] = c[p];
  629. }else
  630. memset(dst, c[p], bytes);
  631. dst += frame->linesize[p];
  632. }
  633. }
  634. }
  635. int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
  636. {
  637. int ret;
  638. if ((ret = update_frame_pool(avctx, frame)) < 0)
  639. return ret;
  640. switch (avctx->codec_type) {
  641. case AVMEDIA_TYPE_VIDEO:
  642. return video_get_buffer(avctx, frame);
  643. case AVMEDIA_TYPE_AUDIO:
  644. return audio_get_buffer(avctx, frame);
  645. default:
  646. return -1;
  647. }
  648. }
  649. static int add_metadata_from_side_data(AVPacket *avpkt, AVFrame *frame)
  650. {
  651. int size;
  652. const uint8_t *side_metadata;
  653. AVDictionary **frame_md = avpriv_frame_get_metadatap(frame);
  654. side_metadata = av_packet_get_side_data(avpkt,
  655. AV_PKT_DATA_STRINGS_METADATA, &size);
  656. return av_packet_unpack_dictionary(side_metadata, size, frame_md);
  657. }
  658. int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
  659. {
  660. AVPacket *pkt = avctx->internal->pkt;
  661. int i;
  662. static const struct {
  663. enum AVPacketSideDataType packet;
  664. enum AVFrameSideDataType frame;
  665. } sd[] = {
  666. { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
  667. { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
  668. { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
  669. { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
  670. };
  671. if (pkt) {
  672. frame->pkt_pts = pkt->pts;
  673. av_frame_set_pkt_pos (frame, pkt->pos);
  674. av_frame_set_pkt_duration(frame, pkt->duration);
  675. av_frame_set_pkt_size (frame, pkt->size);
  676. for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
  677. int size;
  678. uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
  679. if (packet_sd) {
  680. AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
  681. sd[i].frame,
  682. size);
  683. if (!frame_sd)
  684. return AVERROR(ENOMEM);
  685. memcpy(frame_sd->data, packet_sd, size);
  686. }
  687. }
  688. add_metadata_from_side_data(pkt, frame);
  689. } else {
  690. frame->pkt_pts = AV_NOPTS_VALUE;
  691. av_frame_set_pkt_pos (frame, -1);
  692. av_frame_set_pkt_duration(frame, 0);
  693. av_frame_set_pkt_size (frame, -1);
  694. }
  695. frame->reordered_opaque = avctx->reordered_opaque;
  696. if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
  697. frame->color_primaries = avctx->color_primaries;
  698. if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
  699. frame->color_trc = avctx->color_trc;
  700. if (av_frame_get_colorspace(frame) == AVCOL_SPC_UNSPECIFIED)
  701. av_frame_set_colorspace(frame, avctx->colorspace);
  702. if (av_frame_get_color_range(frame) == AVCOL_RANGE_UNSPECIFIED)
  703. av_frame_set_color_range(frame, avctx->color_range);
  704. if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
  705. frame->chroma_location = avctx->chroma_sample_location;
  706. switch (avctx->codec->type) {
  707. case AVMEDIA_TYPE_VIDEO:
  708. frame->format = avctx->pix_fmt;
  709. if (!frame->sample_aspect_ratio.num)
  710. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  711. if (frame->width && frame->height &&
  712. av_image_check_sar(frame->width, frame->height,
  713. frame->sample_aspect_ratio) < 0) {
  714. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  715. frame->sample_aspect_ratio.num,
  716. frame->sample_aspect_ratio.den);
  717. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  718. }
  719. break;
  720. case AVMEDIA_TYPE_AUDIO:
  721. if (!frame->sample_rate)
  722. frame->sample_rate = avctx->sample_rate;
  723. if (frame->format < 0)
  724. frame->format = avctx->sample_fmt;
  725. if (!frame->channel_layout) {
  726. if (avctx->channel_layout) {
  727. if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  728. avctx->channels) {
  729. av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  730. "configuration.\n");
  731. return AVERROR(EINVAL);
  732. }
  733. frame->channel_layout = avctx->channel_layout;
  734. } else {
  735. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  736. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  737. avctx->channels);
  738. return AVERROR(ENOSYS);
  739. }
  740. }
  741. }
  742. av_frame_set_channels(frame, avctx->channels);
  743. break;
  744. }
  745. return 0;
  746. }
  747. int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
  748. {
  749. return ff_init_buffer_info(avctx, frame);
  750. }
  751. static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
  752. {
  753. const AVHWAccel *hwaccel = avctx->hwaccel;
  754. int override_dimensions = 1;
  755. int ret;
  756. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  757. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
  758. av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
  759. return AVERROR(EINVAL);
  760. }
  761. }
  762. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  763. if (frame->width <= 0 || frame->height <= 0) {
  764. frame->width = FFMAX(avctx->width, AV_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
  765. frame->height = FFMAX(avctx->height, AV_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
  766. override_dimensions = 0;
  767. }
  768. }
  769. ret = ff_decode_frame_props(avctx, frame);
  770. if (ret < 0)
  771. return ret;
  772. if (hwaccel) {
  773. if (hwaccel->alloc_frame) {
  774. ret = hwaccel->alloc_frame(avctx, frame);
  775. goto end;
  776. }
  777. } else
  778. avctx->sw_pix_fmt = avctx->pix_fmt;
  779. ret = avctx->get_buffer2(avctx, frame, flags);
  780. end:
  781. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
  782. frame->width = avctx->width;
  783. frame->height = avctx->height;
  784. }
  785. return ret;
  786. }
  787. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  788. {
  789. int ret = get_buffer_internal(avctx, frame, flags);
  790. if (ret < 0) {
  791. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  792. frame->width = frame->height = 0;
  793. }
  794. return ret;
  795. }
  796. static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
  797. {
  798. AVFrame *tmp;
  799. int ret;
  800. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  801. if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
  802. av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  803. frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
  804. av_frame_unref(frame);
  805. }
  806. ff_init_buffer_info(avctx, frame);
  807. if (!frame->data[0])
  808. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  809. if (av_frame_is_writable(frame))
  810. return ff_decode_frame_props(avctx, frame);
  811. tmp = av_frame_alloc();
  812. if (!tmp)
  813. return AVERROR(ENOMEM);
  814. av_frame_move_ref(tmp, frame);
  815. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  816. if (ret < 0) {
  817. av_frame_free(&tmp);
  818. return ret;
  819. }
  820. av_frame_copy(frame, tmp);
  821. av_frame_free(&tmp);
  822. return 0;
  823. }
  824. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  825. {
  826. int ret = reget_buffer_internal(avctx, frame);
  827. if (ret < 0)
  828. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  829. return ret;
  830. }
  831. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  832. {
  833. int i;
  834. for (i = 0; i < count; i++) {
  835. int r = func(c, (char *)arg + i * size);
  836. if (ret)
  837. ret[i] = r;
  838. }
  839. return 0;
  840. }
  841. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  842. {
  843. int i;
  844. for (i = 0; i < count; i++) {
  845. int r = func(c, arg, i, 0);
  846. if (ret)
  847. ret[i] = r;
  848. }
  849. return 0;
  850. }
  851. enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
  852. unsigned int fourcc)
  853. {
  854. while (tags->pix_fmt >= 0) {
  855. if (tags->fourcc == fourcc)
  856. return tags->pix_fmt;
  857. tags++;
  858. }
  859. return AV_PIX_FMT_NONE;
  860. }
  861. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  862. {
  863. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  864. return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
  865. }
  866. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  867. {
  868. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  869. ++fmt;
  870. return fmt[0];
  871. }
  872. static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
  873. enum AVPixelFormat pix_fmt)
  874. {
  875. AVHWAccel *hwaccel = NULL;
  876. while ((hwaccel = av_hwaccel_next(hwaccel)))
  877. if (hwaccel->id == codec_id
  878. && hwaccel->pix_fmt == pix_fmt)
  879. return hwaccel;
  880. return NULL;
  881. }
  882. static int setup_hwaccel(AVCodecContext *avctx,
  883. const enum AVPixelFormat fmt,
  884. const char *name)
  885. {
  886. AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
  887. int ret = 0;
  888. if (avctx->active_thread_type & FF_THREAD_FRAME) {
  889. av_log(avctx, AV_LOG_WARNING,
  890. "Hardware accelerated decoding with frame threading is known to be unstable and its use is discouraged.\n");
  891. }
  892. if (!hwa) {
  893. av_log(avctx, AV_LOG_ERROR,
  894. "Could not find an AVHWAccel for the pixel format: %s",
  895. name);
  896. return AVERROR(ENOENT);
  897. }
  898. if (hwa->capabilities & HWACCEL_CODEC_CAP_EXPERIMENTAL &&
  899. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  900. av_log(avctx, AV_LOG_WARNING, "Ignoring experimental hwaccel: %s\n",
  901. hwa->name);
  902. return AVERROR_PATCHWELCOME;
  903. }
  904. if (hwa->priv_data_size) {
  905. avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
  906. if (!avctx->internal->hwaccel_priv_data)
  907. return AVERROR(ENOMEM);
  908. }
  909. if (hwa->init) {
  910. ret = hwa->init(avctx);
  911. if (ret < 0) {
  912. av_freep(&avctx->internal->hwaccel_priv_data);
  913. return ret;
  914. }
  915. }
  916. avctx->hwaccel = hwa;
  917. return 0;
  918. }
  919. int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  920. {
  921. const AVPixFmtDescriptor *desc;
  922. enum AVPixelFormat *choices;
  923. enum AVPixelFormat ret;
  924. unsigned n = 0;
  925. while (fmt[n] != AV_PIX_FMT_NONE)
  926. ++n;
  927. av_assert0(n >= 1);
  928. avctx->sw_pix_fmt = fmt[n - 1];
  929. av_assert2(!is_hwaccel_pix_fmt(avctx->sw_pix_fmt));
  930. choices = av_malloc_array(n + 1, sizeof(*choices));
  931. if (!choices)
  932. return AV_PIX_FMT_NONE;
  933. memcpy(choices, fmt, (n + 1) * sizeof(*choices));
  934. for (;;) {
  935. if (avctx->hwaccel && avctx->hwaccel->uninit)
  936. avctx->hwaccel->uninit(avctx);
  937. av_freep(&avctx->internal->hwaccel_priv_data);
  938. avctx->hwaccel = NULL;
  939. ret = avctx->get_format(avctx, choices);
  940. desc = av_pix_fmt_desc_get(ret);
  941. if (!desc) {
  942. ret = AV_PIX_FMT_NONE;
  943. break;
  944. }
  945. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
  946. break;
  947. #if FF_API_CAP_VDPAU
  948. if (avctx->codec->capabilities&AV_CODEC_CAP_HWACCEL_VDPAU)
  949. break;
  950. #endif
  951. if (!setup_hwaccel(avctx, ret, desc->name))
  952. break;
  953. /* Remove failed hwaccel from choices */
  954. for (n = 0; choices[n] != ret; n++)
  955. av_assert0(choices[n] != AV_PIX_FMT_NONE);
  956. do
  957. choices[n] = choices[n + 1];
  958. while (choices[n++] != AV_PIX_FMT_NONE);
  959. }
  960. av_freep(&choices);
  961. return ret;
  962. }
  963. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  964. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  965. MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
  966. MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
  967. MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
  968. unsigned av_codec_get_codec_properties(const AVCodecContext *codec)
  969. {
  970. return codec->properties;
  971. }
  972. int av_codec_get_max_lowres(const AVCodec *codec)
  973. {
  974. return codec->max_lowres;
  975. }
  976. static void get_subtitle_defaults(AVSubtitle *sub)
  977. {
  978. memset(sub, 0, sizeof(*sub));
  979. sub->pts = AV_NOPTS_VALUE;
  980. }
  981. static int64_t get_bit_rate(AVCodecContext *ctx)
  982. {
  983. int64_t bit_rate;
  984. int bits_per_sample;
  985. switch (ctx->codec_type) {
  986. case AVMEDIA_TYPE_VIDEO:
  987. case AVMEDIA_TYPE_DATA:
  988. case AVMEDIA_TYPE_SUBTITLE:
  989. case AVMEDIA_TYPE_ATTACHMENT:
  990. bit_rate = ctx->bit_rate;
  991. break;
  992. case AVMEDIA_TYPE_AUDIO:
  993. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  994. bit_rate = bits_per_sample ? ctx->sample_rate * (int64_t)ctx->channels * bits_per_sample : ctx->bit_rate;
  995. break;
  996. default:
  997. bit_rate = 0;
  998. break;
  999. }
  1000. return bit_rate;
  1001. }
  1002. int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  1003. {
  1004. int ret = 0;
  1005. ff_unlock_avcodec(codec);
  1006. ret = avcodec_open2(avctx, codec, options);
  1007. ff_lock_avcodec(avctx, codec);
  1008. return ret;
  1009. }
  1010. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  1011. {
  1012. int ret = 0;
  1013. AVDictionary *tmp = NULL;
  1014. const AVPixFmtDescriptor *pixdesc;
  1015. if (avcodec_is_open(avctx))
  1016. return 0;
  1017. if ((!codec && !avctx->codec)) {
  1018. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  1019. return AVERROR(EINVAL);
  1020. }
  1021. if ((codec && avctx->codec && codec != avctx->codec)) {
  1022. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  1023. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  1024. return AVERROR(EINVAL);
  1025. }
  1026. if (!codec)
  1027. codec = avctx->codec;
  1028. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  1029. return AVERROR(EINVAL);
  1030. if (options)
  1031. av_dict_copy(&tmp, *options, 0);
  1032. ret = ff_lock_avcodec(avctx, codec);
  1033. if (ret < 0)
  1034. return ret;
  1035. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  1036. if (!avctx->internal) {
  1037. ret = AVERROR(ENOMEM);
  1038. goto end;
  1039. }
  1040. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  1041. if (!avctx->internal->pool) {
  1042. ret = AVERROR(ENOMEM);
  1043. goto free_and_end;
  1044. }
  1045. avctx->internal->to_free = av_frame_alloc();
  1046. if (!avctx->internal->to_free) {
  1047. ret = AVERROR(ENOMEM);
  1048. goto free_and_end;
  1049. }
  1050. if (codec->priv_data_size > 0) {
  1051. if (!avctx->priv_data) {
  1052. avctx->priv_data = av_mallocz(codec->priv_data_size);
  1053. if (!avctx->priv_data) {
  1054. ret = AVERROR(ENOMEM);
  1055. goto end;
  1056. }
  1057. if (codec->priv_class) {
  1058. *(const AVClass **)avctx->priv_data = codec->priv_class;
  1059. av_opt_set_defaults(avctx->priv_data);
  1060. }
  1061. }
  1062. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  1063. goto free_and_end;
  1064. } else {
  1065. avctx->priv_data = NULL;
  1066. }
  1067. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  1068. goto free_and_end;
  1069. if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
  1070. av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
  1071. ret = AVERROR(EINVAL);
  1072. goto free_and_end;
  1073. }
  1074. // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
  1075. if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
  1076. (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
  1077. if (avctx->coded_width && avctx->coded_height)
  1078. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  1079. else if (avctx->width && avctx->height)
  1080. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1081. if (ret < 0)
  1082. goto free_and_end;
  1083. }
  1084. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  1085. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  1086. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  1087. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  1088. ff_set_dimensions(avctx, 0, 0);
  1089. }
  1090. if (avctx->width > 0 && avctx->height > 0) {
  1091. if (av_image_check_sar(avctx->width, avctx->height,
  1092. avctx->sample_aspect_ratio) < 0) {
  1093. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  1094. avctx->sample_aspect_ratio.num,
  1095. avctx->sample_aspect_ratio.den);
  1096. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  1097. }
  1098. }
  1099. /* if the decoder init function was already called previously,
  1100. * free the already allocated subtitle_header before overwriting it */
  1101. if (av_codec_is_decoder(codec))
  1102. av_freep(&avctx->subtitle_header);
  1103. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  1104. ret = AVERROR(EINVAL);
  1105. goto free_and_end;
  1106. }
  1107. avctx->codec = codec;
  1108. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  1109. avctx->codec_id == AV_CODEC_ID_NONE) {
  1110. avctx->codec_type = codec->type;
  1111. avctx->codec_id = codec->id;
  1112. }
  1113. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  1114. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  1115. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  1116. ret = AVERROR(EINVAL);
  1117. goto free_and_end;
  1118. }
  1119. avctx->frame_number = 0;
  1120. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  1121. if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
  1122. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  1123. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  1124. AVCodec *codec2;
  1125. av_log(avctx, AV_LOG_ERROR,
  1126. "The %s '%s' is experimental but experimental codecs are not enabled, "
  1127. "add '-strict %d' if you want to use it.\n",
  1128. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  1129. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  1130. if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
  1131. av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  1132. codec_string, codec2->name);
  1133. ret = AVERROR_EXPERIMENTAL;
  1134. goto free_and_end;
  1135. }
  1136. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  1137. (!avctx->time_base.num || !avctx->time_base.den)) {
  1138. avctx->time_base.num = 1;
  1139. avctx->time_base.den = avctx->sample_rate;
  1140. }
  1141. if (!HAVE_THREADS)
  1142. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  1143. if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec)) {
  1144. ff_unlock_avcodec(codec); //we will instantiate a few encoders thus kick the counter to prevent false detection of a problem
  1145. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  1146. ff_lock_avcodec(avctx, codec);
  1147. if (ret < 0)
  1148. goto free_and_end;
  1149. }
  1150. if (HAVE_THREADS
  1151. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  1152. ret = ff_thread_init(avctx);
  1153. if (ret < 0) {
  1154. goto free_and_end;
  1155. }
  1156. }
  1157. if (!HAVE_THREADS && !(codec->capabilities & AV_CODEC_CAP_AUTO_THREADS))
  1158. avctx->thread_count = 1;
  1159. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  1160. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  1161. avctx->codec->max_lowres);
  1162. ret = AVERROR(EINVAL);
  1163. goto free_and_end;
  1164. }
  1165. #if FF_API_VISMV
  1166. if (avctx->debug_mv)
  1167. av_log(avctx, AV_LOG_WARNING, "The 'vismv' option is deprecated, "
  1168. "see the codecview filter instead.\n");
  1169. #endif
  1170. if (av_codec_is_encoder(avctx->codec)) {
  1171. int i;
  1172. #if FF_API_CODED_FRAME
  1173. FF_DISABLE_DEPRECATION_WARNINGS
  1174. avctx->coded_frame = av_frame_alloc();
  1175. if (!avctx->coded_frame) {
  1176. ret = AVERROR(ENOMEM);
  1177. goto free_and_end;
  1178. }
  1179. FF_ENABLE_DEPRECATION_WARNINGS
  1180. #endif
  1181. if (avctx->codec->sample_fmts) {
  1182. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  1183. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  1184. break;
  1185. if (avctx->channels == 1 &&
  1186. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  1187. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  1188. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  1189. break;
  1190. }
  1191. }
  1192. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  1193. char buf[128];
  1194. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  1195. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  1196. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  1197. ret = AVERROR(EINVAL);
  1198. goto free_and_end;
  1199. }
  1200. }
  1201. if (avctx->codec->pix_fmts) {
  1202. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  1203. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  1204. break;
  1205. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  1206. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  1207. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  1208. char buf[128];
  1209. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  1210. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  1211. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  1212. ret = AVERROR(EINVAL);
  1213. goto free_and_end;
  1214. }
  1215. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
  1216. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
  1217. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
  1218. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
  1219. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
  1220. avctx->color_range = AVCOL_RANGE_JPEG;
  1221. }
  1222. if (avctx->codec->supported_samplerates) {
  1223. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  1224. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  1225. break;
  1226. if (avctx->codec->supported_samplerates[i] == 0) {
  1227. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  1228. avctx->sample_rate);
  1229. ret = AVERROR(EINVAL);
  1230. goto free_and_end;
  1231. }
  1232. }
  1233. if (avctx->sample_rate < 0) {
  1234. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  1235. avctx->sample_rate);
  1236. ret = AVERROR(EINVAL);
  1237. goto free_and_end;
  1238. }
  1239. if (avctx->codec->channel_layouts) {
  1240. if (!avctx->channel_layout) {
  1241. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  1242. } else {
  1243. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  1244. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  1245. break;
  1246. if (avctx->codec->channel_layouts[i] == 0) {
  1247. char buf[512];
  1248. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1249. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  1250. ret = AVERROR(EINVAL);
  1251. goto free_and_end;
  1252. }
  1253. }
  1254. }
  1255. if (avctx->channel_layout && avctx->channels) {
  1256. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1257. if (channels != avctx->channels) {
  1258. char buf[512];
  1259. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1260. av_log(avctx, AV_LOG_ERROR,
  1261. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  1262. buf, channels, avctx->channels);
  1263. ret = AVERROR(EINVAL);
  1264. goto free_and_end;
  1265. }
  1266. } else if (avctx->channel_layout) {
  1267. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1268. }
  1269. if (avctx->channels < 0) {
  1270. av_log(avctx, AV_LOG_ERROR, "Specified number of channels %d is not supported\n",
  1271. avctx->channels);
  1272. ret = AVERROR(EINVAL);
  1273. goto free_and_end;
  1274. }
  1275. if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  1276. pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1277. if ( avctx->bits_per_raw_sample < 0
  1278. || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
  1279. av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
  1280. avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
  1281. avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
  1282. }
  1283. if (avctx->width <= 0 || avctx->height <= 0) {
  1284. av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
  1285. ret = AVERROR(EINVAL);
  1286. goto free_and_end;
  1287. }
  1288. }
  1289. if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
  1290. && avctx->bit_rate>0 && avctx->bit_rate<1000) {
  1291. av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", (int64_t)avctx->bit_rate, (int64_t)avctx->bit_rate);
  1292. }
  1293. if (!avctx->rc_initial_buffer_occupancy)
  1294. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  1295. if (avctx->ticks_per_frame && avctx->time_base.num &&
  1296. avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
  1297. av_log(avctx, AV_LOG_ERROR,
  1298. "ticks_per_frame %d too large for the timebase %d/%d.",
  1299. avctx->ticks_per_frame,
  1300. avctx->time_base.num,
  1301. avctx->time_base.den);
  1302. goto free_and_end;
  1303. }
  1304. }
  1305. avctx->pts_correction_num_faulty_pts =
  1306. avctx->pts_correction_num_faulty_dts = 0;
  1307. avctx->pts_correction_last_pts =
  1308. avctx->pts_correction_last_dts = INT64_MIN;
  1309. if ( !CONFIG_GRAY && avctx->flags & AV_CODEC_FLAG_GRAY
  1310. && avctx->codec_descriptor->type == AVMEDIA_TYPE_VIDEO)
  1311. av_log(avctx, AV_LOG_WARNING,
  1312. "gray decoding requested but not enabled at configuration time\n");
  1313. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  1314. || avctx->internal->frame_thread_encoder)) {
  1315. ret = avctx->codec->init(avctx);
  1316. if (ret < 0) {
  1317. goto free_and_end;
  1318. }
  1319. }
  1320. ret=0;
  1321. #if FF_API_AUDIOENC_DELAY
  1322. if (av_codec_is_encoder(avctx->codec))
  1323. avctx->delay = avctx->initial_padding;
  1324. #endif
  1325. if (av_codec_is_decoder(avctx->codec)) {
  1326. if (!avctx->bit_rate)
  1327. avctx->bit_rate = get_bit_rate(avctx);
  1328. /* validate channel layout from the decoder */
  1329. if (avctx->channel_layout) {
  1330. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1331. if (!avctx->channels)
  1332. avctx->channels = channels;
  1333. else if (channels != avctx->channels) {
  1334. char buf[512];
  1335. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1336. av_log(avctx, AV_LOG_WARNING,
  1337. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  1338. "ignoring specified channel layout\n",
  1339. buf, channels, avctx->channels);
  1340. avctx->channel_layout = 0;
  1341. }
  1342. }
  1343. if (avctx->channels && avctx->channels < 0 ||
  1344. avctx->channels > FF_SANE_NB_CHANNELS) {
  1345. ret = AVERROR(EINVAL);
  1346. goto free_and_end;
  1347. }
  1348. if (avctx->sub_charenc) {
  1349. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  1350. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  1351. "supported with subtitles codecs\n");
  1352. ret = AVERROR(EINVAL);
  1353. goto free_and_end;
  1354. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  1355. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  1356. "subtitles character encoding will be ignored\n",
  1357. avctx->codec_descriptor->name);
  1358. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  1359. } else {
  1360. /* input character encoding is set for a text based subtitle
  1361. * codec at this point */
  1362. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  1363. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  1364. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  1365. #if CONFIG_ICONV
  1366. iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  1367. if (cd == (iconv_t)-1) {
  1368. ret = AVERROR(errno);
  1369. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  1370. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  1371. goto free_and_end;
  1372. }
  1373. iconv_close(cd);
  1374. #else
  1375. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  1376. "conversion needs a libavcodec built with iconv support "
  1377. "for this codec\n");
  1378. ret = AVERROR(ENOSYS);
  1379. goto free_and_end;
  1380. #endif
  1381. }
  1382. }
  1383. }
  1384. #if FF_API_AVCTX_TIMEBASE
  1385. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  1386. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  1387. #endif
  1388. }
  1389. if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
  1390. av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
  1391. }
  1392. end:
  1393. ff_unlock_avcodec(codec);
  1394. if (options) {
  1395. av_dict_free(options);
  1396. *options = tmp;
  1397. }
  1398. return ret;
  1399. free_and_end:
  1400. if (avctx->codec &&
  1401. (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
  1402. avctx->codec->close(avctx);
  1403. if (codec->priv_class && codec->priv_data_size)
  1404. av_opt_free(avctx->priv_data);
  1405. av_opt_free(avctx);
  1406. #if FF_API_CODED_FRAME
  1407. FF_DISABLE_DEPRECATION_WARNINGS
  1408. av_frame_free(&avctx->coded_frame);
  1409. FF_ENABLE_DEPRECATION_WARNINGS
  1410. #endif
  1411. av_dict_free(&tmp);
  1412. av_freep(&avctx->priv_data);
  1413. if (avctx->internal) {
  1414. av_frame_free(&avctx->internal->to_free);
  1415. av_freep(&avctx->internal->pool);
  1416. }
  1417. av_freep(&avctx->internal);
  1418. avctx->codec = NULL;
  1419. goto end;
  1420. }
  1421. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
  1422. {
  1423. if (avpkt->size < 0) {
  1424. av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
  1425. return AVERROR(EINVAL);
  1426. }
  1427. if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  1428. av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
  1429. size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
  1430. return AVERROR(EINVAL);
  1431. }
  1432. if (avctx && 2*min_size < size) { // FIXME The factor needs to be finetuned
  1433. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  1434. if (!avpkt->data || avpkt->size < size) {
  1435. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  1436. avpkt->data = avctx->internal->byte_buffer;
  1437. avpkt->size = avctx->internal->byte_buffer_size;
  1438. }
  1439. }
  1440. if (avpkt->data) {
  1441. AVBufferRef *buf = avpkt->buf;
  1442. if (avpkt->size < size) {
  1443. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
  1444. return AVERROR(EINVAL);
  1445. }
  1446. av_init_packet(avpkt);
  1447. avpkt->buf = buf;
  1448. avpkt->size = size;
  1449. return 0;
  1450. } else {
  1451. int ret = av_new_packet(avpkt, size);
  1452. if (ret < 0)
  1453. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
  1454. return ret;
  1455. }
  1456. }
  1457. int ff_alloc_packet(AVPacket *avpkt, int size)
  1458. {
  1459. return ff_alloc_packet2(NULL, avpkt, size, 0);
  1460. }
  1461. /**
  1462. * Pad last frame with silence.
  1463. */
  1464. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1465. {
  1466. AVFrame *frame = NULL;
  1467. int ret;
  1468. if (!(frame = av_frame_alloc()))
  1469. return AVERROR(ENOMEM);
  1470. frame->format = src->format;
  1471. frame->channel_layout = src->channel_layout;
  1472. av_frame_set_channels(frame, av_frame_get_channels(src));
  1473. frame->nb_samples = s->frame_size;
  1474. ret = av_frame_get_buffer(frame, 32);
  1475. if (ret < 0)
  1476. goto fail;
  1477. ret = av_frame_copy_props(frame, src);
  1478. if (ret < 0)
  1479. goto fail;
  1480. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1481. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1482. goto fail;
  1483. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1484. frame->nb_samples - src->nb_samples,
  1485. s->channels, s->sample_fmt)) < 0)
  1486. goto fail;
  1487. *dst = frame;
  1488. return 0;
  1489. fail:
  1490. av_frame_free(&frame);
  1491. return ret;
  1492. }
  1493. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1494. AVPacket *avpkt,
  1495. const AVFrame *frame,
  1496. int *got_packet_ptr)
  1497. {
  1498. AVFrame *extended_frame = NULL;
  1499. AVFrame *padded_frame = NULL;
  1500. int ret;
  1501. AVPacket user_pkt = *avpkt;
  1502. int needs_realloc = !user_pkt.data;
  1503. *got_packet_ptr = 0;
  1504. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  1505. av_packet_unref(avpkt);
  1506. av_init_packet(avpkt);
  1507. return 0;
  1508. }
  1509. /* ensure that extended_data is properly set */
  1510. if (frame && !frame->extended_data) {
  1511. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1512. avctx->channels > AV_NUM_DATA_POINTERS) {
  1513. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1514. "with more than %d channels, but extended_data is not set.\n",
  1515. AV_NUM_DATA_POINTERS);
  1516. return AVERROR(EINVAL);
  1517. }
  1518. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1519. extended_frame = av_frame_alloc();
  1520. if (!extended_frame)
  1521. return AVERROR(ENOMEM);
  1522. memcpy(extended_frame, frame, sizeof(AVFrame));
  1523. extended_frame->extended_data = extended_frame->data;
  1524. frame = extended_frame;
  1525. }
  1526. /* extract audio service type metadata */
  1527. if (frame) {
  1528. AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
  1529. if (sd && sd->size >= sizeof(enum AVAudioServiceType))
  1530. avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
  1531. }
  1532. /* check for valid frame size */
  1533. if (frame) {
  1534. if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
  1535. if (frame->nb_samples > avctx->frame_size) {
  1536. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1537. ret = AVERROR(EINVAL);
  1538. goto end;
  1539. }
  1540. } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1541. if (frame->nb_samples < avctx->frame_size &&
  1542. !avctx->internal->last_audio_frame) {
  1543. ret = pad_last_frame(avctx, &padded_frame, frame);
  1544. if (ret < 0)
  1545. goto end;
  1546. frame = padded_frame;
  1547. avctx->internal->last_audio_frame = 1;
  1548. }
  1549. if (frame->nb_samples != avctx->frame_size) {
  1550. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1551. ret = AVERROR(EINVAL);
  1552. goto end;
  1553. }
  1554. }
  1555. }
  1556. av_assert0(avctx->codec->encode2);
  1557. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1558. if (!ret) {
  1559. if (*got_packet_ptr) {
  1560. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
  1561. if (avpkt->pts == AV_NOPTS_VALUE)
  1562. avpkt->pts = frame->pts;
  1563. if (!avpkt->duration)
  1564. avpkt->duration = ff_samples_to_time_base(avctx,
  1565. frame->nb_samples);
  1566. }
  1567. avpkt->dts = avpkt->pts;
  1568. } else {
  1569. avpkt->size = 0;
  1570. }
  1571. }
  1572. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1573. needs_realloc = 0;
  1574. if (user_pkt.data) {
  1575. if (user_pkt.size >= avpkt->size) {
  1576. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1577. } else {
  1578. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1579. avpkt->size = user_pkt.size;
  1580. ret = -1;
  1581. }
  1582. avpkt->buf = user_pkt.buf;
  1583. avpkt->data = user_pkt.data;
  1584. } else {
  1585. if (av_dup_packet(avpkt) < 0) {
  1586. ret = AVERROR(ENOMEM);
  1587. }
  1588. }
  1589. }
  1590. if (!ret) {
  1591. if (needs_realloc && avpkt->data) {
  1592. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
  1593. if (ret >= 0)
  1594. avpkt->data = avpkt->buf->data;
  1595. }
  1596. avctx->frame_number++;
  1597. }
  1598. if (ret < 0 || !*got_packet_ptr) {
  1599. av_packet_unref(avpkt);
  1600. av_init_packet(avpkt);
  1601. goto end;
  1602. }
  1603. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1604. * this needs to be moved to the encoders, but for now we can do it
  1605. * here to simplify things */
  1606. avpkt->flags |= AV_PKT_FLAG_KEY;
  1607. end:
  1608. av_frame_free(&padded_frame);
  1609. av_free(extended_frame);
  1610. #if FF_API_AUDIOENC_DELAY
  1611. avctx->delay = avctx->initial_padding;
  1612. #endif
  1613. return ret;
  1614. }
  1615. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1616. AVPacket *avpkt,
  1617. const AVFrame *frame,
  1618. int *got_packet_ptr)
  1619. {
  1620. int ret;
  1621. AVPacket user_pkt = *avpkt;
  1622. int needs_realloc = !user_pkt.data;
  1623. *got_packet_ptr = 0;
  1624. if(CONFIG_FRAME_THREAD_ENCODER &&
  1625. avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1626. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1627. if ((avctx->flags&AV_CODEC_FLAG_PASS1) && avctx->stats_out)
  1628. avctx->stats_out[0] = '\0';
  1629. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  1630. av_packet_unref(avpkt);
  1631. av_init_packet(avpkt);
  1632. avpkt->size = 0;
  1633. return 0;
  1634. }
  1635. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1636. return AVERROR(EINVAL);
  1637. if (frame && frame->format == AV_PIX_FMT_NONE)
  1638. av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
  1639. if (frame && (frame->width == 0 || frame->height == 0))
  1640. av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
  1641. av_assert0(avctx->codec->encode2);
  1642. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1643. av_assert0(ret <= 0);
  1644. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1645. needs_realloc = 0;
  1646. if (user_pkt.data) {
  1647. if (user_pkt.size >= avpkt->size) {
  1648. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1649. } else {
  1650. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1651. avpkt->size = user_pkt.size;
  1652. ret = -1;
  1653. }
  1654. avpkt->buf = user_pkt.buf;
  1655. avpkt->data = user_pkt.data;
  1656. } else {
  1657. if (av_dup_packet(avpkt) < 0) {
  1658. ret = AVERROR(ENOMEM);
  1659. }
  1660. }
  1661. }
  1662. if (!ret) {
  1663. if (!*got_packet_ptr)
  1664. avpkt->size = 0;
  1665. else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  1666. avpkt->pts = avpkt->dts = frame->pts;
  1667. if (needs_realloc && avpkt->data) {
  1668. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
  1669. if (ret >= 0)
  1670. avpkt->data = avpkt->buf->data;
  1671. }
  1672. avctx->frame_number++;
  1673. }
  1674. if (ret < 0 || !*got_packet_ptr)
  1675. av_packet_unref(avpkt);
  1676. emms_c();
  1677. return ret;
  1678. }
  1679. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1680. const AVSubtitle *sub)
  1681. {
  1682. int ret;
  1683. if (sub->start_display_time) {
  1684. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1685. return -1;
  1686. }
  1687. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1688. avctx->frame_number++;
  1689. return ret;
  1690. }
  1691. /**
  1692. * Attempt to guess proper monotonic timestamps for decoded video frames
  1693. * which might have incorrect times. Input timestamps may wrap around, in
  1694. * which case the output will as well.
  1695. *
  1696. * @param pts the pts field of the decoded AVPacket, as passed through
  1697. * AVFrame.pkt_pts
  1698. * @param dts the dts field of the decoded AVPacket
  1699. * @return one of the input values, may be AV_NOPTS_VALUE
  1700. */
  1701. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1702. int64_t reordered_pts, int64_t dts)
  1703. {
  1704. int64_t pts = AV_NOPTS_VALUE;
  1705. if (dts != AV_NOPTS_VALUE) {
  1706. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1707. ctx->pts_correction_last_dts = dts;
  1708. } else if (reordered_pts != AV_NOPTS_VALUE)
  1709. ctx->pts_correction_last_dts = reordered_pts;
  1710. if (reordered_pts != AV_NOPTS_VALUE) {
  1711. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1712. ctx->pts_correction_last_pts = reordered_pts;
  1713. } else if(dts != AV_NOPTS_VALUE)
  1714. ctx->pts_correction_last_pts = dts;
  1715. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1716. && reordered_pts != AV_NOPTS_VALUE)
  1717. pts = reordered_pts;
  1718. else
  1719. pts = dts;
  1720. return pts;
  1721. }
  1722. static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1723. {
  1724. int size = 0, ret;
  1725. const uint8_t *data;
  1726. uint32_t flags;
  1727. int64_t val;
  1728. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1729. if (!data)
  1730. return 0;
  1731. if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
  1732. av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
  1733. "changes, but PARAM_CHANGE side data was sent to it.\n");
  1734. return AVERROR(EINVAL);
  1735. }
  1736. if (size < 4)
  1737. goto fail;
  1738. flags = bytestream_get_le32(&data);
  1739. size -= 4;
  1740. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1741. if (size < 4)
  1742. goto fail;
  1743. val = bytestream_get_le32(&data);
  1744. if (val <= 0 || val > INT_MAX) {
  1745. av_log(avctx, AV_LOG_ERROR, "Invalid channel count");
  1746. return AVERROR_INVALIDDATA;
  1747. }
  1748. avctx->channels = val;
  1749. size -= 4;
  1750. }
  1751. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1752. if (size < 8)
  1753. goto fail;
  1754. avctx->channel_layout = bytestream_get_le64(&data);
  1755. size -= 8;
  1756. }
  1757. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1758. if (size < 4)
  1759. goto fail;
  1760. val = bytestream_get_le32(&data);
  1761. if (val <= 0 || val > INT_MAX) {
  1762. av_log(avctx, AV_LOG_ERROR, "Invalid sample rate");
  1763. return AVERROR_INVALIDDATA;
  1764. }
  1765. avctx->sample_rate = val;
  1766. size -= 4;
  1767. }
  1768. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1769. if (size < 8)
  1770. goto fail;
  1771. avctx->width = bytestream_get_le32(&data);
  1772. avctx->height = bytestream_get_le32(&data);
  1773. size -= 8;
  1774. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1775. if (ret < 0)
  1776. return ret;
  1777. }
  1778. return 0;
  1779. fail:
  1780. av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
  1781. return AVERROR_INVALIDDATA;
  1782. }
  1783. static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
  1784. {
  1785. int ret;
  1786. /* move the original frame to our backup */
  1787. av_frame_unref(avci->to_free);
  1788. av_frame_move_ref(avci->to_free, frame);
  1789. /* now copy everything except the AVBufferRefs back
  1790. * note that we make a COPY of the side data, so calling av_frame_free() on
  1791. * the caller's frame will work properly */
  1792. ret = av_frame_copy_props(frame, avci->to_free);
  1793. if (ret < 0)
  1794. return ret;
  1795. memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
  1796. memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
  1797. if (avci->to_free->extended_data != avci->to_free->data) {
  1798. int planes = av_frame_get_channels(avci->to_free);
  1799. int size = planes * sizeof(*frame->extended_data);
  1800. if (!size) {
  1801. av_frame_unref(frame);
  1802. return AVERROR_BUG;
  1803. }
  1804. frame->extended_data = av_malloc(size);
  1805. if (!frame->extended_data) {
  1806. av_frame_unref(frame);
  1807. return AVERROR(ENOMEM);
  1808. }
  1809. memcpy(frame->extended_data, avci->to_free->extended_data,
  1810. size);
  1811. } else
  1812. frame->extended_data = frame->data;
  1813. frame->format = avci->to_free->format;
  1814. frame->width = avci->to_free->width;
  1815. frame->height = avci->to_free->height;
  1816. frame->channel_layout = avci->to_free->channel_layout;
  1817. frame->nb_samples = avci->to_free->nb_samples;
  1818. av_frame_set_channels(frame, av_frame_get_channels(avci->to_free));
  1819. return 0;
  1820. }
  1821. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1822. int *got_picture_ptr,
  1823. const AVPacket *avpkt)
  1824. {
  1825. AVCodecInternal *avci = avctx->internal;
  1826. int ret;
  1827. // copy to ensure we do not change avpkt
  1828. AVPacket tmp = *avpkt;
  1829. if (!avctx->codec)
  1830. return AVERROR(EINVAL);
  1831. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1832. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1833. return AVERROR(EINVAL);
  1834. }
  1835. *got_picture_ptr = 0;
  1836. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1837. return AVERROR(EINVAL);
  1838. av_frame_unref(picture);
  1839. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size ||
  1840. (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1841. int did_split = av_packet_split_side_data(&tmp);
  1842. ret = apply_param_change(avctx, &tmp);
  1843. if (ret < 0) {
  1844. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1845. if (avctx->err_recognition & AV_EF_EXPLODE)
  1846. goto fail;
  1847. }
  1848. avctx->internal->pkt = &tmp;
  1849. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1850. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1851. &tmp);
  1852. else {
  1853. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1854. &tmp);
  1855. if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
  1856. picture->pkt_dts = avpkt->dts;
  1857. if(!avctx->has_b_frames){
  1858. av_frame_set_pkt_pos(picture, avpkt->pos);
  1859. }
  1860. //FIXME these should be under if(!avctx->has_b_frames)
  1861. /* get_buffer is supposed to set frame parameters */
  1862. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
  1863. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1864. if (!picture->width) picture->width = avctx->width;
  1865. if (!picture->height) picture->height = avctx->height;
  1866. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  1867. }
  1868. }
  1869. fail:
  1870. emms_c(); //needed to avoid an emms_c() call before every return;
  1871. avctx->internal->pkt = NULL;
  1872. if (did_split) {
  1873. av_packet_free_side_data(&tmp);
  1874. if(ret == tmp.size)
  1875. ret = avpkt->size;
  1876. }
  1877. if (*got_picture_ptr) {
  1878. if (!avctx->refcounted_frames) {
  1879. int err = unrefcount_frame(avci, picture);
  1880. if (err < 0)
  1881. return err;
  1882. }
  1883. avctx->frame_number++;
  1884. av_frame_set_best_effort_timestamp(picture,
  1885. guess_correct_pts(avctx,
  1886. picture->pkt_pts,
  1887. picture->pkt_dts));
  1888. } else
  1889. av_frame_unref(picture);
  1890. } else
  1891. ret = 0;
  1892. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1893. * make sure it's set correctly */
  1894. av_assert0(!picture->extended_data || picture->extended_data == picture->data);
  1895. #if FF_API_AVCTX_TIMEBASE
  1896. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  1897. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  1898. #endif
  1899. return ret;
  1900. }
  1901. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1902. AVFrame *frame,
  1903. int *got_frame_ptr,
  1904. const AVPacket *avpkt)
  1905. {
  1906. AVCodecInternal *avci = avctx->internal;
  1907. int ret = 0;
  1908. *got_frame_ptr = 0;
  1909. if (!avpkt->data && avpkt->size) {
  1910. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1911. return AVERROR(EINVAL);
  1912. }
  1913. if (!avctx->codec)
  1914. return AVERROR(EINVAL);
  1915. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1916. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1917. return AVERROR(EINVAL);
  1918. }
  1919. av_frame_unref(frame);
  1920. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1921. uint8_t *side;
  1922. int side_size;
  1923. uint32_t discard_padding = 0;
  1924. uint8_t skip_reason = 0;
  1925. uint8_t discard_reason = 0;
  1926. // copy to ensure we do not change avpkt
  1927. AVPacket tmp = *avpkt;
  1928. int did_split = av_packet_split_side_data(&tmp);
  1929. ret = apply_param_change(avctx, &tmp);
  1930. if (ret < 0) {
  1931. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1932. if (avctx->err_recognition & AV_EF_EXPLODE)
  1933. goto fail;
  1934. }
  1935. avctx->internal->pkt = &tmp;
  1936. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1937. ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
  1938. else {
  1939. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1940. av_assert0(ret <= tmp.size);
  1941. frame->pkt_dts = avpkt->dts;
  1942. }
  1943. if (ret >= 0 && *got_frame_ptr) {
  1944. avctx->frame_number++;
  1945. av_frame_set_best_effort_timestamp(frame,
  1946. guess_correct_pts(avctx,
  1947. frame->pkt_pts,
  1948. frame->pkt_dts));
  1949. if (frame->format == AV_SAMPLE_FMT_NONE)
  1950. frame->format = avctx->sample_fmt;
  1951. if (!frame->channel_layout)
  1952. frame->channel_layout = avctx->channel_layout;
  1953. if (!av_frame_get_channels(frame))
  1954. av_frame_set_channels(frame, avctx->channels);
  1955. if (!frame->sample_rate)
  1956. frame->sample_rate = avctx->sample_rate;
  1957. }
  1958. side= av_packet_get_side_data(avctx->internal->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  1959. if(side && side_size>=10) {
  1960. avctx->internal->skip_samples = AV_RL32(side);
  1961. discard_padding = AV_RL32(side + 4);
  1962. av_log(avctx, AV_LOG_DEBUG, "skip %d / discard %d samples due to side data\n",
  1963. avctx->internal->skip_samples, (int)discard_padding);
  1964. skip_reason = AV_RL8(side + 8);
  1965. discard_reason = AV_RL8(side + 9);
  1966. }
  1967. if (avctx->internal->skip_samples && *got_frame_ptr &&
  1968. !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
  1969. if(frame->nb_samples <= avctx->internal->skip_samples){
  1970. *got_frame_ptr = 0;
  1971. avctx->internal->skip_samples -= frame->nb_samples;
  1972. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  1973. avctx->internal->skip_samples);
  1974. } else {
  1975. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  1976. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  1977. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1978. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  1979. (AVRational){1, avctx->sample_rate},
  1980. avctx->pkt_timebase);
  1981. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  1982. frame->pkt_pts += diff_ts;
  1983. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  1984. frame->pkt_dts += diff_ts;
  1985. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  1986. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  1987. } else {
  1988. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  1989. }
  1990. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  1991. avctx->internal->skip_samples, frame->nb_samples);
  1992. frame->nb_samples -= avctx->internal->skip_samples;
  1993. avctx->internal->skip_samples = 0;
  1994. }
  1995. }
  1996. if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr &&
  1997. !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
  1998. if (discard_padding == frame->nb_samples) {
  1999. *got_frame_ptr = 0;
  2000. } else {
  2001. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  2002. int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
  2003. (AVRational){1, avctx->sample_rate},
  2004. avctx->pkt_timebase);
  2005. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  2006. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  2007. } else {
  2008. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
  2009. }
  2010. av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
  2011. (int)discard_padding, frame->nb_samples);
  2012. frame->nb_samples -= discard_padding;
  2013. }
  2014. }
  2015. if ((avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL) && *got_frame_ptr) {
  2016. AVFrameSideData *fside = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
  2017. if (fside) {
  2018. AV_WL32(fside->data, avctx->internal->skip_samples);
  2019. AV_WL32(fside->data + 4, discard_padding);
  2020. AV_WL8(fside->data + 8, skip_reason);
  2021. AV_WL8(fside->data + 9, discard_reason);
  2022. avctx->internal->skip_samples = 0;
  2023. }
  2024. }
  2025. fail:
  2026. avctx->internal->pkt = NULL;
  2027. if (did_split) {
  2028. av_packet_free_side_data(&tmp);
  2029. if(ret == tmp.size)
  2030. ret = avpkt->size;
  2031. }
  2032. if (ret >= 0 && *got_frame_ptr) {
  2033. if (!avctx->refcounted_frames) {
  2034. int err = unrefcount_frame(avci, frame);
  2035. if (err < 0)
  2036. return err;
  2037. }
  2038. } else
  2039. av_frame_unref(frame);
  2040. }
  2041. return ret;
  2042. }
  2043. #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
  2044. static int recode_subtitle(AVCodecContext *avctx,
  2045. AVPacket *outpkt, const AVPacket *inpkt)
  2046. {
  2047. #if CONFIG_ICONV
  2048. iconv_t cd = (iconv_t)-1;
  2049. int ret = 0;
  2050. char *inb, *outb;
  2051. size_t inl, outl;
  2052. AVPacket tmp;
  2053. #endif
  2054. if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
  2055. return 0;
  2056. #if CONFIG_ICONV
  2057. cd = iconv_open("UTF-8", avctx->sub_charenc);
  2058. av_assert0(cd != (iconv_t)-1);
  2059. inb = inpkt->data;
  2060. inl = inpkt->size;
  2061. if (inl >= INT_MAX / UTF8_MAX_BYTES - AV_INPUT_BUFFER_PADDING_SIZE) {
  2062. av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
  2063. ret = AVERROR(ENOMEM);
  2064. goto end;
  2065. }
  2066. ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
  2067. if (ret < 0)
  2068. goto end;
  2069. outpkt->buf = tmp.buf;
  2070. outpkt->data = tmp.data;
  2071. outpkt->size = tmp.size;
  2072. outb = outpkt->data;
  2073. outl = outpkt->size;
  2074. if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
  2075. iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
  2076. outl >= outpkt->size || inl != 0) {
  2077. ret = FFMIN(AVERROR(errno), -1);
  2078. av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
  2079. "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
  2080. av_packet_unref(&tmp);
  2081. goto end;
  2082. }
  2083. outpkt->size -= outl;
  2084. memset(outpkt->data + outpkt->size, 0, outl);
  2085. end:
  2086. if (cd != (iconv_t)-1)
  2087. iconv_close(cd);
  2088. return ret;
  2089. #else
  2090. av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
  2091. return AVERROR(EINVAL);
  2092. #endif
  2093. }
  2094. static int utf8_check(const uint8_t *str)
  2095. {
  2096. const uint8_t *byte;
  2097. uint32_t codepoint, min;
  2098. while (*str) {
  2099. byte = str;
  2100. GET_UTF8(codepoint, *(byte++), return 0;);
  2101. min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
  2102. 1 << (5 * (byte - str) - 4);
  2103. if (codepoint < min || codepoint >= 0x110000 ||
  2104. codepoint == 0xFFFE /* BOM */ ||
  2105. codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
  2106. return 0;
  2107. str = byte;
  2108. }
  2109. return 1;
  2110. }
  2111. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  2112. int *got_sub_ptr,
  2113. AVPacket *avpkt)
  2114. {
  2115. int i, ret = 0;
  2116. if (!avpkt->data && avpkt->size) {
  2117. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  2118. return AVERROR(EINVAL);
  2119. }
  2120. if (!avctx->codec)
  2121. return AVERROR(EINVAL);
  2122. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  2123. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  2124. return AVERROR(EINVAL);
  2125. }
  2126. *got_sub_ptr = 0;
  2127. get_subtitle_defaults(sub);
  2128. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
  2129. AVPacket pkt_recoded;
  2130. AVPacket tmp = *avpkt;
  2131. int did_split = av_packet_split_side_data(&tmp);
  2132. //apply_param_change(avctx, &tmp);
  2133. if (did_split) {
  2134. /* FFMIN() prevents overflow in case the packet wasn't allocated with
  2135. * proper padding.
  2136. * If the side data is smaller than the buffer padding size, the
  2137. * remaining bytes should have already been filled with zeros by the
  2138. * original packet allocation anyway. */
  2139. memset(tmp.data + tmp.size, 0,
  2140. FFMIN(avpkt->size - tmp.size, AV_INPUT_BUFFER_PADDING_SIZE));
  2141. }
  2142. pkt_recoded = tmp;
  2143. ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
  2144. if (ret < 0) {
  2145. *got_sub_ptr = 0;
  2146. } else {
  2147. avctx->internal->pkt = &pkt_recoded;
  2148. if (avctx->pkt_timebase.num && avpkt->pts != AV_NOPTS_VALUE)
  2149. sub->pts = av_rescale_q(avpkt->pts,
  2150. avctx->pkt_timebase, AV_TIME_BASE_Q);
  2151. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
  2152. av_assert1((ret >= 0) >= !!*got_sub_ptr &&
  2153. !!*got_sub_ptr >= !!sub->num_rects);
  2154. if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
  2155. avctx->pkt_timebase.num) {
  2156. AVRational ms = { 1, 1000 };
  2157. sub->end_display_time = av_rescale_q(avpkt->duration,
  2158. avctx->pkt_timebase, ms);
  2159. }
  2160. for (i = 0; i < sub->num_rects; i++) {
  2161. if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
  2162. av_log(avctx, AV_LOG_ERROR,
  2163. "Invalid UTF-8 in decoded subtitles text; "
  2164. "maybe missing -sub_charenc option\n");
  2165. avsubtitle_free(sub);
  2166. return AVERROR_INVALIDDATA;
  2167. }
  2168. }
  2169. if (tmp.data != pkt_recoded.data) { // did we recode?
  2170. /* prevent from destroying side data from original packet */
  2171. pkt_recoded.side_data = NULL;
  2172. pkt_recoded.side_data_elems = 0;
  2173. av_packet_unref(&pkt_recoded);
  2174. }
  2175. if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
  2176. sub->format = 0;
  2177. else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
  2178. sub->format = 1;
  2179. avctx->internal->pkt = NULL;
  2180. }
  2181. if (did_split) {
  2182. av_packet_free_side_data(&tmp);
  2183. if(ret == tmp.size)
  2184. ret = avpkt->size;
  2185. }
  2186. if (*got_sub_ptr)
  2187. avctx->frame_number++;
  2188. }
  2189. return ret;
  2190. }
  2191. void avsubtitle_free(AVSubtitle *sub)
  2192. {
  2193. int i;
  2194. for (i = 0; i < sub->num_rects; i++) {
  2195. av_freep(&sub->rects[i]->data[0]);
  2196. av_freep(&sub->rects[i]->data[1]);
  2197. av_freep(&sub->rects[i]->data[2]);
  2198. av_freep(&sub->rects[i]->data[3]);
  2199. av_freep(&sub->rects[i]->text);
  2200. av_freep(&sub->rects[i]->ass);
  2201. av_freep(&sub->rects[i]);
  2202. }
  2203. av_freep(&sub->rects);
  2204. memset(sub, 0, sizeof(AVSubtitle));
  2205. }
  2206. av_cold int avcodec_close(AVCodecContext *avctx)
  2207. {
  2208. int i;
  2209. if (!avctx)
  2210. return 0;
  2211. if (avcodec_is_open(avctx)) {
  2212. FramePool *pool = avctx->internal->pool;
  2213. if (CONFIG_FRAME_THREAD_ENCODER &&
  2214. avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  2215. ff_frame_thread_encoder_free(avctx);
  2216. }
  2217. if (HAVE_THREADS && avctx->internal->thread_ctx)
  2218. ff_thread_free(avctx);
  2219. if (avctx->codec && avctx->codec->close)
  2220. avctx->codec->close(avctx);
  2221. avctx->internal->byte_buffer_size = 0;
  2222. av_freep(&avctx->internal->byte_buffer);
  2223. av_frame_free(&avctx->internal->to_free);
  2224. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  2225. av_buffer_pool_uninit(&pool->pools[i]);
  2226. av_freep(&avctx->internal->pool);
  2227. if (avctx->hwaccel && avctx->hwaccel->uninit)
  2228. avctx->hwaccel->uninit(avctx);
  2229. av_freep(&avctx->internal->hwaccel_priv_data);
  2230. av_freep(&avctx->internal);
  2231. }
  2232. for (i = 0; i < avctx->nb_coded_side_data; i++)
  2233. av_freep(&avctx->coded_side_data[i].data);
  2234. av_freep(&avctx->coded_side_data);
  2235. avctx->nb_coded_side_data = 0;
  2236. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  2237. av_opt_free(avctx->priv_data);
  2238. av_opt_free(avctx);
  2239. av_freep(&avctx->priv_data);
  2240. if (av_codec_is_encoder(avctx->codec)) {
  2241. av_freep(&avctx->extradata);
  2242. #if FF_API_CODED_FRAME
  2243. FF_DISABLE_DEPRECATION_WARNINGS
  2244. av_frame_free(&avctx->coded_frame);
  2245. FF_ENABLE_DEPRECATION_WARNINGS
  2246. #endif
  2247. }
  2248. avctx->codec = NULL;
  2249. avctx->active_thread_type = 0;
  2250. return 0;
  2251. }
  2252. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  2253. {
  2254. switch(id){
  2255. //This is for future deprecatec codec ids, its empty since
  2256. //last major bump but will fill up again over time, please don't remove it
  2257. default : return id;
  2258. }
  2259. }
  2260. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  2261. {
  2262. AVCodec *p, *experimental = NULL;
  2263. p = first_avcodec;
  2264. id= remap_deprecated_codec_id(id);
  2265. while (p) {
  2266. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  2267. p->id == id) {
  2268. if (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL && !experimental) {
  2269. experimental = p;
  2270. } else
  2271. return p;
  2272. }
  2273. p = p->next;
  2274. }
  2275. return experimental;
  2276. }
  2277. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  2278. {
  2279. return find_encdec(id, 1);
  2280. }
  2281. AVCodec *avcodec_find_encoder_by_name(const char *name)
  2282. {
  2283. AVCodec *p;
  2284. if (!name)
  2285. return NULL;
  2286. p = first_avcodec;
  2287. while (p) {
  2288. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  2289. return p;
  2290. p = p->next;
  2291. }
  2292. return NULL;
  2293. }
  2294. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  2295. {
  2296. return find_encdec(id, 0);
  2297. }
  2298. AVCodec *avcodec_find_decoder_by_name(const char *name)
  2299. {
  2300. AVCodec *p;
  2301. if (!name)
  2302. return NULL;
  2303. p = first_avcodec;
  2304. while (p) {
  2305. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  2306. return p;
  2307. p = p->next;
  2308. }
  2309. return NULL;
  2310. }
  2311. const char *avcodec_get_name(enum AVCodecID id)
  2312. {
  2313. const AVCodecDescriptor *cd;
  2314. AVCodec *codec;
  2315. if (id == AV_CODEC_ID_NONE)
  2316. return "none";
  2317. cd = avcodec_descriptor_get(id);
  2318. if (cd)
  2319. return cd->name;
  2320. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  2321. codec = avcodec_find_decoder(id);
  2322. if (codec)
  2323. return codec->name;
  2324. codec = avcodec_find_encoder(id);
  2325. if (codec)
  2326. return codec->name;
  2327. return "unknown_codec";
  2328. }
  2329. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  2330. {
  2331. int i, len, ret = 0;
  2332. #define TAG_PRINT(x) \
  2333. (((x) >= '0' && (x) <= '9') || \
  2334. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  2335. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  2336. for (i = 0; i < 4; i++) {
  2337. len = snprintf(buf, buf_size,
  2338. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  2339. buf += len;
  2340. buf_size = buf_size > len ? buf_size - len : 0;
  2341. ret += len;
  2342. codec_tag >>= 8;
  2343. }
  2344. return ret;
  2345. }
  2346. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  2347. {
  2348. const char *codec_type;
  2349. const char *codec_name;
  2350. const char *profile = NULL;
  2351. int64_t bitrate;
  2352. int new_line = 0;
  2353. AVRational display_aspect_ratio;
  2354. const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
  2355. if (!buf || buf_size <= 0)
  2356. return;
  2357. codec_type = av_get_media_type_string(enc->codec_type);
  2358. codec_name = avcodec_get_name(enc->codec_id);
  2359. profile = avcodec_profile_name(enc->codec_id, enc->profile);
  2360. snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
  2361. codec_name);
  2362. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  2363. if (enc->codec && strcmp(enc->codec->name, codec_name))
  2364. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
  2365. if (profile)
  2366. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  2367. if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
  2368. && av_log_get_level() >= AV_LOG_VERBOSE
  2369. && enc->refs)
  2370. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2371. ", %d reference frame%s",
  2372. enc->refs, enc->refs > 1 ? "s" : "");
  2373. if (enc->codec_tag) {
  2374. char tag_buf[32];
  2375. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  2376. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2377. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  2378. }
  2379. switch (enc->codec_type) {
  2380. case AVMEDIA_TYPE_VIDEO:
  2381. {
  2382. char detail[256] = "(";
  2383. av_strlcat(buf, separator, buf_size);
  2384. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2385. "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
  2386. av_get_pix_fmt_name(enc->pix_fmt));
  2387. if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
  2388. enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
  2389. av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
  2390. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  2391. av_strlcatf(detail, sizeof(detail), "%s, ",
  2392. av_color_range_name(enc->color_range));
  2393. if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
  2394. enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  2395. enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
  2396. if (enc->colorspace != (int)enc->color_primaries ||
  2397. enc->colorspace != (int)enc->color_trc) {
  2398. new_line = 1;
  2399. av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
  2400. av_color_space_name(enc->colorspace),
  2401. av_color_primaries_name(enc->color_primaries),
  2402. av_color_transfer_name(enc->color_trc));
  2403. } else
  2404. av_strlcatf(detail, sizeof(detail), "%s, ",
  2405. av_get_colorspace_name(enc->colorspace));
  2406. }
  2407. if (av_log_get_level() >= AV_LOG_DEBUG &&
  2408. enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
  2409. av_strlcatf(detail, sizeof(detail), "%s, ",
  2410. av_chroma_location_name(enc->chroma_sample_location));
  2411. if (strlen(detail) > 1) {
  2412. detail[strlen(detail) - 2] = 0;
  2413. av_strlcatf(buf, buf_size, "%s)", detail);
  2414. }
  2415. }
  2416. if (enc->width) {
  2417. av_strlcat(buf, new_line ? separator : ", ", buf_size);
  2418. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2419. "%dx%d",
  2420. enc->width, enc->height);
  2421. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  2422. (enc->width != enc->coded_width ||
  2423. enc->height != enc->coded_height))
  2424. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2425. " (%dx%d)", enc->coded_width, enc->coded_height);
  2426. if (enc->sample_aspect_ratio.num) {
  2427. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  2428. enc->width * (int64_t)enc->sample_aspect_ratio.num,
  2429. enc->height * (int64_t)enc->sample_aspect_ratio.den,
  2430. 1024 * 1024);
  2431. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2432. " [SAR %d:%d DAR %d:%d]",
  2433. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  2434. display_aspect_ratio.num, display_aspect_ratio.den);
  2435. }
  2436. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2437. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2438. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2439. ", %d/%d",
  2440. enc->time_base.num / g, enc->time_base.den / g);
  2441. }
  2442. }
  2443. if (encode) {
  2444. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2445. ", q=%d-%d", enc->qmin, enc->qmax);
  2446. } else {
  2447. if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
  2448. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2449. ", Closed Captions");
  2450. if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
  2451. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2452. ", lossless");
  2453. }
  2454. break;
  2455. case AVMEDIA_TYPE_AUDIO:
  2456. av_strlcat(buf, separator, buf_size);
  2457. if (enc->sample_rate) {
  2458. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2459. "%d Hz, ", enc->sample_rate);
  2460. }
  2461. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  2462. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  2463. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2464. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  2465. }
  2466. if ( enc->bits_per_raw_sample > 0
  2467. && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
  2468. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2469. " (%d bit)", enc->bits_per_raw_sample);
  2470. break;
  2471. case AVMEDIA_TYPE_DATA:
  2472. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2473. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2474. if (g)
  2475. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2476. ", %d/%d",
  2477. enc->time_base.num / g, enc->time_base.den / g);
  2478. }
  2479. break;
  2480. case AVMEDIA_TYPE_SUBTITLE:
  2481. if (enc->width)
  2482. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2483. ", %dx%d", enc->width, enc->height);
  2484. break;
  2485. default:
  2486. return;
  2487. }
  2488. if (encode) {
  2489. if (enc->flags & AV_CODEC_FLAG_PASS1)
  2490. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2491. ", pass 1");
  2492. if (enc->flags & AV_CODEC_FLAG_PASS2)
  2493. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2494. ", pass 2");
  2495. }
  2496. bitrate = get_bit_rate(enc);
  2497. if (bitrate != 0) {
  2498. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2499. ", %"PRId64" kb/s", bitrate / 1000);
  2500. } else if (enc->rc_max_rate > 0) {
  2501. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2502. ", max. %"PRId64" kb/s", (int64_t)enc->rc_max_rate / 1000);
  2503. }
  2504. }
  2505. const char *av_get_profile_name(const AVCodec *codec, int profile)
  2506. {
  2507. const AVProfile *p;
  2508. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  2509. return NULL;
  2510. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  2511. if (p->profile == profile)
  2512. return p->name;
  2513. return NULL;
  2514. }
  2515. const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
  2516. {
  2517. const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
  2518. const AVProfile *p;
  2519. if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
  2520. return NULL;
  2521. for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  2522. if (p->profile == profile)
  2523. return p->name;
  2524. return NULL;
  2525. }
  2526. unsigned avcodec_version(void)
  2527. {
  2528. // av_assert0(AV_CODEC_ID_V410==164);
  2529. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  2530. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  2531. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  2532. av_assert0(AV_CODEC_ID_SRT==94216);
  2533. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  2534. return LIBAVCODEC_VERSION_INT;
  2535. }
  2536. const char *avcodec_configuration(void)
  2537. {
  2538. return FFMPEG_CONFIGURATION;
  2539. }
  2540. const char *avcodec_license(void)
  2541. {
  2542. #define LICENSE_PREFIX "libavcodec license: "
  2543. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  2544. }
  2545. void avcodec_flush_buffers(AVCodecContext *avctx)
  2546. {
  2547. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2548. ff_thread_flush(avctx);
  2549. else if (avctx->codec->flush)
  2550. avctx->codec->flush(avctx);
  2551. avctx->pts_correction_last_pts =
  2552. avctx->pts_correction_last_dts = INT64_MIN;
  2553. if (!avctx->refcounted_frames)
  2554. av_frame_unref(avctx->internal->to_free);
  2555. }
  2556. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  2557. {
  2558. switch (codec_id) {
  2559. case AV_CODEC_ID_8SVX_EXP:
  2560. case AV_CODEC_ID_8SVX_FIB:
  2561. case AV_CODEC_ID_ADPCM_CT:
  2562. case AV_CODEC_ID_ADPCM_IMA_APC:
  2563. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  2564. case AV_CODEC_ID_ADPCM_IMA_OKI:
  2565. case AV_CODEC_ID_ADPCM_IMA_WS:
  2566. case AV_CODEC_ID_ADPCM_G722:
  2567. case AV_CODEC_ID_ADPCM_YAMAHA:
  2568. case AV_CODEC_ID_ADPCM_AICA:
  2569. return 4;
  2570. case AV_CODEC_ID_DSD_LSBF:
  2571. case AV_CODEC_ID_DSD_MSBF:
  2572. case AV_CODEC_ID_DSD_LSBF_PLANAR:
  2573. case AV_CODEC_ID_DSD_MSBF_PLANAR:
  2574. case AV_CODEC_ID_PCM_ALAW:
  2575. case AV_CODEC_ID_PCM_MULAW:
  2576. case AV_CODEC_ID_PCM_S8:
  2577. case AV_CODEC_ID_PCM_S8_PLANAR:
  2578. case AV_CODEC_ID_PCM_U8:
  2579. case AV_CODEC_ID_PCM_ZORK:
  2580. case AV_CODEC_ID_SDX2_DPCM:
  2581. return 8;
  2582. case AV_CODEC_ID_PCM_S16BE:
  2583. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  2584. case AV_CODEC_ID_PCM_S16LE:
  2585. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  2586. case AV_CODEC_ID_PCM_U16BE:
  2587. case AV_CODEC_ID_PCM_U16LE:
  2588. return 16;
  2589. case AV_CODEC_ID_PCM_S24DAUD:
  2590. case AV_CODEC_ID_PCM_S24BE:
  2591. case AV_CODEC_ID_PCM_S24LE:
  2592. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  2593. case AV_CODEC_ID_PCM_U24BE:
  2594. case AV_CODEC_ID_PCM_U24LE:
  2595. return 24;
  2596. case AV_CODEC_ID_PCM_S32BE:
  2597. case AV_CODEC_ID_PCM_S32LE:
  2598. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  2599. case AV_CODEC_ID_PCM_U32BE:
  2600. case AV_CODEC_ID_PCM_U32LE:
  2601. case AV_CODEC_ID_PCM_F32BE:
  2602. case AV_CODEC_ID_PCM_F32LE:
  2603. return 32;
  2604. case AV_CODEC_ID_PCM_F64BE:
  2605. case AV_CODEC_ID_PCM_F64LE:
  2606. return 64;
  2607. default:
  2608. return 0;
  2609. }
  2610. }
  2611. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  2612. {
  2613. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  2614. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2615. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2616. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2617. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2618. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2619. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2620. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2621. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2622. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2623. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2624. };
  2625. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  2626. return AV_CODEC_ID_NONE;
  2627. if (be < 0 || be > 1)
  2628. be = AV_NE(1, 0);
  2629. return map[fmt][be];
  2630. }
  2631. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2632. {
  2633. switch (codec_id) {
  2634. case AV_CODEC_ID_ADPCM_SBPRO_2:
  2635. return 2;
  2636. case AV_CODEC_ID_ADPCM_SBPRO_3:
  2637. return 3;
  2638. case AV_CODEC_ID_ADPCM_SBPRO_4:
  2639. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2640. case AV_CODEC_ID_ADPCM_IMA_QT:
  2641. case AV_CODEC_ID_ADPCM_SWF:
  2642. case AV_CODEC_ID_ADPCM_MS:
  2643. return 4;
  2644. default:
  2645. return av_get_exact_bits_per_sample(codec_id);
  2646. }
  2647. }
  2648. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2649. {
  2650. int id, sr, ch, ba, tag, bps;
  2651. id = avctx->codec_id;
  2652. sr = avctx->sample_rate;
  2653. ch = avctx->channels;
  2654. ba = avctx->block_align;
  2655. tag = avctx->codec_tag;
  2656. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  2657. /* codecs with an exact constant bits per sample */
  2658. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  2659. return (frame_bytes * 8LL) / (bps * ch);
  2660. bps = avctx->bits_per_coded_sample;
  2661. /* codecs with a fixed packet duration */
  2662. switch (id) {
  2663. case AV_CODEC_ID_ADPCM_ADX: return 32;
  2664. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  2665. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  2666. case AV_CODEC_ID_AMR_NB:
  2667. case AV_CODEC_ID_EVRC:
  2668. case AV_CODEC_ID_GSM:
  2669. case AV_CODEC_ID_QCELP:
  2670. case AV_CODEC_ID_RA_288: return 160;
  2671. case AV_CODEC_ID_AMR_WB:
  2672. case AV_CODEC_ID_GSM_MS: return 320;
  2673. case AV_CODEC_ID_MP1: return 384;
  2674. case AV_CODEC_ID_ATRAC1: return 512;
  2675. case AV_CODEC_ID_ATRAC3: return 1024;
  2676. case AV_CODEC_ID_ATRAC3P: return 2048;
  2677. case AV_CODEC_ID_MP2:
  2678. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2679. case AV_CODEC_ID_AC3: return 1536;
  2680. }
  2681. if (sr > 0) {
  2682. /* calc from sample rate */
  2683. if (id == AV_CODEC_ID_TTA)
  2684. return 256 * sr / 245;
  2685. if (ch > 0) {
  2686. /* calc from sample rate and channels */
  2687. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2688. return (480 << (sr / 22050)) / ch;
  2689. }
  2690. }
  2691. if (ba > 0) {
  2692. /* calc from block_align */
  2693. if (id == AV_CODEC_ID_SIPR) {
  2694. switch (ba) {
  2695. case 20: return 160;
  2696. case 19: return 144;
  2697. case 29: return 288;
  2698. case 37: return 480;
  2699. }
  2700. } else if (id == AV_CODEC_ID_ILBC) {
  2701. switch (ba) {
  2702. case 38: return 160;
  2703. case 50: return 240;
  2704. }
  2705. }
  2706. }
  2707. if (frame_bytes > 0) {
  2708. /* calc from frame_bytes only */
  2709. if (id == AV_CODEC_ID_TRUESPEECH)
  2710. return 240 * (frame_bytes / 32);
  2711. if (id == AV_CODEC_ID_NELLYMOSER)
  2712. return 256 * (frame_bytes / 64);
  2713. if (id == AV_CODEC_ID_RA_144)
  2714. return 160 * (frame_bytes / 20);
  2715. if (id == AV_CODEC_ID_G723_1)
  2716. return 240 * (frame_bytes / 24);
  2717. if (bps > 0) {
  2718. /* calc from frame_bytes and bits_per_coded_sample */
  2719. if (id == AV_CODEC_ID_ADPCM_G726)
  2720. return frame_bytes * 8 / bps;
  2721. }
  2722. if (ch > 0 && ch < INT_MAX/16) {
  2723. /* calc from frame_bytes and channels */
  2724. switch (id) {
  2725. case AV_CODEC_ID_ADPCM_AFC:
  2726. return frame_bytes / (9 * ch) * 16;
  2727. case AV_CODEC_ID_ADPCM_PSX:
  2728. case AV_CODEC_ID_ADPCM_DTK:
  2729. return frame_bytes / (16 * ch) * 28;
  2730. case AV_CODEC_ID_ADPCM_4XM:
  2731. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2732. return (frame_bytes - 4 * ch) * 2 / ch;
  2733. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2734. return (frame_bytes - 4) * 2 / ch;
  2735. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2736. return (frame_bytes - 8) * 2 / ch;
  2737. case AV_CODEC_ID_ADPCM_THP:
  2738. case AV_CODEC_ID_ADPCM_THP_LE:
  2739. if (avctx->extradata)
  2740. return frame_bytes * 14 / (8 * ch);
  2741. break;
  2742. case AV_CODEC_ID_ADPCM_XA:
  2743. return (frame_bytes / 128) * 224 / ch;
  2744. case AV_CODEC_ID_INTERPLAY_DPCM:
  2745. return (frame_bytes - 6 - ch) / ch;
  2746. case AV_CODEC_ID_ROQ_DPCM:
  2747. return (frame_bytes - 8) / ch;
  2748. case AV_CODEC_ID_XAN_DPCM:
  2749. return (frame_bytes - 2 * ch) / ch;
  2750. case AV_CODEC_ID_MACE3:
  2751. return 3 * frame_bytes / ch;
  2752. case AV_CODEC_ID_MACE6:
  2753. return 6 * frame_bytes / ch;
  2754. case AV_CODEC_ID_PCM_LXF:
  2755. return 2 * (frame_bytes / (5 * ch));
  2756. case AV_CODEC_ID_IAC:
  2757. case AV_CODEC_ID_IMC:
  2758. return 4 * frame_bytes / ch;
  2759. }
  2760. if (tag) {
  2761. /* calc from frame_bytes, channels, and codec_tag */
  2762. if (id == AV_CODEC_ID_SOL_DPCM) {
  2763. if (tag == 3)
  2764. return frame_bytes / ch;
  2765. else
  2766. return frame_bytes * 2 / ch;
  2767. }
  2768. }
  2769. if (ba > 0) {
  2770. /* calc from frame_bytes, channels, and block_align */
  2771. int blocks = frame_bytes / ba;
  2772. switch (avctx->codec_id) {
  2773. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2774. if (bps < 2 || bps > 5)
  2775. return 0;
  2776. return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
  2777. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2778. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2779. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2780. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2781. case AV_CODEC_ID_ADPCM_IMA_RAD:
  2782. return blocks * ((ba - 4 * ch) * 2 / ch);
  2783. case AV_CODEC_ID_ADPCM_MS:
  2784. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2785. }
  2786. }
  2787. if (bps > 0) {
  2788. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2789. switch (avctx->codec_id) {
  2790. case AV_CODEC_ID_PCM_DVD:
  2791. if(bps<4)
  2792. return 0;
  2793. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2794. case AV_CODEC_ID_PCM_BLURAY:
  2795. if(bps<4)
  2796. return 0;
  2797. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2798. case AV_CODEC_ID_S302M:
  2799. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2800. }
  2801. }
  2802. }
  2803. }
  2804. /* Fall back on using frame_size */
  2805. if (avctx->frame_size > 1 && frame_bytes)
  2806. return avctx->frame_size;
  2807. //For WMA we currently have no other means to calculate duration thus we
  2808. //do it here by assuming CBR, which is true for all known cases.
  2809. if (avctx->bit_rate>0 && frame_bytes>0 && avctx->sample_rate>0 && avctx->block_align>1) {
  2810. if (avctx->codec_id == AV_CODEC_ID_WMAV1 || avctx->codec_id == AV_CODEC_ID_WMAV2)
  2811. return (frame_bytes * 8LL * avctx->sample_rate) / avctx->bit_rate;
  2812. }
  2813. return 0;
  2814. }
  2815. #if !HAVE_THREADS
  2816. int ff_thread_init(AVCodecContext *s)
  2817. {
  2818. return -1;
  2819. }
  2820. #endif
  2821. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2822. {
  2823. unsigned int n = 0;
  2824. while (v >= 0xff) {
  2825. *s++ = 0xff;
  2826. v -= 0xff;
  2827. n++;
  2828. }
  2829. *s = v;
  2830. n++;
  2831. return n;
  2832. }
  2833. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2834. {
  2835. int i;
  2836. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2837. return i;
  2838. }
  2839. #if FF_API_MISSING_SAMPLE
  2840. FF_DISABLE_DEPRECATION_WARNINGS
  2841. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2842. {
  2843. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  2844. "version to the newest one from Git. If the problem still "
  2845. "occurs, it means that your file has a feature which has not "
  2846. "been implemented.\n", feature);
  2847. if(want_sample)
  2848. av_log_ask_for_sample(avc, NULL);
  2849. }
  2850. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2851. {
  2852. va_list argument_list;
  2853. va_start(argument_list, msg);
  2854. if (msg)
  2855. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2856. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2857. "of this file to ftp://upload.ffmpeg.org/incoming/ "
  2858. "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n");
  2859. va_end(argument_list);
  2860. }
  2861. FF_ENABLE_DEPRECATION_WARNINGS
  2862. #endif /* FF_API_MISSING_SAMPLE */
  2863. static AVHWAccel *first_hwaccel = NULL;
  2864. static AVHWAccel **last_hwaccel = &first_hwaccel;
  2865. void av_register_hwaccel(AVHWAccel *hwaccel)
  2866. {
  2867. AVHWAccel **p = last_hwaccel;
  2868. hwaccel->next = NULL;
  2869. while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
  2870. p = &(*p)->next;
  2871. last_hwaccel = &hwaccel->next;
  2872. }
  2873. AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
  2874. {
  2875. return hwaccel ? hwaccel->next : first_hwaccel;
  2876. }
  2877. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2878. {
  2879. if (lockmgr_cb) {
  2880. // There is no good way to rollback a failure to destroy the
  2881. // mutex, so we ignore failures.
  2882. lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
  2883. lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
  2884. lockmgr_cb = NULL;
  2885. codec_mutex = NULL;
  2886. avformat_mutex = NULL;
  2887. }
  2888. if (cb) {
  2889. void *new_codec_mutex = NULL;
  2890. void *new_avformat_mutex = NULL;
  2891. int err;
  2892. if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
  2893. return err > 0 ? AVERROR_UNKNOWN : err;
  2894. }
  2895. if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
  2896. // Ignore failures to destroy the newly created mutex.
  2897. cb(&new_codec_mutex, AV_LOCK_DESTROY);
  2898. return err > 0 ? AVERROR_UNKNOWN : err;
  2899. }
  2900. lockmgr_cb = cb;
  2901. codec_mutex = new_codec_mutex;
  2902. avformat_mutex = new_avformat_mutex;
  2903. }
  2904. return 0;
  2905. }
  2906. int ff_lock_avcodec(AVCodecContext *log_ctx, const AVCodec *codec)
  2907. {
  2908. if (codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE || !codec->init)
  2909. return 0;
  2910. if (lockmgr_cb) {
  2911. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  2912. return -1;
  2913. }
  2914. if (avpriv_atomic_int_add_and_fetch(&entangled_thread_counter, 1) != 1) {
  2915. av_log(log_ctx, AV_LOG_ERROR,
  2916. "Insufficient thread locking. At least %d threads are "
  2917. "calling avcodec_open2() at the same time right now.\n",
  2918. entangled_thread_counter);
  2919. if (!lockmgr_cb)
  2920. av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
  2921. ff_avcodec_locked = 1;
  2922. ff_unlock_avcodec(codec);
  2923. return AVERROR(EINVAL);
  2924. }
  2925. av_assert0(!ff_avcodec_locked);
  2926. ff_avcodec_locked = 1;
  2927. return 0;
  2928. }
  2929. int ff_unlock_avcodec(const AVCodec *codec)
  2930. {
  2931. if (codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE || !codec->init)
  2932. return 0;
  2933. av_assert0(ff_avcodec_locked);
  2934. ff_avcodec_locked = 0;
  2935. avpriv_atomic_int_add_and_fetch(&entangled_thread_counter, -1);
  2936. if (lockmgr_cb) {
  2937. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
  2938. return -1;
  2939. }
  2940. return 0;
  2941. }
  2942. int avpriv_lock_avformat(void)
  2943. {
  2944. if (lockmgr_cb) {
  2945. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2946. return -1;
  2947. }
  2948. return 0;
  2949. }
  2950. int avpriv_unlock_avformat(void)
  2951. {
  2952. if (lockmgr_cb) {
  2953. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2954. return -1;
  2955. }
  2956. return 0;
  2957. }
  2958. unsigned int avpriv_toupper4(unsigned int x)
  2959. {
  2960. return av_toupper(x & 0xFF) +
  2961. (av_toupper((x >> 8) & 0xFF) << 8) +
  2962. (av_toupper((x >> 16) & 0xFF) << 16) +
  2963. ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
  2964. }
  2965. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  2966. {
  2967. int ret;
  2968. dst->owner = src->owner;
  2969. ret = av_frame_ref(dst->f, src->f);
  2970. if (ret < 0)
  2971. return ret;
  2972. av_assert0(!dst->progress);
  2973. if (src->progress &&
  2974. !(dst->progress = av_buffer_ref(src->progress))) {
  2975. ff_thread_release_buffer(dst->owner, dst);
  2976. return AVERROR(ENOMEM);
  2977. }
  2978. return 0;
  2979. }
  2980. #if !HAVE_THREADS
  2981. enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  2982. {
  2983. return ff_get_format(avctx, fmt);
  2984. }
  2985. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  2986. {
  2987. f->owner = avctx;
  2988. return ff_get_buffer(avctx, f->f, flags);
  2989. }
  2990. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  2991. {
  2992. if (f->f)
  2993. av_frame_unref(f->f);
  2994. }
  2995. void ff_thread_finish_setup(AVCodecContext *avctx)
  2996. {
  2997. }
  2998. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  2999. {
  3000. }
  3001. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  3002. {
  3003. }
  3004. int ff_thread_can_start_frame(AVCodecContext *avctx)
  3005. {
  3006. return 1;
  3007. }
  3008. int ff_alloc_entries(AVCodecContext *avctx, int count)
  3009. {
  3010. return 0;
  3011. }
  3012. void ff_reset_entries(AVCodecContext *avctx)
  3013. {
  3014. }
  3015. void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
  3016. {
  3017. }
  3018. void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
  3019. {
  3020. }
  3021. #endif
  3022. int avcodec_is_open(AVCodecContext *s)
  3023. {
  3024. return !!s->internal;
  3025. }
  3026. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
  3027. {
  3028. int ret;
  3029. char *str;
  3030. ret = av_bprint_finalize(buf, &str);
  3031. if (ret < 0)
  3032. return ret;
  3033. if (!av_bprint_is_complete(buf)) {
  3034. av_free(str);
  3035. return AVERROR(ENOMEM);
  3036. }
  3037. avctx->extradata = str;
  3038. /* Note: the string is NUL terminated (so extradata can be read as a
  3039. * string), but the ending character is not accounted in the size (in
  3040. * binary formats you are likely not supposed to mux that character). When
  3041. * extradata is copied, it is also padded with AV_INPUT_BUFFER_PADDING_SIZE
  3042. * zeros. */
  3043. avctx->extradata_size = buf->len;
  3044. return 0;
  3045. }
  3046. const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
  3047. const uint8_t *end,
  3048. uint32_t *av_restrict state)
  3049. {
  3050. int i;
  3051. av_assert0(p <= end);
  3052. if (p >= end)
  3053. return end;
  3054. for (i = 0; i < 3; i++) {
  3055. uint32_t tmp = *state << 8;
  3056. *state = tmp + *(p++);
  3057. if (tmp == 0x100 || p == end)
  3058. return p;
  3059. }
  3060. while (p < end) {
  3061. if (p[-1] > 1 ) p += 3;
  3062. else if (p[-2] ) p += 2;
  3063. else if (p[-3]|(p[-1]-1)) p++;
  3064. else {
  3065. p++;
  3066. break;
  3067. }
  3068. }
  3069. p = FFMIN(p, end) - 4;
  3070. *state = AV_RB32(p);
  3071. return p + 4;
  3072. }
  3073. AVCPBProperties *av_cpb_properties_alloc(size_t *size)
  3074. {
  3075. AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
  3076. if (!props)
  3077. return NULL;
  3078. if (size)
  3079. *size = sizeof(*props);
  3080. props->vbv_delay = UINT64_MAX;
  3081. return props;
  3082. }
  3083. AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
  3084. {
  3085. AVPacketSideData *tmp;
  3086. AVCPBProperties *props;
  3087. size_t size;
  3088. props = av_cpb_properties_alloc(&size);
  3089. if (!props)
  3090. return NULL;
  3091. tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
  3092. if (!tmp) {
  3093. av_freep(&props);
  3094. return NULL;
  3095. }
  3096. avctx->coded_side_data = tmp;
  3097. avctx->nb_coded_side_data++;
  3098. avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
  3099. avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
  3100. avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
  3101. return props;
  3102. }