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.

3495 lines
113KB

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