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.

4330 lines
143KB

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