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.

3656 lines
120KB

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