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.

3531 lines
115KB

  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. if (avcodec_is_open(avctx))
  1010. return 0;
  1011. if ((!codec && !avctx->codec)) {
  1012. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  1013. return AVERROR(EINVAL);
  1014. }
  1015. if ((codec && avctx->codec && codec != avctx->codec)) {
  1016. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  1017. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  1018. return AVERROR(EINVAL);
  1019. }
  1020. if (!codec)
  1021. codec = avctx->codec;
  1022. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  1023. return AVERROR(EINVAL);
  1024. if (options)
  1025. av_dict_copy(&tmp, *options, 0);
  1026. ret = ff_lock_avcodec(avctx, codec);
  1027. if (ret < 0)
  1028. return ret;
  1029. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  1030. if (!avctx->internal) {
  1031. ret = AVERROR(ENOMEM);
  1032. goto end;
  1033. }
  1034. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  1035. if (!avctx->internal->pool) {
  1036. ret = AVERROR(ENOMEM);
  1037. goto free_and_end;
  1038. }
  1039. avctx->internal->to_free = av_frame_alloc();
  1040. if (!avctx->internal->to_free) {
  1041. ret = AVERROR(ENOMEM);
  1042. goto free_and_end;
  1043. }
  1044. if (codec->priv_data_size > 0) {
  1045. if (!avctx->priv_data) {
  1046. avctx->priv_data = av_mallocz(codec->priv_data_size);
  1047. if (!avctx->priv_data) {
  1048. ret = AVERROR(ENOMEM);
  1049. goto end;
  1050. }
  1051. if (codec->priv_class) {
  1052. *(const AVClass **)avctx->priv_data = codec->priv_class;
  1053. av_opt_set_defaults(avctx->priv_data);
  1054. }
  1055. }
  1056. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  1057. goto free_and_end;
  1058. } else {
  1059. avctx->priv_data = NULL;
  1060. }
  1061. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  1062. goto free_and_end;
  1063. if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
  1064. av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist\n", codec->name);
  1065. ret = AVERROR(EINVAL);
  1066. goto free_and_end;
  1067. }
  1068. // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
  1069. if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
  1070. (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
  1071. if (avctx->coded_width && avctx->coded_height)
  1072. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  1073. else if (avctx->width && avctx->height)
  1074. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1075. if (ret < 0)
  1076. goto free_and_end;
  1077. }
  1078. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  1079. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  1080. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  1081. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  1082. ff_set_dimensions(avctx, 0, 0);
  1083. }
  1084. if (avctx->width > 0 && avctx->height > 0) {
  1085. if (av_image_check_sar(avctx->width, avctx->height,
  1086. avctx->sample_aspect_ratio) < 0) {
  1087. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  1088. avctx->sample_aspect_ratio.num,
  1089. avctx->sample_aspect_ratio.den);
  1090. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  1091. }
  1092. }
  1093. /* if the decoder init function was already called previously,
  1094. * free the already allocated subtitle_header before overwriting it */
  1095. if (av_codec_is_decoder(codec))
  1096. av_freep(&avctx->subtitle_header);
  1097. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  1098. ret = AVERROR(EINVAL);
  1099. goto free_and_end;
  1100. }
  1101. avctx->codec = codec;
  1102. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  1103. avctx->codec_id == AV_CODEC_ID_NONE) {
  1104. avctx->codec_type = codec->type;
  1105. avctx->codec_id = codec->id;
  1106. }
  1107. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  1108. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  1109. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  1110. ret = AVERROR(EINVAL);
  1111. goto free_and_end;
  1112. }
  1113. avctx->frame_number = 0;
  1114. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  1115. if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
  1116. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  1117. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  1118. AVCodec *codec2;
  1119. av_log(avctx, AV_LOG_ERROR,
  1120. "The %s '%s' is experimental but experimental codecs are not enabled, "
  1121. "add '-strict %d' if you want to use it.\n",
  1122. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  1123. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  1124. if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
  1125. av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  1126. codec_string, codec2->name);
  1127. ret = AVERROR_EXPERIMENTAL;
  1128. goto free_and_end;
  1129. }
  1130. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  1131. (!avctx->time_base.num || !avctx->time_base.den)) {
  1132. avctx->time_base.num = 1;
  1133. avctx->time_base.den = avctx->sample_rate;
  1134. }
  1135. if (!HAVE_THREADS)
  1136. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  1137. if (CONFIG_FRAME_THREAD_ENCODER) {
  1138. ff_unlock_avcodec(codec); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  1139. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  1140. ff_lock_avcodec(avctx, codec);
  1141. if (ret < 0)
  1142. goto free_and_end;
  1143. }
  1144. if (HAVE_THREADS
  1145. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  1146. ret = ff_thread_init(avctx);
  1147. if (ret < 0) {
  1148. goto free_and_end;
  1149. }
  1150. }
  1151. if (!HAVE_THREADS && !(codec->capabilities & AV_CODEC_CAP_AUTO_THREADS))
  1152. avctx->thread_count = 1;
  1153. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  1154. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  1155. avctx->codec->max_lowres);
  1156. ret = AVERROR(EINVAL);
  1157. goto free_and_end;
  1158. }
  1159. #if FF_API_VISMV
  1160. if (avctx->debug_mv)
  1161. av_log(avctx, AV_LOG_WARNING, "The 'vismv' option is deprecated, "
  1162. "see the codecview filter instead.\n");
  1163. #endif
  1164. if (av_codec_is_encoder(avctx->codec)) {
  1165. int i;
  1166. #if FF_API_CODED_FRAME
  1167. FF_DISABLE_DEPRECATION_WARNINGS
  1168. avctx->coded_frame = av_frame_alloc();
  1169. if (!avctx->coded_frame) {
  1170. ret = AVERROR(ENOMEM);
  1171. goto free_and_end;
  1172. }
  1173. FF_ENABLE_DEPRECATION_WARNINGS
  1174. #endif
  1175. if (avctx->codec->sample_fmts) {
  1176. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  1177. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  1178. break;
  1179. if (avctx->channels == 1 &&
  1180. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  1181. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  1182. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  1183. break;
  1184. }
  1185. }
  1186. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  1187. char buf[128];
  1188. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  1189. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  1190. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  1191. ret = AVERROR(EINVAL);
  1192. goto free_and_end;
  1193. }
  1194. }
  1195. if (avctx->codec->pix_fmts) {
  1196. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  1197. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  1198. break;
  1199. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  1200. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  1201. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  1202. char buf[128];
  1203. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  1204. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  1205. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  1206. ret = AVERROR(EINVAL);
  1207. goto free_and_end;
  1208. }
  1209. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
  1210. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
  1211. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
  1212. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
  1213. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
  1214. avctx->color_range = AVCOL_RANGE_JPEG;
  1215. }
  1216. if (avctx->codec->supported_samplerates) {
  1217. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  1218. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  1219. break;
  1220. if (avctx->codec->supported_samplerates[i] == 0) {
  1221. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  1222. avctx->sample_rate);
  1223. ret = AVERROR(EINVAL);
  1224. goto free_and_end;
  1225. }
  1226. }
  1227. if (avctx->sample_rate < 0) {
  1228. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  1229. avctx->sample_rate);
  1230. ret = AVERROR(EINVAL);
  1231. goto free_and_end;
  1232. }
  1233. if (avctx->codec->channel_layouts) {
  1234. if (!avctx->channel_layout) {
  1235. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  1236. } else {
  1237. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  1238. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  1239. break;
  1240. if (avctx->codec->channel_layouts[i] == 0) {
  1241. char buf[512];
  1242. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1243. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  1244. ret = AVERROR(EINVAL);
  1245. goto free_and_end;
  1246. }
  1247. }
  1248. }
  1249. if (avctx->channel_layout && avctx->channels) {
  1250. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1251. if (channels != avctx->channels) {
  1252. char buf[512];
  1253. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1254. av_log(avctx, AV_LOG_ERROR,
  1255. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  1256. buf, channels, avctx->channels);
  1257. ret = AVERROR(EINVAL);
  1258. goto free_and_end;
  1259. }
  1260. } else if (avctx->channel_layout) {
  1261. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1262. }
  1263. if (avctx->channels < 0) {
  1264. av_log(avctx, AV_LOG_ERROR, "Specified number of channels %d is not supported\n",
  1265. avctx->channels);
  1266. ret = AVERROR(EINVAL);
  1267. goto free_and_end;
  1268. }
  1269. if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  1270. if (avctx->width <= 0 || avctx->height <= 0) {
  1271. av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
  1272. ret = AVERROR(EINVAL);
  1273. goto free_and_end;
  1274. }
  1275. }
  1276. if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
  1277. && avctx->bit_rate>0 && avctx->bit_rate<1000) {
  1278. 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);
  1279. }
  1280. if (!avctx->rc_initial_buffer_occupancy)
  1281. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  1282. }
  1283. avctx->pts_correction_num_faulty_pts =
  1284. avctx->pts_correction_num_faulty_dts = 0;
  1285. avctx->pts_correction_last_pts =
  1286. avctx->pts_correction_last_dts = INT64_MIN;
  1287. if ( !CONFIG_GRAY && avctx->flags & AV_CODEC_FLAG_GRAY
  1288. && avctx->codec_descriptor->type == AVMEDIA_TYPE_VIDEO)
  1289. av_log(avctx, AV_LOG_WARNING,
  1290. "gray decoding requested but not enabled at configuration time\n");
  1291. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  1292. || avctx->internal->frame_thread_encoder)) {
  1293. ret = avctx->codec->init(avctx);
  1294. if (ret < 0) {
  1295. goto free_and_end;
  1296. }
  1297. }
  1298. ret=0;
  1299. #if FF_API_AUDIOENC_DELAY
  1300. if (av_codec_is_encoder(avctx->codec))
  1301. avctx->delay = avctx->initial_padding;
  1302. #endif
  1303. if (av_codec_is_decoder(avctx->codec)) {
  1304. if (!avctx->bit_rate)
  1305. avctx->bit_rate = get_bit_rate(avctx);
  1306. /* validate channel layout from the decoder */
  1307. if (avctx->channel_layout) {
  1308. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1309. if (!avctx->channels)
  1310. avctx->channels = channels;
  1311. else if (channels != avctx->channels) {
  1312. char buf[512];
  1313. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1314. av_log(avctx, AV_LOG_WARNING,
  1315. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  1316. "ignoring specified channel layout\n",
  1317. buf, channels, avctx->channels);
  1318. avctx->channel_layout = 0;
  1319. }
  1320. }
  1321. if (avctx->channels && avctx->channels < 0 ||
  1322. avctx->channels > FF_SANE_NB_CHANNELS) {
  1323. ret = AVERROR(EINVAL);
  1324. goto free_and_end;
  1325. }
  1326. if (avctx->sub_charenc) {
  1327. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  1328. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  1329. "supported with subtitles codecs\n");
  1330. ret = AVERROR(EINVAL);
  1331. goto free_and_end;
  1332. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  1333. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  1334. "subtitles character encoding will be ignored\n",
  1335. avctx->codec_descriptor->name);
  1336. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  1337. } else {
  1338. /* input character encoding is set for a text based subtitle
  1339. * codec at this point */
  1340. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  1341. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  1342. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  1343. #if CONFIG_ICONV
  1344. iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  1345. if (cd == (iconv_t)-1) {
  1346. ret = AVERROR(errno);
  1347. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  1348. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  1349. goto free_and_end;
  1350. }
  1351. iconv_close(cd);
  1352. #else
  1353. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  1354. "conversion needs a libavcodec built with iconv support "
  1355. "for this codec\n");
  1356. ret = AVERROR(ENOSYS);
  1357. goto free_and_end;
  1358. #endif
  1359. }
  1360. }
  1361. }
  1362. #if FF_API_AVCTX_TIMEBASE
  1363. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  1364. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  1365. #endif
  1366. }
  1367. if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
  1368. av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
  1369. }
  1370. end:
  1371. ff_unlock_avcodec(codec);
  1372. if (options) {
  1373. av_dict_free(options);
  1374. *options = tmp;
  1375. }
  1376. return ret;
  1377. free_and_end:
  1378. if (avctx->codec &&
  1379. (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
  1380. avctx->codec->close(avctx);
  1381. if (codec->priv_class && codec->priv_data_size)
  1382. av_opt_free(avctx->priv_data);
  1383. av_opt_free(avctx);
  1384. #if FF_API_CODED_FRAME
  1385. FF_DISABLE_DEPRECATION_WARNINGS
  1386. av_frame_free(&avctx->coded_frame);
  1387. FF_ENABLE_DEPRECATION_WARNINGS
  1388. #endif
  1389. av_dict_free(&tmp);
  1390. av_freep(&avctx->priv_data);
  1391. if (avctx->internal) {
  1392. av_frame_free(&avctx->internal->to_free);
  1393. av_freep(&avctx->internal->pool);
  1394. }
  1395. av_freep(&avctx->internal);
  1396. avctx->codec = NULL;
  1397. goto end;
  1398. }
  1399. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
  1400. {
  1401. if (avpkt->size < 0) {
  1402. av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
  1403. return AVERROR(EINVAL);
  1404. }
  1405. if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  1406. av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
  1407. size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
  1408. return AVERROR(EINVAL);
  1409. }
  1410. if (avctx && 2*min_size < size) { // FIXME The factor needs to be finetuned
  1411. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  1412. if (!avpkt->data || avpkt->size < size) {
  1413. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  1414. avpkt->data = avctx->internal->byte_buffer;
  1415. avpkt->size = avctx->internal->byte_buffer_size;
  1416. }
  1417. }
  1418. if (avpkt->data) {
  1419. AVBufferRef *buf = avpkt->buf;
  1420. if (avpkt->size < size) {
  1421. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
  1422. return AVERROR(EINVAL);
  1423. }
  1424. av_init_packet(avpkt);
  1425. avpkt->buf = buf;
  1426. avpkt->size = size;
  1427. return 0;
  1428. } else {
  1429. int ret = av_new_packet(avpkt, size);
  1430. if (ret < 0)
  1431. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
  1432. return ret;
  1433. }
  1434. }
  1435. int ff_alloc_packet(AVPacket *avpkt, int size)
  1436. {
  1437. return ff_alloc_packet2(NULL, avpkt, size, 0);
  1438. }
  1439. /**
  1440. * Pad last frame with silence.
  1441. */
  1442. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1443. {
  1444. AVFrame *frame = NULL;
  1445. int ret;
  1446. if (!(frame = av_frame_alloc()))
  1447. return AVERROR(ENOMEM);
  1448. frame->format = src->format;
  1449. frame->channel_layout = src->channel_layout;
  1450. av_frame_set_channels(frame, av_frame_get_channels(src));
  1451. frame->nb_samples = s->frame_size;
  1452. ret = av_frame_get_buffer(frame, 32);
  1453. if (ret < 0)
  1454. goto fail;
  1455. ret = av_frame_copy_props(frame, src);
  1456. if (ret < 0)
  1457. goto fail;
  1458. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1459. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1460. goto fail;
  1461. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1462. frame->nb_samples - src->nb_samples,
  1463. s->channels, s->sample_fmt)) < 0)
  1464. goto fail;
  1465. *dst = frame;
  1466. return 0;
  1467. fail:
  1468. av_frame_free(&frame);
  1469. return ret;
  1470. }
  1471. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1472. AVPacket *avpkt,
  1473. const AVFrame *frame,
  1474. int *got_packet_ptr)
  1475. {
  1476. AVFrame *extended_frame = NULL;
  1477. AVFrame *padded_frame = NULL;
  1478. int ret;
  1479. AVPacket user_pkt = *avpkt;
  1480. int needs_realloc = !user_pkt.data;
  1481. *got_packet_ptr = 0;
  1482. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  1483. av_packet_unref(avpkt);
  1484. av_init_packet(avpkt);
  1485. return 0;
  1486. }
  1487. /* ensure that extended_data is properly set */
  1488. if (frame && !frame->extended_data) {
  1489. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1490. avctx->channels > AV_NUM_DATA_POINTERS) {
  1491. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1492. "with more than %d channels, but extended_data is not set.\n",
  1493. AV_NUM_DATA_POINTERS);
  1494. return AVERROR(EINVAL);
  1495. }
  1496. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1497. extended_frame = av_frame_alloc();
  1498. if (!extended_frame)
  1499. return AVERROR(ENOMEM);
  1500. memcpy(extended_frame, frame, sizeof(AVFrame));
  1501. extended_frame->extended_data = extended_frame->data;
  1502. frame = extended_frame;
  1503. }
  1504. /* extract audio service type metadata */
  1505. if (frame) {
  1506. AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
  1507. if (sd && sd->size >= sizeof(enum AVAudioServiceType))
  1508. avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
  1509. }
  1510. /* check for valid frame size */
  1511. if (frame) {
  1512. if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
  1513. if (frame->nb_samples > avctx->frame_size) {
  1514. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1515. ret = AVERROR(EINVAL);
  1516. goto end;
  1517. }
  1518. } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1519. if (frame->nb_samples < avctx->frame_size &&
  1520. !avctx->internal->last_audio_frame) {
  1521. ret = pad_last_frame(avctx, &padded_frame, frame);
  1522. if (ret < 0)
  1523. goto end;
  1524. frame = padded_frame;
  1525. avctx->internal->last_audio_frame = 1;
  1526. }
  1527. if (frame->nb_samples != avctx->frame_size) {
  1528. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1529. ret = AVERROR(EINVAL);
  1530. goto end;
  1531. }
  1532. }
  1533. }
  1534. av_assert0(avctx->codec->encode2);
  1535. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1536. if (!ret) {
  1537. if (*got_packet_ptr) {
  1538. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
  1539. if (avpkt->pts == AV_NOPTS_VALUE)
  1540. avpkt->pts = frame->pts;
  1541. if (!avpkt->duration)
  1542. avpkt->duration = ff_samples_to_time_base(avctx,
  1543. frame->nb_samples);
  1544. }
  1545. avpkt->dts = avpkt->pts;
  1546. } else {
  1547. avpkt->size = 0;
  1548. }
  1549. }
  1550. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1551. needs_realloc = 0;
  1552. if (user_pkt.data) {
  1553. if (user_pkt.size >= avpkt->size) {
  1554. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1555. } else {
  1556. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1557. avpkt->size = user_pkt.size;
  1558. ret = -1;
  1559. }
  1560. avpkt->buf = user_pkt.buf;
  1561. avpkt->data = user_pkt.data;
  1562. } else {
  1563. if (av_dup_packet(avpkt) < 0) {
  1564. ret = AVERROR(ENOMEM);
  1565. }
  1566. }
  1567. }
  1568. if (!ret) {
  1569. if (needs_realloc && avpkt->data) {
  1570. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
  1571. if (ret >= 0)
  1572. avpkt->data = avpkt->buf->data;
  1573. }
  1574. avctx->frame_number++;
  1575. }
  1576. if (ret < 0 || !*got_packet_ptr) {
  1577. av_packet_unref(avpkt);
  1578. av_init_packet(avpkt);
  1579. goto end;
  1580. }
  1581. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1582. * this needs to be moved to the encoders, but for now we can do it
  1583. * here to simplify things */
  1584. avpkt->flags |= AV_PKT_FLAG_KEY;
  1585. end:
  1586. av_frame_free(&padded_frame);
  1587. av_free(extended_frame);
  1588. #if FF_API_AUDIOENC_DELAY
  1589. avctx->delay = avctx->initial_padding;
  1590. #endif
  1591. return ret;
  1592. }
  1593. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1594. AVPacket *avpkt,
  1595. const AVFrame *frame,
  1596. int *got_packet_ptr)
  1597. {
  1598. int ret;
  1599. AVPacket user_pkt = *avpkt;
  1600. int needs_realloc = !user_pkt.data;
  1601. *got_packet_ptr = 0;
  1602. if(CONFIG_FRAME_THREAD_ENCODER &&
  1603. avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1604. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1605. if ((avctx->flags&AV_CODEC_FLAG_PASS1) && avctx->stats_out)
  1606. avctx->stats_out[0] = '\0';
  1607. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  1608. av_packet_unref(avpkt);
  1609. av_init_packet(avpkt);
  1610. avpkt->size = 0;
  1611. return 0;
  1612. }
  1613. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1614. return AVERROR(EINVAL);
  1615. if (frame && frame->format == AV_PIX_FMT_NONE)
  1616. av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
  1617. if (frame && (frame->width == 0 || frame->height == 0))
  1618. av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
  1619. av_assert0(avctx->codec->encode2);
  1620. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1621. av_assert0(ret <= 0);
  1622. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1623. needs_realloc = 0;
  1624. if (user_pkt.data) {
  1625. if (user_pkt.size >= avpkt->size) {
  1626. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1627. } else {
  1628. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1629. avpkt->size = user_pkt.size;
  1630. ret = -1;
  1631. }
  1632. avpkt->buf = user_pkt.buf;
  1633. avpkt->data = user_pkt.data;
  1634. } else {
  1635. if (av_dup_packet(avpkt) < 0) {
  1636. ret = AVERROR(ENOMEM);
  1637. }
  1638. }
  1639. }
  1640. if (!ret) {
  1641. if (!*got_packet_ptr)
  1642. avpkt->size = 0;
  1643. else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  1644. avpkt->pts = avpkt->dts = frame->pts;
  1645. if (needs_realloc && avpkt->data) {
  1646. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
  1647. if (ret >= 0)
  1648. avpkt->data = avpkt->buf->data;
  1649. }
  1650. avctx->frame_number++;
  1651. }
  1652. if (ret < 0 || !*got_packet_ptr)
  1653. av_packet_unref(avpkt);
  1654. emms_c();
  1655. return ret;
  1656. }
  1657. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1658. const AVSubtitle *sub)
  1659. {
  1660. int ret;
  1661. if (sub->start_display_time) {
  1662. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1663. return -1;
  1664. }
  1665. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1666. avctx->frame_number++;
  1667. return ret;
  1668. }
  1669. /**
  1670. * Attempt to guess proper monotonic timestamps for decoded video frames
  1671. * which might have incorrect times. Input timestamps may wrap around, in
  1672. * which case the output will as well.
  1673. *
  1674. * @param pts the pts field of the decoded AVPacket, as passed through
  1675. * AVFrame.pkt_pts
  1676. * @param dts the dts field of the decoded AVPacket
  1677. * @return one of the input values, may be AV_NOPTS_VALUE
  1678. */
  1679. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1680. int64_t reordered_pts, int64_t dts)
  1681. {
  1682. int64_t pts = AV_NOPTS_VALUE;
  1683. if (dts != AV_NOPTS_VALUE) {
  1684. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1685. ctx->pts_correction_last_dts = dts;
  1686. } else if (reordered_pts != AV_NOPTS_VALUE)
  1687. ctx->pts_correction_last_dts = reordered_pts;
  1688. if (reordered_pts != AV_NOPTS_VALUE) {
  1689. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1690. ctx->pts_correction_last_pts = reordered_pts;
  1691. } else if(dts != AV_NOPTS_VALUE)
  1692. ctx->pts_correction_last_pts = dts;
  1693. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1694. && reordered_pts != AV_NOPTS_VALUE)
  1695. pts = reordered_pts;
  1696. else
  1697. pts = dts;
  1698. return pts;
  1699. }
  1700. static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1701. {
  1702. int size = 0, ret;
  1703. const uint8_t *data;
  1704. uint32_t flags;
  1705. int64_t val;
  1706. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1707. if (!data)
  1708. return 0;
  1709. if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
  1710. av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
  1711. "changes, but PARAM_CHANGE side data was sent to it.\n");
  1712. return AVERROR(EINVAL);
  1713. }
  1714. if (size < 4)
  1715. goto fail;
  1716. flags = bytestream_get_le32(&data);
  1717. size -= 4;
  1718. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1719. if (size < 4)
  1720. goto fail;
  1721. val = bytestream_get_le32(&data);
  1722. if (val <= 0 || val > INT_MAX) {
  1723. av_log(avctx, AV_LOG_ERROR, "Invalid channel count");
  1724. return AVERROR_INVALIDDATA;
  1725. }
  1726. avctx->channels = val;
  1727. size -= 4;
  1728. }
  1729. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1730. if (size < 8)
  1731. goto fail;
  1732. avctx->channel_layout = bytestream_get_le64(&data);
  1733. size -= 8;
  1734. }
  1735. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1736. if (size < 4)
  1737. goto fail;
  1738. val = bytestream_get_le32(&data);
  1739. if (val <= 0 || val > INT_MAX) {
  1740. av_log(avctx, AV_LOG_ERROR, "Invalid sample rate");
  1741. return AVERROR_INVALIDDATA;
  1742. }
  1743. avctx->sample_rate = val;
  1744. size -= 4;
  1745. }
  1746. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1747. if (size < 8)
  1748. goto fail;
  1749. avctx->width = bytestream_get_le32(&data);
  1750. avctx->height = bytestream_get_le32(&data);
  1751. size -= 8;
  1752. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1753. if (ret < 0)
  1754. return ret;
  1755. }
  1756. return 0;
  1757. fail:
  1758. av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
  1759. return AVERROR_INVALIDDATA;
  1760. }
  1761. static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
  1762. {
  1763. int ret;
  1764. /* move the original frame to our backup */
  1765. av_frame_unref(avci->to_free);
  1766. av_frame_move_ref(avci->to_free, frame);
  1767. /* now copy everything except the AVBufferRefs back
  1768. * note that we make a COPY of the side data, so calling av_frame_free() on
  1769. * the caller's frame will work properly */
  1770. ret = av_frame_copy_props(frame, avci->to_free);
  1771. if (ret < 0)
  1772. return ret;
  1773. memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
  1774. memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
  1775. if (avci->to_free->extended_data != avci->to_free->data) {
  1776. int planes = av_frame_get_channels(avci->to_free);
  1777. int size = planes * sizeof(*frame->extended_data);
  1778. if (!size) {
  1779. av_frame_unref(frame);
  1780. return AVERROR_BUG;
  1781. }
  1782. frame->extended_data = av_malloc(size);
  1783. if (!frame->extended_data) {
  1784. av_frame_unref(frame);
  1785. return AVERROR(ENOMEM);
  1786. }
  1787. memcpy(frame->extended_data, avci->to_free->extended_data,
  1788. size);
  1789. } else
  1790. frame->extended_data = frame->data;
  1791. frame->format = avci->to_free->format;
  1792. frame->width = avci->to_free->width;
  1793. frame->height = avci->to_free->height;
  1794. frame->channel_layout = avci->to_free->channel_layout;
  1795. frame->nb_samples = avci->to_free->nb_samples;
  1796. av_frame_set_channels(frame, av_frame_get_channels(avci->to_free));
  1797. return 0;
  1798. }
  1799. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1800. int *got_picture_ptr,
  1801. const AVPacket *avpkt)
  1802. {
  1803. AVCodecInternal *avci = avctx->internal;
  1804. int ret;
  1805. // copy to ensure we do not change avpkt
  1806. AVPacket tmp = *avpkt;
  1807. if (!avctx->codec)
  1808. return AVERROR(EINVAL);
  1809. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1810. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1811. return AVERROR(EINVAL);
  1812. }
  1813. *got_picture_ptr = 0;
  1814. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1815. return AVERROR(EINVAL);
  1816. av_frame_unref(picture);
  1817. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size ||
  1818. (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1819. int did_split = av_packet_split_side_data(&tmp);
  1820. ret = apply_param_change(avctx, &tmp);
  1821. if (ret < 0) {
  1822. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1823. if (avctx->err_recognition & AV_EF_EXPLODE)
  1824. goto fail;
  1825. }
  1826. avctx->internal->pkt = &tmp;
  1827. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1828. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1829. &tmp);
  1830. else {
  1831. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1832. &tmp);
  1833. if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
  1834. picture->pkt_dts = avpkt->dts;
  1835. if(!avctx->has_b_frames){
  1836. av_frame_set_pkt_pos(picture, avpkt->pos);
  1837. }
  1838. //FIXME these should be under if(!avctx->has_b_frames)
  1839. /* get_buffer is supposed to set frame parameters */
  1840. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
  1841. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1842. if (!picture->width) picture->width = avctx->width;
  1843. if (!picture->height) picture->height = avctx->height;
  1844. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  1845. }
  1846. }
  1847. fail:
  1848. emms_c(); //needed to avoid an emms_c() call before every return;
  1849. avctx->internal->pkt = NULL;
  1850. if (did_split) {
  1851. av_packet_free_side_data(&tmp);
  1852. if(ret == tmp.size)
  1853. ret = avpkt->size;
  1854. }
  1855. if (*got_picture_ptr) {
  1856. if (!avctx->refcounted_frames) {
  1857. int err = unrefcount_frame(avci, picture);
  1858. if (err < 0)
  1859. return err;
  1860. }
  1861. avctx->frame_number++;
  1862. av_frame_set_best_effort_timestamp(picture,
  1863. guess_correct_pts(avctx,
  1864. picture->pkt_pts,
  1865. picture->pkt_dts));
  1866. } else
  1867. av_frame_unref(picture);
  1868. } else
  1869. ret = 0;
  1870. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1871. * make sure it's set correctly */
  1872. av_assert0(!picture->extended_data || picture->extended_data == picture->data);
  1873. #if FF_API_AVCTX_TIMEBASE
  1874. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  1875. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  1876. #endif
  1877. return ret;
  1878. }
  1879. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1880. AVFrame *frame,
  1881. int *got_frame_ptr,
  1882. const AVPacket *avpkt)
  1883. {
  1884. AVCodecInternal *avci = avctx->internal;
  1885. int ret = 0;
  1886. *got_frame_ptr = 0;
  1887. if (!avpkt->data && avpkt->size) {
  1888. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1889. return AVERROR(EINVAL);
  1890. }
  1891. if (!avctx->codec)
  1892. return AVERROR(EINVAL);
  1893. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1894. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1895. return AVERROR(EINVAL);
  1896. }
  1897. av_frame_unref(frame);
  1898. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1899. uint8_t *side;
  1900. int side_size;
  1901. uint32_t discard_padding = 0;
  1902. uint8_t skip_reason = 0;
  1903. uint8_t discard_reason = 0;
  1904. // copy to ensure we do not change avpkt
  1905. AVPacket tmp = *avpkt;
  1906. int did_split = av_packet_split_side_data(&tmp);
  1907. ret = apply_param_change(avctx, &tmp);
  1908. if (ret < 0) {
  1909. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1910. if (avctx->err_recognition & AV_EF_EXPLODE)
  1911. goto fail;
  1912. }
  1913. avctx->internal->pkt = &tmp;
  1914. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1915. ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
  1916. else {
  1917. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1918. av_assert0(ret <= tmp.size);
  1919. frame->pkt_dts = avpkt->dts;
  1920. }
  1921. if (ret >= 0 && *got_frame_ptr) {
  1922. avctx->frame_number++;
  1923. av_frame_set_best_effort_timestamp(frame,
  1924. guess_correct_pts(avctx,
  1925. frame->pkt_pts,
  1926. frame->pkt_dts));
  1927. if (frame->format == AV_SAMPLE_FMT_NONE)
  1928. frame->format = avctx->sample_fmt;
  1929. if (!frame->channel_layout)
  1930. frame->channel_layout = avctx->channel_layout;
  1931. if (!av_frame_get_channels(frame))
  1932. av_frame_set_channels(frame, avctx->channels);
  1933. if (!frame->sample_rate)
  1934. frame->sample_rate = avctx->sample_rate;
  1935. }
  1936. side= av_packet_get_side_data(avctx->internal->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  1937. if(side && side_size>=10) {
  1938. avctx->internal->skip_samples = AV_RL32(side);
  1939. discard_padding = AV_RL32(side + 4);
  1940. av_log(avctx, AV_LOG_DEBUG, "skip %d / discard %d samples due to side data\n",
  1941. avctx->internal->skip_samples, (int)discard_padding);
  1942. skip_reason = AV_RL8(side + 8);
  1943. discard_reason = AV_RL8(side + 9);
  1944. }
  1945. if (avctx->internal->skip_samples && *got_frame_ptr &&
  1946. !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
  1947. if(frame->nb_samples <= avctx->internal->skip_samples){
  1948. *got_frame_ptr = 0;
  1949. avctx->internal->skip_samples -= frame->nb_samples;
  1950. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  1951. avctx->internal->skip_samples);
  1952. } else {
  1953. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  1954. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  1955. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1956. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  1957. (AVRational){1, avctx->sample_rate},
  1958. avctx->pkt_timebase);
  1959. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  1960. frame->pkt_pts += diff_ts;
  1961. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  1962. frame->pkt_dts += diff_ts;
  1963. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  1964. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  1965. } else {
  1966. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  1967. }
  1968. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  1969. avctx->internal->skip_samples, frame->nb_samples);
  1970. frame->nb_samples -= avctx->internal->skip_samples;
  1971. avctx->internal->skip_samples = 0;
  1972. }
  1973. }
  1974. if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr &&
  1975. !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
  1976. if (discard_padding == frame->nb_samples) {
  1977. *got_frame_ptr = 0;
  1978. } else {
  1979. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1980. int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
  1981. (AVRational){1, avctx->sample_rate},
  1982. avctx->pkt_timebase);
  1983. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  1984. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  1985. } else {
  1986. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
  1987. }
  1988. av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
  1989. (int)discard_padding, frame->nb_samples);
  1990. frame->nb_samples -= discard_padding;
  1991. }
  1992. }
  1993. if ((avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL) && *got_frame_ptr) {
  1994. AVFrameSideData *fside = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
  1995. if (fside) {
  1996. AV_WL32(fside->data, avctx->internal->skip_samples);
  1997. AV_WL32(fside->data + 4, discard_padding);
  1998. AV_WL8(fside->data + 8, skip_reason);
  1999. AV_WL8(fside->data + 9, discard_reason);
  2000. avctx->internal->skip_samples = 0;
  2001. }
  2002. }
  2003. fail:
  2004. avctx->internal->pkt = NULL;
  2005. if (did_split) {
  2006. av_packet_free_side_data(&tmp);
  2007. if(ret == tmp.size)
  2008. ret = avpkt->size;
  2009. }
  2010. if (ret >= 0 && *got_frame_ptr) {
  2011. if (!avctx->refcounted_frames) {
  2012. int err = unrefcount_frame(avci, frame);
  2013. if (err < 0)
  2014. return err;
  2015. }
  2016. } else
  2017. av_frame_unref(frame);
  2018. }
  2019. return ret;
  2020. }
  2021. #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
  2022. static int recode_subtitle(AVCodecContext *avctx,
  2023. AVPacket *outpkt, const AVPacket *inpkt)
  2024. {
  2025. #if CONFIG_ICONV
  2026. iconv_t cd = (iconv_t)-1;
  2027. int ret = 0;
  2028. char *inb, *outb;
  2029. size_t inl, outl;
  2030. AVPacket tmp;
  2031. #endif
  2032. if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
  2033. return 0;
  2034. #if CONFIG_ICONV
  2035. cd = iconv_open("UTF-8", avctx->sub_charenc);
  2036. av_assert0(cd != (iconv_t)-1);
  2037. inb = inpkt->data;
  2038. inl = inpkt->size;
  2039. if (inl >= INT_MAX / UTF8_MAX_BYTES - AV_INPUT_BUFFER_PADDING_SIZE) {
  2040. av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
  2041. ret = AVERROR(ENOMEM);
  2042. goto end;
  2043. }
  2044. ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
  2045. if (ret < 0)
  2046. goto end;
  2047. outpkt->buf = tmp.buf;
  2048. outpkt->data = tmp.data;
  2049. outpkt->size = tmp.size;
  2050. outb = outpkt->data;
  2051. outl = outpkt->size;
  2052. if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
  2053. iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
  2054. outl >= outpkt->size || inl != 0) {
  2055. ret = FFMIN(AVERROR(errno), -1);
  2056. av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
  2057. "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
  2058. av_packet_unref(&tmp);
  2059. goto end;
  2060. }
  2061. outpkt->size -= outl;
  2062. memset(outpkt->data + outpkt->size, 0, outl);
  2063. end:
  2064. if (cd != (iconv_t)-1)
  2065. iconv_close(cd);
  2066. return ret;
  2067. #else
  2068. av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
  2069. return AVERROR(EINVAL);
  2070. #endif
  2071. }
  2072. static int utf8_check(const uint8_t *str)
  2073. {
  2074. const uint8_t *byte;
  2075. uint32_t codepoint, min;
  2076. while (*str) {
  2077. byte = str;
  2078. GET_UTF8(codepoint, *(byte++), return 0;);
  2079. min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
  2080. 1 << (5 * (byte - str) - 4);
  2081. if (codepoint < min || codepoint >= 0x110000 ||
  2082. codepoint == 0xFFFE /* BOM */ ||
  2083. codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
  2084. return 0;
  2085. str = byte;
  2086. }
  2087. return 1;
  2088. }
  2089. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  2090. int *got_sub_ptr,
  2091. AVPacket *avpkt)
  2092. {
  2093. int i, ret = 0;
  2094. if (!avpkt->data && avpkt->size) {
  2095. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  2096. return AVERROR(EINVAL);
  2097. }
  2098. if (!avctx->codec)
  2099. return AVERROR(EINVAL);
  2100. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  2101. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  2102. return AVERROR(EINVAL);
  2103. }
  2104. *got_sub_ptr = 0;
  2105. get_subtitle_defaults(sub);
  2106. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
  2107. AVPacket pkt_recoded;
  2108. AVPacket tmp = *avpkt;
  2109. int did_split = av_packet_split_side_data(&tmp);
  2110. //apply_param_change(avctx, &tmp);
  2111. if (did_split) {
  2112. /* FFMIN() prevents overflow in case the packet wasn't allocated with
  2113. * proper padding.
  2114. * If the side data is smaller than the buffer padding size, the
  2115. * remaining bytes should have already been filled with zeros by the
  2116. * original packet allocation anyway. */
  2117. memset(tmp.data + tmp.size, 0,
  2118. FFMIN(avpkt->size - tmp.size, AV_INPUT_BUFFER_PADDING_SIZE));
  2119. }
  2120. pkt_recoded = tmp;
  2121. ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
  2122. if (ret < 0) {
  2123. *got_sub_ptr = 0;
  2124. } else {
  2125. avctx->internal->pkt = &pkt_recoded;
  2126. if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  2127. sub->pts = av_rescale_q(avpkt->pts,
  2128. avctx->pkt_timebase, AV_TIME_BASE_Q);
  2129. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
  2130. av_assert1((ret >= 0) >= !!*got_sub_ptr &&
  2131. !!*got_sub_ptr >= !!sub->num_rects);
  2132. if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
  2133. avctx->pkt_timebase.num) {
  2134. AVRational ms = { 1, 1000 };
  2135. sub->end_display_time = av_rescale_q(avpkt->duration,
  2136. avctx->pkt_timebase, ms);
  2137. }
  2138. for (i = 0; i < sub->num_rects; i++) {
  2139. if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
  2140. av_log(avctx, AV_LOG_ERROR,
  2141. "Invalid UTF-8 in decoded subtitles text; "
  2142. "maybe missing -sub_charenc option\n");
  2143. avsubtitle_free(sub);
  2144. return AVERROR_INVALIDDATA;
  2145. }
  2146. }
  2147. if (tmp.data != pkt_recoded.data) { // did we recode?
  2148. /* prevent from destroying side data from original packet */
  2149. pkt_recoded.side_data = NULL;
  2150. pkt_recoded.side_data_elems = 0;
  2151. av_packet_unref(&pkt_recoded);
  2152. }
  2153. if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
  2154. sub->format = 0;
  2155. else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
  2156. sub->format = 1;
  2157. avctx->internal->pkt = NULL;
  2158. }
  2159. if (did_split) {
  2160. av_packet_free_side_data(&tmp);
  2161. if(ret == tmp.size)
  2162. ret = avpkt->size;
  2163. }
  2164. if (*got_sub_ptr)
  2165. avctx->frame_number++;
  2166. }
  2167. return ret;
  2168. }
  2169. void avsubtitle_free(AVSubtitle *sub)
  2170. {
  2171. int i;
  2172. for (i = 0; i < sub->num_rects; i++) {
  2173. av_freep(&sub->rects[i]->data[0]);
  2174. av_freep(&sub->rects[i]->data[1]);
  2175. av_freep(&sub->rects[i]->data[2]);
  2176. av_freep(&sub->rects[i]->data[3]);
  2177. av_freep(&sub->rects[i]->text);
  2178. av_freep(&sub->rects[i]->ass);
  2179. av_freep(&sub->rects[i]);
  2180. }
  2181. av_freep(&sub->rects);
  2182. memset(sub, 0, sizeof(AVSubtitle));
  2183. }
  2184. av_cold int avcodec_close(AVCodecContext *avctx)
  2185. {
  2186. int i;
  2187. if (!avctx)
  2188. return 0;
  2189. if (avcodec_is_open(avctx)) {
  2190. FramePool *pool = avctx->internal->pool;
  2191. if (CONFIG_FRAME_THREAD_ENCODER &&
  2192. avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  2193. ff_frame_thread_encoder_free(avctx);
  2194. }
  2195. if (HAVE_THREADS && avctx->internal->thread_ctx)
  2196. ff_thread_free(avctx);
  2197. if (avctx->codec && avctx->codec->close)
  2198. avctx->codec->close(avctx);
  2199. avctx->internal->byte_buffer_size = 0;
  2200. av_freep(&avctx->internal->byte_buffer);
  2201. av_frame_free(&avctx->internal->to_free);
  2202. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  2203. av_buffer_pool_uninit(&pool->pools[i]);
  2204. av_freep(&avctx->internal->pool);
  2205. if (avctx->hwaccel && avctx->hwaccel->uninit)
  2206. avctx->hwaccel->uninit(avctx);
  2207. av_freep(&avctx->internal->hwaccel_priv_data);
  2208. av_freep(&avctx->internal);
  2209. }
  2210. for (i = 0; i < avctx->nb_coded_side_data; i++)
  2211. av_freep(&avctx->coded_side_data[i].data);
  2212. av_freep(&avctx->coded_side_data);
  2213. avctx->nb_coded_side_data = 0;
  2214. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  2215. av_opt_free(avctx->priv_data);
  2216. av_opt_free(avctx);
  2217. av_freep(&avctx->priv_data);
  2218. if (av_codec_is_encoder(avctx->codec)) {
  2219. av_freep(&avctx->extradata);
  2220. #if FF_API_CODED_FRAME
  2221. FF_DISABLE_DEPRECATION_WARNINGS
  2222. av_frame_free(&avctx->coded_frame);
  2223. FF_ENABLE_DEPRECATION_WARNINGS
  2224. #endif
  2225. }
  2226. avctx->codec = NULL;
  2227. avctx->active_thread_type = 0;
  2228. return 0;
  2229. }
  2230. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  2231. {
  2232. switch(id){
  2233. //This is for future deprecatec codec ids, its empty since
  2234. //last major bump but will fill up again over time, please don't remove it
  2235. default : return id;
  2236. }
  2237. }
  2238. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  2239. {
  2240. AVCodec *p, *experimental = NULL;
  2241. p = first_avcodec;
  2242. id= remap_deprecated_codec_id(id);
  2243. while (p) {
  2244. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  2245. p->id == id) {
  2246. if (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL && !experimental) {
  2247. experimental = p;
  2248. } else
  2249. return p;
  2250. }
  2251. p = p->next;
  2252. }
  2253. return experimental;
  2254. }
  2255. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  2256. {
  2257. return find_encdec(id, 1);
  2258. }
  2259. AVCodec *avcodec_find_encoder_by_name(const char *name)
  2260. {
  2261. AVCodec *p;
  2262. if (!name)
  2263. return NULL;
  2264. p = first_avcodec;
  2265. while (p) {
  2266. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  2267. return p;
  2268. p = p->next;
  2269. }
  2270. return NULL;
  2271. }
  2272. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  2273. {
  2274. return find_encdec(id, 0);
  2275. }
  2276. AVCodec *avcodec_find_decoder_by_name(const char *name)
  2277. {
  2278. AVCodec *p;
  2279. if (!name)
  2280. return NULL;
  2281. p = first_avcodec;
  2282. while (p) {
  2283. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  2284. return p;
  2285. p = p->next;
  2286. }
  2287. return NULL;
  2288. }
  2289. const char *avcodec_get_name(enum AVCodecID id)
  2290. {
  2291. const AVCodecDescriptor *cd;
  2292. AVCodec *codec;
  2293. if (id == AV_CODEC_ID_NONE)
  2294. return "none";
  2295. cd = avcodec_descriptor_get(id);
  2296. if (cd)
  2297. return cd->name;
  2298. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  2299. codec = avcodec_find_decoder(id);
  2300. if (codec)
  2301. return codec->name;
  2302. codec = avcodec_find_encoder(id);
  2303. if (codec)
  2304. return codec->name;
  2305. return "unknown_codec";
  2306. }
  2307. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  2308. {
  2309. int i, len, ret = 0;
  2310. #define TAG_PRINT(x) \
  2311. (((x) >= '0' && (x) <= '9') || \
  2312. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  2313. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  2314. for (i = 0; i < 4; i++) {
  2315. len = snprintf(buf, buf_size,
  2316. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  2317. buf += len;
  2318. buf_size = buf_size > len ? buf_size - len : 0;
  2319. ret += len;
  2320. codec_tag >>= 8;
  2321. }
  2322. return ret;
  2323. }
  2324. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  2325. {
  2326. const char *codec_type;
  2327. const char *codec_name;
  2328. const char *profile = NULL;
  2329. const AVCodec *p;
  2330. int64_t bitrate;
  2331. int new_line = 0;
  2332. AVRational display_aspect_ratio;
  2333. const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
  2334. if (!buf || buf_size <= 0)
  2335. return;
  2336. codec_type = av_get_media_type_string(enc->codec_type);
  2337. codec_name = avcodec_get_name(enc->codec_id);
  2338. if (enc->profile != FF_PROFILE_UNKNOWN) {
  2339. if (enc->codec)
  2340. p = enc->codec;
  2341. else
  2342. p = encode ? avcodec_find_encoder(enc->codec_id) :
  2343. avcodec_find_decoder(enc->codec_id);
  2344. if (p)
  2345. profile = av_get_profile_name(p, enc->profile);
  2346. }
  2347. snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
  2348. codec_name);
  2349. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  2350. if (enc->codec && strcmp(enc->codec->name, codec_name))
  2351. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
  2352. if (profile)
  2353. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  2354. if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
  2355. && av_log_get_level() >= AV_LOG_VERBOSE
  2356. && enc->refs)
  2357. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2358. ", %d reference frame%s",
  2359. enc->refs, enc->refs > 1 ? "s" : "");
  2360. if (enc->codec_tag) {
  2361. char tag_buf[32];
  2362. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  2363. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2364. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  2365. }
  2366. switch (enc->codec_type) {
  2367. case AVMEDIA_TYPE_VIDEO:
  2368. {
  2369. char detail[256] = "(";
  2370. av_strlcat(buf, separator, buf_size);
  2371. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2372. "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
  2373. av_get_pix_fmt_name(enc->pix_fmt));
  2374. if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
  2375. enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
  2376. av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
  2377. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  2378. av_strlcatf(detail, sizeof(detail), "%s, ",
  2379. av_color_range_name(enc->color_range));
  2380. if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
  2381. enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  2382. enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
  2383. if (enc->colorspace != (int)enc->color_primaries ||
  2384. enc->colorspace != (int)enc->color_trc) {
  2385. new_line = 1;
  2386. av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
  2387. av_color_space_name(enc->colorspace),
  2388. av_color_primaries_name(enc->color_primaries),
  2389. av_color_transfer_name(enc->color_trc));
  2390. } else
  2391. av_strlcatf(detail, sizeof(detail), "%s, ",
  2392. av_get_colorspace_name(enc->colorspace));
  2393. }
  2394. if (av_log_get_level() >= AV_LOG_DEBUG &&
  2395. enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
  2396. av_strlcatf(detail, sizeof(detail), "%s, ",
  2397. av_chroma_location_name(enc->chroma_sample_location));
  2398. if (strlen(detail) > 1) {
  2399. detail[strlen(detail) - 2] = 0;
  2400. av_strlcatf(buf, buf_size, "%s)", detail);
  2401. }
  2402. }
  2403. if (enc->width) {
  2404. av_strlcat(buf, new_line ? separator : ", ", buf_size);
  2405. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2406. "%dx%d",
  2407. enc->width, enc->height);
  2408. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  2409. (enc->width != enc->coded_width ||
  2410. enc->height != enc->coded_height))
  2411. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2412. " (%dx%d)", enc->coded_width, enc->coded_height);
  2413. if (enc->sample_aspect_ratio.num) {
  2414. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  2415. enc->width * (int64_t)enc->sample_aspect_ratio.num,
  2416. enc->height * (int64_t)enc->sample_aspect_ratio.den,
  2417. 1024 * 1024);
  2418. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2419. " [SAR %d:%d DAR %d:%d]",
  2420. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  2421. display_aspect_ratio.num, display_aspect_ratio.den);
  2422. }
  2423. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2424. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2425. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2426. ", %d/%d",
  2427. enc->time_base.num / g, enc->time_base.den / g);
  2428. }
  2429. }
  2430. if (encode) {
  2431. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2432. ", q=%d-%d", enc->qmin, enc->qmax);
  2433. } else {
  2434. if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
  2435. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2436. ", Closed Captions");
  2437. if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
  2438. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2439. ", lossless");
  2440. }
  2441. break;
  2442. case AVMEDIA_TYPE_AUDIO:
  2443. av_strlcat(buf, separator, buf_size);
  2444. if (enc->sample_rate) {
  2445. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2446. "%d Hz, ", enc->sample_rate);
  2447. }
  2448. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  2449. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  2450. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2451. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  2452. }
  2453. if ( enc->bits_per_raw_sample > 0
  2454. && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
  2455. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2456. " (%d bit)", enc->bits_per_raw_sample);
  2457. break;
  2458. case AVMEDIA_TYPE_DATA:
  2459. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2460. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2461. if (g)
  2462. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2463. ", %d/%d",
  2464. enc->time_base.num / g, enc->time_base.den / g);
  2465. }
  2466. break;
  2467. case AVMEDIA_TYPE_SUBTITLE:
  2468. if (enc->width)
  2469. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2470. ", %dx%d", enc->width, enc->height);
  2471. break;
  2472. default:
  2473. return;
  2474. }
  2475. if (encode) {
  2476. if (enc->flags & AV_CODEC_FLAG_PASS1)
  2477. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2478. ", pass 1");
  2479. if (enc->flags & AV_CODEC_FLAG_PASS2)
  2480. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2481. ", pass 2");
  2482. }
  2483. bitrate = get_bit_rate(enc);
  2484. if (bitrate != 0) {
  2485. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2486. ", %"PRId64" kb/s", bitrate / 1000);
  2487. } else if (enc->rc_max_rate > 0) {
  2488. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2489. ", max. %"PRId64" kb/s", (int64_t)enc->rc_max_rate / 1000);
  2490. }
  2491. }
  2492. const char *av_get_profile_name(const AVCodec *codec, int profile)
  2493. {
  2494. const AVProfile *p;
  2495. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  2496. return NULL;
  2497. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  2498. if (p->profile == profile)
  2499. return p->name;
  2500. return NULL;
  2501. }
  2502. unsigned avcodec_version(void)
  2503. {
  2504. // av_assert0(AV_CODEC_ID_V410==164);
  2505. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  2506. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  2507. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  2508. av_assert0(AV_CODEC_ID_SRT==94216);
  2509. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  2510. return LIBAVCODEC_VERSION_INT;
  2511. }
  2512. const char *avcodec_configuration(void)
  2513. {
  2514. return FFMPEG_CONFIGURATION;
  2515. }
  2516. const char *avcodec_license(void)
  2517. {
  2518. #define LICENSE_PREFIX "libavcodec license: "
  2519. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  2520. }
  2521. void avcodec_flush_buffers(AVCodecContext *avctx)
  2522. {
  2523. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2524. ff_thread_flush(avctx);
  2525. else if (avctx->codec->flush)
  2526. avctx->codec->flush(avctx);
  2527. avctx->pts_correction_last_pts =
  2528. avctx->pts_correction_last_dts = INT64_MIN;
  2529. if (!avctx->refcounted_frames)
  2530. av_frame_unref(avctx->internal->to_free);
  2531. }
  2532. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  2533. {
  2534. switch (codec_id) {
  2535. case AV_CODEC_ID_8SVX_EXP:
  2536. case AV_CODEC_ID_8SVX_FIB:
  2537. case AV_CODEC_ID_ADPCM_CT:
  2538. case AV_CODEC_ID_ADPCM_IMA_APC:
  2539. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  2540. case AV_CODEC_ID_ADPCM_IMA_OKI:
  2541. case AV_CODEC_ID_ADPCM_IMA_WS:
  2542. case AV_CODEC_ID_ADPCM_G722:
  2543. case AV_CODEC_ID_ADPCM_YAMAHA:
  2544. case AV_CODEC_ID_ADPCM_AICA:
  2545. return 4;
  2546. case AV_CODEC_ID_DSD_LSBF:
  2547. case AV_CODEC_ID_DSD_MSBF:
  2548. case AV_CODEC_ID_DSD_LSBF_PLANAR:
  2549. case AV_CODEC_ID_DSD_MSBF_PLANAR:
  2550. case AV_CODEC_ID_PCM_ALAW:
  2551. case AV_CODEC_ID_PCM_MULAW:
  2552. case AV_CODEC_ID_PCM_S8:
  2553. case AV_CODEC_ID_PCM_S8_PLANAR:
  2554. case AV_CODEC_ID_PCM_U8:
  2555. case AV_CODEC_ID_PCM_ZORK:
  2556. case AV_CODEC_ID_SDX2_DPCM:
  2557. return 8;
  2558. case AV_CODEC_ID_PCM_S16BE:
  2559. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  2560. case AV_CODEC_ID_PCM_S16LE:
  2561. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  2562. case AV_CODEC_ID_PCM_U16BE:
  2563. case AV_CODEC_ID_PCM_U16LE:
  2564. return 16;
  2565. case AV_CODEC_ID_PCM_S24DAUD:
  2566. case AV_CODEC_ID_PCM_S24BE:
  2567. case AV_CODEC_ID_PCM_S24LE:
  2568. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  2569. case AV_CODEC_ID_PCM_U24BE:
  2570. case AV_CODEC_ID_PCM_U24LE:
  2571. return 24;
  2572. case AV_CODEC_ID_PCM_S32BE:
  2573. case AV_CODEC_ID_PCM_S32LE:
  2574. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  2575. case AV_CODEC_ID_PCM_U32BE:
  2576. case AV_CODEC_ID_PCM_U32LE:
  2577. case AV_CODEC_ID_PCM_F32BE:
  2578. case AV_CODEC_ID_PCM_F32LE:
  2579. return 32;
  2580. case AV_CODEC_ID_PCM_F64BE:
  2581. case AV_CODEC_ID_PCM_F64LE:
  2582. return 64;
  2583. default:
  2584. return 0;
  2585. }
  2586. }
  2587. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  2588. {
  2589. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  2590. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2591. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2592. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2593. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2594. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2595. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2596. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2597. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2598. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2599. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2600. };
  2601. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  2602. return AV_CODEC_ID_NONE;
  2603. if (be < 0 || be > 1)
  2604. be = AV_NE(1, 0);
  2605. return map[fmt][be];
  2606. }
  2607. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2608. {
  2609. switch (codec_id) {
  2610. case AV_CODEC_ID_ADPCM_SBPRO_2:
  2611. return 2;
  2612. case AV_CODEC_ID_ADPCM_SBPRO_3:
  2613. return 3;
  2614. case AV_CODEC_ID_ADPCM_SBPRO_4:
  2615. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2616. case AV_CODEC_ID_ADPCM_IMA_QT:
  2617. case AV_CODEC_ID_ADPCM_SWF:
  2618. case AV_CODEC_ID_ADPCM_MS:
  2619. return 4;
  2620. default:
  2621. return av_get_exact_bits_per_sample(codec_id);
  2622. }
  2623. }
  2624. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2625. {
  2626. int id, sr, ch, ba, tag, bps;
  2627. id = avctx->codec_id;
  2628. sr = avctx->sample_rate;
  2629. ch = avctx->channels;
  2630. ba = avctx->block_align;
  2631. tag = avctx->codec_tag;
  2632. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  2633. /* codecs with an exact constant bits per sample */
  2634. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  2635. return (frame_bytes * 8LL) / (bps * ch);
  2636. bps = avctx->bits_per_coded_sample;
  2637. /* codecs with a fixed packet duration */
  2638. switch (id) {
  2639. case AV_CODEC_ID_ADPCM_ADX: return 32;
  2640. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  2641. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  2642. case AV_CODEC_ID_AMR_NB:
  2643. case AV_CODEC_ID_EVRC:
  2644. case AV_CODEC_ID_GSM:
  2645. case AV_CODEC_ID_QCELP:
  2646. case AV_CODEC_ID_RA_288: return 160;
  2647. case AV_CODEC_ID_AMR_WB:
  2648. case AV_CODEC_ID_GSM_MS: return 320;
  2649. case AV_CODEC_ID_MP1: return 384;
  2650. case AV_CODEC_ID_ATRAC1: return 512;
  2651. case AV_CODEC_ID_ATRAC3: return 1024;
  2652. case AV_CODEC_ID_ATRAC3P: return 2048;
  2653. case AV_CODEC_ID_MP2:
  2654. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2655. case AV_CODEC_ID_AC3: return 1536;
  2656. }
  2657. if (sr > 0) {
  2658. /* calc from sample rate */
  2659. if (id == AV_CODEC_ID_TTA)
  2660. return 256 * sr / 245;
  2661. if (ch > 0) {
  2662. /* calc from sample rate and channels */
  2663. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2664. return (480 << (sr / 22050)) / ch;
  2665. }
  2666. }
  2667. if (ba > 0) {
  2668. /* calc from block_align */
  2669. if (id == AV_CODEC_ID_SIPR) {
  2670. switch (ba) {
  2671. case 20: return 160;
  2672. case 19: return 144;
  2673. case 29: return 288;
  2674. case 37: return 480;
  2675. }
  2676. } else if (id == AV_CODEC_ID_ILBC) {
  2677. switch (ba) {
  2678. case 38: return 160;
  2679. case 50: return 240;
  2680. }
  2681. }
  2682. }
  2683. if (frame_bytes > 0) {
  2684. /* calc from frame_bytes only */
  2685. if (id == AV_CODEC_ID_TRUESPEECH)
  2686. return 240 * (frame_bytes / 32);
  2687. if (id == AV_CODEC_ID_NELLYMOSER)
  2688. return 256 * (frame_bytes / 64);
  2689. if (id == AV_CODEC_ID_RA_144)
  2690. return 160 * (frame_bytes / 20);
  2691. if (id == AV_CODEC_ID_G723_1)
  2692. return 240 * (frame_bytes / 24);
  2693. if (bps > 0) {
  2694. /* calc from frame_bytes and bits_per_coded_sample */
  2695. if (id == AV_CODEC_ID_ADPCM_G726)
  2696. return frame_bytes * 8 / bps;
  2697. }
  2698. if (ch > 0 && ch < INT_MAX/16) {
  2699. /* calc from frame_bytes and channels */
  2700. switch (id) {
  2701. case AV_CODEC_ID_ADPCM_AFC:
  2702. return frame_bytes / (9 * ch) * 16;
  2703. case AV_CODEC_ID_ADPCM_PSX:
  2704. case AV_CODEC_ID_ADPCM_DTK:
  2705. return frame_bytes / (16 * ch) * 28;
  2706. case AV_CODEC_ID_ADPCM_4XM:
  2707. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2708. return (frame_bytes - 4 * ch) * 2 / ch;
  2709. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2710. return (frame_bytes - 4) * 2 / ch;
  2711. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2712. return (frame_bytes - 8) * 2 / ch;
  2713. case AV_CODEC_ID_ADPCM_THP:
  2714. case AV_CODEC_ID_ADPCM_THP_LE:
  2715. if (avctx->extradata)
  2716. return frame_bytes * 14 / (8 * ch);
  2717. break;
  2718. case AV_CODEC_ID_ADPCM_XA:
  2719. return (frame_bytes / 128) * 224 / ch;
  2720. case AV_CODEC_ID_INTERPLAY_DPCM:
  2721. return (frame_bytes - 6 - ch) / ch;
  2722. case AV_CODEC_ID_ROQ_DPCM:
  2723. return (frame_bytes - 8) / ch;
  2724. case AV_CODEC_ID_XAN_DPCM:
  2725. return (frame_bytes - 2 * ch) / ch;
  2726. case AV_CODEC_ID_MACE3:
  2727. return 3 * frame_bytes / ch;
  2728. case AV_CODEC_ID_MACE6:
  2729. return 6 * frame_bytes / ch;
  2730. case AV_CODEC_ID_PCM_LXF:
  2731. return 2 * (frame_bytes / (5 * ch));
  2732. case AV_CODEC_ID_IAC:
  2733. case AV_CODEC_ID_IMC:
  2734. return 4 * frame_bytes / ch;
  2735. }
  2736. if (tag) {
  2737. /* calc from frame_bytes, channels, and codec_tag */
  2738. if (id == AV_CODEC_ID_SOL_DPCM) {
  2739. if (tag == 3)
  2740. return frame_bytes / ch;
  2741. else
  2742. return frame_bytes * 2 / ch;
  2743. }
  2744. }
  2745. if (ba > 0) {
  2746. /* calc from frame_bytes, channels, and block_align */
  2747. int blocks = frame_bytes / ba;
  2748. switch (avctx->codec_id) {
  2749. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2750. if (bps < 2 || bps > 5)
  2751. return 0;
  2752. return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
  2753. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2754. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2755. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2756. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2757. case AV_CODEC_ID_ADPCM_IMA_RAD:
  2758. return blocks * ((ba - 4 * ch) * 2 / ch);
  2759. case AV_CODEC_ID_ADPCM_MS:
  2760. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2761. }
  2762. }
  2763. if (bps > 0) {
  2764. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2765. switch (avctx->codec_id) {
  2766. case AV_CODEC_ID_PCM_DVD:
  2767. if(bps<4)
  2768. return 0;
  2769. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2770. case AV_CODEC_ID_PCM_BLURAY:
  2771. if(bps<4)
  2772. return 0;
  2773. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2774. case AV_CODEC_ID_S302M:
  2775. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2776. }
  2777. }
  2778. }
  2779. }
  2780. /* Fall back on using frame_size */
  2781. if (avctx->frame_size > 1 && frame_bytes)
  2782. return avctx->frame_size;
  2783. //For WMA we currently have no other means to calculate duration thus we
  2784. //do it here by assuming CBR, which is true for all known cases.
  2785. if (avctx->bit_rate>0 && frame_bytes>0 && avctx->sample_rate>0 && avctx->block_align>1) {
  2786. if (avctx->codec_id == AV_CODEC_ID_WMAV1 || avctx->codec_id == AV_CODEC_ID_WMAV2)
  2787. return (frame_bytes * 8LL * avctx->sample_rate) / avctx->bit_rate;
  2788. }
  2789. return 0;
  2790. }
  2791. #if !HAVE_THREADS
  2792. int ff_thread_init(AVCodecContext *s)
  2793. {
  2794. return -1;
  2795. }
  2796. #endif
  2797. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2798. {
  2799. unsigned int n = 0;
  2800. while (v >= 0xff) {
  2801. *s++ = 0xff;
  2802. v -= 0xff;
  2803. n++;
  2804. }
  2805. *s = v;
  2806. n++;
  2807. return n;
  2808. }
  2809. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2810. {
  2811. int i;
  2812. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2813. return i;
  2814. }
  2815. #if FF_API_MISSING_SAMPLE
  2816. FF_DISABLE_DEPRECATION_WARNINGS
  2817. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2818. {
  2819. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  2820. "version to the newest one from Git. If the problem still "
  2821. "occurs, it means that your file has a feature which has not "
  2822. "been implemented.\n", feature);
  2823. if(want_sample)
  2824. av_log_ask_for_sample(avc, NULL);
  2825. }
  2826. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2827. {
  2828. va_list argument_list;
  2829. va_start(argument_list, msg);
  2830. if (msg)
  2831. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2832. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2833. "of this file to ftp://upload.ffmpeg.org/incoming/ "
  2834. "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n");
  2835. va_end(argument_list);
  2836. }
  2837. FF_ENABLE_DEPRECATION_WARNINGS
  2838. #endif /* FF_API_MISSING_SAMPLE */
  2839. static AVHWAccel *first_hwaccel = NULL;
  2840. static AVHWAccel **last_hwaccel = &first_hwaccel;
  2841. void av_register_hwaccel(AVHWAccel *hwaccel)
  2842. {
  2843. AVHWAccel **p = last_hwaccel;
  2844. hwaccel->next = NULL;
  2845. while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
  2846. p = &(*p)->next;
  2847. last_hwaccel = &hwaccel->next;
  2848. }
  2849. AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
  2850. {
  2851. return hwaccel ? hwaccel->next : first_hwaccel;
  2852. }
  2853. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2854. {
  2855. if (lockmgr_cb) {
  2856. // There is no good way to rollback a failure to destroy the
  2857. // mutex, so we ignore failures.
  2858. lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
  2859. lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
  2860. lockmgr_cb = NULL;
  2861. codec_mutex = NULL;
  2862. avformat_mutex = NULL;
  2863. }
  2864. if (cb) {
  2865. void *new_codec_mutex = NULL;
  2866. void *new_avformat_mutex = NULL;
  2867. int err;
  2868. if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
  2869. return err > 0 ? AVERROR_UNKNOWN : err;
  2870. }
  2871. if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
  2872. // Ignore failures to destroy the newly created mutex.
  2873. cb(&new_codec_mutex, AV_LOCK_DESTROY);
  2874. return err > 0 ? AVERROR_UNKNOWN : err;
  2875. }
  2876. lockmgr_cb = cb;
  2877. codec_mutex = new_codec_mutex;
  2878. avformat_mutex = new_avformat_mutex;
  2879. }
  2880. return 0;
  2881. }
  2882. int ff_lock_avcodec(AVCodecContext *log_ctx, const AVCodec *codec)
  2883. {
  2884. if (codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE || !codec->init)
  2885. return 0;
  2886. if (lockmgr_cb) {
  2887. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  2888. return -1;
  2889. }
  2890. if (avpriv_atomic_int_add_and_fetch(&entangled_thread_counter, 1) != 1) {
  2891. av_log(log_ctx, AV_LOG_ERROR,
  2892. "Insufficient thread locking. At least %d threads are "
  2893. "calling avcodec_open2() at the same time right now.\n",
  2894. entangled_thread_counter);
  2895. if (!lockmgr_cb)
  2896. av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
  2897. ff_avcodec_locked = 1;
  2898. ff_unlock_avcodec(codec);
  2899. return AVERROR(EINVAL);
  2900. }
  2901. av_assert0(!ff_avcodec_locked);
  2902. ff_avcodec_locked = 1;
  2903. return 0;
  2904. }
  2905. int ff_unlock_avcodec(const AVCodec *codec)
  2906. {
  2907. if (codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE || !codec->init)
  2908. return 0;
  2909. av_assert0(ff_avcodec_locked);
  2910. ff_avcodec_locked = 0;
  2911. avpriv_atomic_int_add_and_fetch(&entangled_thread_counter, -1);
  2912. if (lockmgr_cb) {
  2913. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
  2914. return -1;
  2915. }
  2916. return 0;
  2917. }
  2918. int avpriv_lock_avformat(void)
  2919. {
  2920. if (lockmgr_cb) {
  2921. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2922. return -1;
  2923. }
  2924. return 0;
  2925. }
  2926. int avpriv_unlock_avformat(void)
  2927. {
  2928. if (lockmgr_cb) {
  2929. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2930. return -1;
  2931. }
  2932. return 0;
  2933. }
  2934. unsigned int avpriv_toupper4(unsigned int x)
  2935. {
  2936. return av_toupper(x & 0xFF) +
  2937. (av_toupper((x >> 8) & 0xFF) << 8) +
  2938. (av_toupper((x >> 16) & 0xFF) << 16) +
  2939. ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
  2940. }
  2941. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  2942. {
  2943. int ret;
  2944. dst->owner = src->owner;
  2945. ret = av_frame_ref(dst->f, src->f);
  2946. if (ret < 0)
  2947. return ret;
  2948. av_assert0(!dst->progress);
  2949. if (src->progress &&
  2950. !(dst->progress = av_buffer_ref(src->progress))) {
  2951. ff_thread_release_buffer(dst->owner, dst);
  2952. return AVERROR(ENOMEM);
  2953. }
  2954. return 0;
  2955. }
  2956. #if !HAVE_THREADS
  2957. enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  2958. {
  2959. return ff_get_format(avctx, fmt);
  2960. }
  2961. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  2962. {
  2963. f->owner = avctx;
  2964. return ff_get_buffer(avctx, f->f, flags);
  2965. }
  2966. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  2967. {
  2968. if (f->f)
  2969. av_frame_unref(f->f);
  2970. }
  2971. void ff_thread_finish_setup(AVCodecContext *avctx)
  2972. {
  2973. }
  2974. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  2975. {
  2976. }
  2977. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  2978. {
  2979. }
  2980. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2981. {
  2982. return 1;
  2983. }
  2984. int ff_alloc_entries(AVCodecContext *avctx, int count)
  2985. {
  2986. return 0;
  2987. }
  2988. void ff_reset_entries(AVCodecContext *avctx)
  2989. {
  2990. }
  2991. void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
  2992. {
  2993. }
  2994. void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
  2995. {
  2996. }
  2997. #endif
  2998. int avcodec_is_open(AVCodecContext *s)
  2999. {
  3000. return !!s->internal;
  3001. }
  3002. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
  3003. {
  3004. int ret;
  3005. char *str;
  3006. ret = av_bprint_finalize(buf, &str);
  3007. if (ret < 0)
  3008. return ret;
  3009. if (!av_bprint_is_complete(buf)) {
  3010. av_free(str);
  3011. return AVERROR(ENOMEM);
  3012. }
  3013. avctx->extradata = str;
  3014. /* Note: the string is NUL terminated (so extradata can be read as a
  3015. * string), but the ending character is not accounted in the size (in
  3016. * binary formats you are likely not supposed to mux that character). When
  3017. * extradata is copied, it is also padded with AV_INPUT_BUFFER_PADDING_SIZE
  3018. * zeros. */
  3019. avctx->extradata_size = buf->len;
  3020. return 0;
  3021. }
  3022. const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
  3023. const uint8_t *end,
  3024. uint32_t *av_restrict state)
  3025. {
  3026. int i;
  3027. av_assert0(p <= end);
  3028. if (p >= end)
  3029. return end;
  3030. for (i = 0; i < 3; i++) {
  3031. uint32_t tmp = *state << 8;
  3032. *state = tmp + *(p++);
  3033. if (tmp == 0x100 || p == end)
  3034. return p;
  3035. }
  3036. while (p < end) {
  3037. if (p[-1] > 1 ) p += 3;
  3038. else if (p[-2] ) p += 2;
  3039. else if (p[-3]|(p[-1]-1)) p++;
  3040. else {
  3041. p++;
  3042. break;
  3043. }
  3044. }
  3045. p = FFMIN(p, end) - 4;
  3046. *state = AV_RB32(p);
  3047. return p + 4;
  3048. }
  3049. AVCPBProperties *av_cpb_properties_alloc(size_t *size)
  3050. {
  3051. AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
  3052. if (!props)
  3053. return NULL;
  3054. if (size)
  3055. *size = sizeof(*props);
  3056. props->vbv_delay = UINT64_MAX;
  3057. return props;
  3058. }
  3059. AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
  3060. {
  3061. AVPacketSideData *tmp;
  3062. AVCPBProperties *props;
  3063. size_t size;
  3064. props = av_cpb_properties_alloc(&size);
  3065. if (!props)
  3066. return NULL;
  3067. tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
  3068. if (!tmp) {
  3069. av_freep(&props);
  3070. return NULL;
  3071. }
  3072. avctx->coded_side_data = tmp;
  3073. avctx->nb_coded_side_data++;
  3074. avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
  3075. avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
  3076. avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
  3077. return props;
  3078. }