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.

3559 lines
117KB

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