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.

3569 lines
117KB

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