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.

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