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.

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