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.

3643 lines
120KB

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