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.

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