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.

3682 lines
121KB

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