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.

3480 lines
114KB

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