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.

3545 lines
116KB

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