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.

3555 lines
116KB

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