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.

2720 lines
87KB

  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 "libavutil/avassert.h"
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/bprint.h"
  29. #include "libavutil/channel_layout.h"
  30. #include "libavutil/crc.h"
  31. #include "libavutil/mathematics.h"
  32. #include "libavutil/pixdesc.h"
  33. #include "libavutil/imgutils.h"
  34. #include "libavutil/samplefmt.h"
  35. #include "libavutil/dict.h"
  36. #include "libavutil/avassert.h"
  37. #include "avcodec.h"
  38. #include "dsputil.h"
  39. #include "libavutil/opt.h"
  40. #include "thread.h"
  41. #include "frame_thread_encoder.h"
  42. #include "internal.h"
  43. #include "bytestream.h"
  44. #include <stdlib.h>
  45. #include <stdarg.h>
  46. #include <limits.h>
  47. #include <float.h>
  48. volatile int ff_avcodec_locked;
  49. static int volatile entangled_thread_counter = 0;
  50. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  51. static void *codec_mutex;
  52. static void *avformat_mutex;
  53. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  54. {
  55. if (min_size < *size)
  56. return ptr;
  57. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  58. ptr = av_realloc(ptr, min_size);
  59. /* we could set this to the unmodified min_size but this is safer
  60. * if the user lost the ptr and uses NULL now
  61. */
  62. if (!ptr)
  63. min_size = 0;
  64. *size = min_size;
  65. return ptr;
  66. }
  67. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  68. {
  69. void **p = ptr;
  70. if (min_size < *size)
  71. return 0;
  72. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  73. av_free(*p);
  74. *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  75. if (!*p)
  76. min_size = 0;
  77. *size = min_size;
  78. return 1;
  79. }
  80. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  81. {
  82. ff_fast_malloc(ptr, size, min_size, 0);
  83. }
  84. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  85. {
  86. uint8_t **p = ptr;
  87. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  88. av_freep(p);
  89. *size = 0;
  90. return;
  91. }
  92. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  93. memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  94. }
  95. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  96. {
  97. uint8_t **p = ptr;
  98. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  99. av_freep(p);
  100. *size = 0;
  101. return;
  102. }
  103. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  104. memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  105. }
  106. /* encoder management */
  107. static AVCodec *first_avcodec = NULL;
  108. AVCodec *av_codec_next(const AVCodec *c)
  109. {
  110. if (c)
  111. return c->next;
  112. else
  113. return first_avcodec;
  114. }
  115. static void avcodec_init(void)
  116. {
  117. static int initialized = 0;
  118. if (initialized != 0)
  119. return;
  120. initialized = 1;
  121. ff_dsputil_static_init();
  122. }
  123. int av_codec_is_encoder(const AVCodec *codec)
  124. {
  125. return codec && (codec->encode_sub || codec->encode2);
  126. }
  127. int av_codec_is_decoder(const AVCodec *codec)
  128. {
  129. return codec && codec->decode;
  130. }
  131. void avcodec_register(AVCodec *codec)
  132. {
  133. AVCodec **p;
  134. avcodec_init();
  135. p = &first_avcodec;
  136. while (*p != NULL)
  137. p = &(*p)->next;
  138. *p = codec;
  139. codec->next = NULL;
  140. if (codec->init_static_data)
  141. codec->init_static_data(codec);
  142. }
  143. unsigned avcodec_get_edge_width(void)
  144. {
  145. return EDGE_WIDTH;
  146. }
  147. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  148. {
  149. s->coded_width = width;
  150. s->coded_height = height;
  151. s->width = -((-width ) >> s->lowres);
  152. s->height = -((-height) >> s->lowres);
  153. }
  154. #define INTERNAL_BUFFER_SIZE (32 + 1)
  155. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  156. int linesize_align[AV_NUM_DATA_POINTERS])
  157. {
  158. int i;
  159. int w_align = 1;
  160. int h_align = 1;
  161. switch (s->pix_fmt) {
  162. case AV_PIX_FMT_YUV420P:
  163. case AV_PIX_FMT_YUYV422:
  164. case AV_PIX_FMT_UYVY422:
  165. case AV_PIX_FMT_YUV422P:
  166. case AV_PIX_FMT_YUV440P:
  167. case AV_PIX_FMT_YUV444P:
  168. case AV_PIX_FMT_GBRP:
  169. case AV_PIX_FMT_GRAY8:
  170. case AV_PIX_FMT_GRAY16BE:
  171. case AV_PIX_FMT_GRAY16LE:
  172. case AV_PIX_FMT_YUVJ420P:
  173. case AV_PIX_FMT_YUVJ422P:
  174. case AV_PIX_FMT_YUVJ440P:
  175. case AV_PIX_FMT_YUVJ444P:
  176. case AV_PIX_FMT_YUVA420P:
  177. case AV_PIX_FMT_YUVA422P:
  178. case AV_PIX_FMT_YUVA444P:
  179. case AV_PIX_FMT_YUV420P9LE:
  180. case AV_PIX_FMT_YUV420P9BE:
  181. case AV_PIX_FMT_YUV420P10LE:
  182. case AV_PIX_FMT_YUV420P10BE:
  183. case AV_PIX_FMT_YUV420P12LE:
  184. case AV_PIX_FMT_YUV420P12BE:
  185. case AV_PIX_FMT_YUV420P14LE:
  186. case AV_PIX_FMT_YUV420P14BE:
  187. case AV_PIX_FMT_YUV422P9LE:
  188. case AV_PIX_FMT_YUV422P9BE:
  189. case AV_PIX_FMT_YUV422P10LE:
  190. case AV_PIX_FMT_YUV422P10BE:
  191. case AV_PIX_FMT_YUV422P12LE:
  192. case AV_PIX_FMT_YUV422P12BE:
  193. case AV_PIX_FMT_YUV422P14LE:
  194. case AV_PIX_FMT_YUV422P14BE:
  195. case AV_PIX_FMT_YUV444P9LE:
  196. case AV_PIX_FMT_YUV444P9BE:
  197. case AV_PIX_FMT_YUV444P10LE:
  198. case AV_PIX_FMT_YUV444P10BE:
  199. case AV_PIX_FMT_YUV444P12LE:
  200. case AV_PIX_FMT_YUV444P12BE:
  201. case AV_PIX_FMT_YUV444P14LE:
  202. case AV_PIX_FMT_YUV444P14BE:
  203. case AV_PIX_FMT_GBRP9LE:
  204. case AV_PIX_FMT_GBRP9BE:
  205. case AV_PIX_FMT_GBRP10LE:
  206. case AV_PIX_FMT_GBRP10BE:
  207. case AV_PIX_FMT_GBRP12LE:
  208. case AV_PIX_FMT_GBRP12BE:
  209. case AV_PIX_FMT_GBRP14LE:
  210. case AV_PIX_FMT_GBRP14BE:
  211. w_align = 16; //FIXME assume 16 pixel per macroblock
  212. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  213. break;
  214. case AV_PIX_FMT_YUV411P:
  215. case AV_PIX_FMT_UYYVYY411:
  216. w_align = 32;
  217. h_align = 8;
  218. break;
  219. case AV_PIX_FMT_YUV410P:
  220. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  221. w_align = 64;
  222. h_align = 64;
  223. }
  224. break;
  225. case AV_PIX_FMT_RGB555:
  226. if (s->codec_id == AV_CODEC_ID_RPZA) {
  227. w_align = 4;
  228. h_align = 4;
  229. }
  230. break;
  231. case AV_PIX_FMT_PAL8:
  232. case AV_PIX_FMT_BGR8:
  233. case AV_PIX_FMT_RGB8:
  234. if (s->codec_id == AV_CODEC_ID_SMC) {
  235. w_align = 4;
  236. h_align = 4;
  237. }
  238. break;
  239. case AV_PIX_FMT_BGR24:
  240. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  241. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  242. w_align = 4;
  243. h_align = 4;
  244. }
  245. break;
  246. default:
  247. w_align = 1;
  248. h_align = 1;
  249. break;
  250. }
  251. if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
  252. w_align = FFMAX(w_align, 8);
  253. }
  254. *width = FFALIGN(*width, w_align);
  255. *height = FFALIGN(*height, h_align);
  256. if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
  257. // some of the optimized chroma MC reads one line too much
  258. // which is also done in mpeg decoders with lowres > 0
  259. *height += 2;
  260. for (i = 0; i < 4; i++)
  261. linesize_align[i] = STRIDE_ALIGN;
  262. }
  263. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  264. {
  265. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  266. int chroma_shift = desc->log2_chroma_w;
  267. int linesize_align[AV_NUM_DATA_POINTERS];
  268. int align;
  269. avcodec_align_dimensions2(s, width, height, linesize_align);
  270. align = FFMAX(linesize_align[0], linesize_align[3]);
  271. linesize_align[1] <<= chroma_shift;
  272. linesize_align[2] <<= chroma_shift;
  273. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  274. *width = FFALIGN(*width, align);
  275. }
  276. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  277. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  278. int buf_size, int align)
  279. {
  280. int ch, planar, needed_size, ret = 0;
  281. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  282. frame->nb_samples, sample_fmt,
  283. align);
  284. if (buf_size < needed_size)
  285. return AVERROR(EINVAL);
  286. planar = av_sample_fmt_is_planar(sample_fmt);
  287. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  288. if (!(frame->extended_data = av_mallocz(nb_channels *
  289. sizeof(*frame->extended_data))))
  290. return AVERROR(ENOMEM);
  291. } else {
  292. frame->extended_data = frame->data;
  293. }
  294. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  295. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  296. sample_fmt, align)) < 0) {
  297. if (frame->extended_data != frame->data)
  298. av_freep(&frame->extended_data);
  299. return ret;
  300. }
  301. if (frame->extended_data != frame->data) {
  302. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  303. frame->data[ch] = frame->extended_data[ch];
  304. }
  305. return ret;
  306. }
  307. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  308. {
  309. AVCodecInternal *avci = avctx->internal;
  310. int buf_size, ret;
  311. av_freep(&avci->audio_data);
  312. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  313. frame->nb_samples, avctx->sample_fmt,
  314. 0);
  315. if (buf_size < 0)
  316. return AVERROR(EINVAL);
  317. frame->data[0] = av_mallocz(buf_size);
  318. if (!frame->data[0])
  319. return AVERROR(ENOMEM);
  320. ret = avcodec_fill_audio_frame(frame, avctx->channels, avctx->sample_fmt,
  321. frame->data[0], buf_size, 0);
  322. if (ret < 0) {
  323. av_freep(&frame->data[0]);
  324. return ret;
  325. }
  326. avci->audio_data = frame->data[0];
  327. if (avctx->debug & FF_DEBUG_BUFFERS)
  328. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  329. "internal audio buffer used\n", frame);
  330. return 0;
  331. }
  332. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  333. {
  334. int i;
  335. int w = s->width;
  336. int h = s->height;
  337. InternalBuffer *buf;
  338. AVCodecInternal *avci = s->internal;
  339. if (pic->data[0] != NULL) {
  340. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  341. return -1;
  342. }
  343. if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  344. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  345. return -1;
  346. }
  347. if (av_image_check_size(w, h, 0, s) || s->pix_fmt<0) {
  348. av_log(s, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
  349. return -1;
  350. }
  351. if (!avci->buffer) {
  352. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
  353. sizeof(InternalBuffer));
  354. }
  355. buf = &avci->buffer[avci->buffer_count];
  356. if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
  357. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  358. av_freep(&buf->base[i]);
  359. buf->data[i] = NULL;
  360. }
  361. }
  362. if (!buf->base[0]) {
  363. int h_chroma_shift, v_chroma_shift;
  364. int size[4] = { 0 };
  365. int tmpsize;
  366. int unaligned;
  367. AVPicture picture;
  368. int stride_align[AV_NUM_DATA_POINTERS];
  369. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  370. const int pixel_size = desc->comp[0].step_minus1 + 1;
  371. av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift,
  372. &v_chroma_shift);
  373. avcodec_align_dimensions2(s, &w, &h, stride_align);
  374. if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
  375. w += EDGE_WIDTH * 2;
  376. h += EDGE_WIDTH * 2;
  377. }
  378. do {
  379. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  380. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  381. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  382. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  383. w += w & ~(w - 1);
  384. unaligned = 0;
  385. for (i = 0; i < 4; i++)
  386. unaligned |= picture.linesize[i] % stride_align[i];
  387. } while (unaligned);
  388. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  389. if (tmpsize < 0)
  390. return -1;
  391. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  392. size[i] = picture.data[i + 1] - picture.data[i];
  393. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  394. memset(buf->base, 0, sizeof(buf->base));
  395. memset(buf->data, 0, sizeof(buf->data));
  396. for (i = 0; i < 4 && size[i]; i++) {
  397. const int h_shift = i == 0 ? 0 : h_chroma_shift;
  398. const int v_shift = i == 0 ? 0 : v_chroma_shift;
  399. buf->linesize[i] = picture.linesize[i];
  400. buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
  401. if (buf->base[i] == NULL)
  402. return AVERROR(ENOMEM);
  403. memset(buf->base[i], 128, size[i]);
  404. // no edge if EDGE EMU or not planar YUV
  405. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
  406. buf->data[i] = buf->base[i];
  407. else
  408. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
  409. }
  410. for (; i < AV_NUM_DATA_POINTERS; i++) {
  411. buf->base[i] = buf->data[i] = NULL;
  412. buf->linesize[i] = 0;
  413. }
  414. if (size[1] && !size[2])
  415. avpriv_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
  416. buf->width = s->width;
  417. buf->height = s->height;
  418. buf->pix_fmt = s->pix_fmt;
  419. }
  420. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  421. pic->base[i] = buf->base[i];
  422. pic->data[i] = buf->data[i];
  423. pic->linesize[i] = buf->linesize[i];
  424. }
  425. pic->extended_data = pic->data;
  426. avci->buffer_count++;
  427. if (s->debug & FF_DEBUG_BUFFERS)
  428. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  429. "buffers used\n", pic, avci->buffer_count);
  430. return 0;
  431. }
  432. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  433. {
  434. frame->type = FF_BUFFER_TYPE_INTERNAL;
  435. switch (avctx->codec_type) {
  436. case AVMEDIA_TYPE_VIDEO:
  437. return video_get_buffer(avctx, frame);
  438. case AVMEDIA_TYPE_AUDIO:
  439. return audio_get_buffer(avctx, frame);
  440. default:
  441. return -1;
  442. }
  443. }
  444. void ff_init_buffer_info(AVCodecContext *s, AVFrame *frame)
  445. {
  446. if (s->pkt) {
  447. frame->pkt_pts = s->pkt->pts;
  448. frame->pkt_pos = s->pkt->pos;
  449. frame->pkt_duration = s->pkt->duration;
  450. frame->pkt_size = s->pkt->size;
  451. } else {
  452. frame->pkt_pts = AV_NOPTS_VALUE;
  453. frame->pkt_pos = -1;
  454. frame->pkt_duration = 0;
  455. frame->pkt_size = -1;
  456. }
  457. frame->reordered_opaque = s->reordered_opaque;
  458. switch (s->codec->type) {
  459. case AVMEDIA_TYPE_VIDEO:
  460. frame->width = s->width;
  461. frame->height = s->height;
  462. frame->format = s->pix_fmt;
  463. frame->sample_aspect_ratio = s->sample_aspect_ratio;
  464. break;
  465. case AVMEDIA_TYPE_AUDIO:
  466. frame->sample_rate = s->sample_rate;
  467. frame->format = s->sample_fmt;
  468. frame->channel_layout = s->channel_layout;
  469. frame->channels = s->channels;
  470. break;
  471. }
  472. }
  473. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  474. {
  475. ff_init_buffer_info(avctx, frame);
  476. return avctx->get_buffer(avctx, frame);
  477. }
  478. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  479. {
  480. int i;
  481. InternalBuffer *buf, *last;
  482. AVCodecInternal *avci = s->internal;
  483. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  484. assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
  485. assert(avci->buffer_count);
  486. if (avci->buffer) {
  487. buf = NULL; /* avoids warning */
  488. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  489. buf = &avci->buffer[i];
  490. if (buf->data[0] == pic->data[0])
  491. break;
  492. }
  493. av_assert0(i < avci->buffer_count);
  494. avci->buffer_count--;
  495. last = &avci->buffer[avci->buffer_count];
  496. if (buf != last)
  497. FFSWAP(InternalBuffer, *buf, *last);
  498. }
  499. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  500. pic->data[i] = NULL;
  501. // pic->base[i]=NULL;
  502. if (s->debug & FF_DEBUG_BUFFERS)
  503. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  504. "buffers used\n", pic, avci->buffer_count);
  505. }
  506. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  507. {
  508. AVFrame temp_pic;
  509. int i, ret;
  510. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  511. if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
  512. av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  513. pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
  514. s->release_buffer(s, pic);
  515. }
  516. ff_init_buffer_info(s, pic);
  517. /* If no picture return a new buffer */
  518. if (pic->data[0] == NULL) {
  519. /* We will copy from buffer, so must be readable */
  520. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  521. return ff_get_buffer(s, pic);
  522. }
  523. assert(s->pix_fmt == pic->format);
  524. /* If internal buffer type return the same buffer */
  525. if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
  526. return 0;
  527. }
  528. /*
  529. * Not internal type and reget_buffer not overridden, emulate cr buffer
  530. */
  531. temp_pic = *pic;
  532. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  533. pic->data[i] = pic->base[i] = NULL;
  534. pic->opaque = NULL;
  535. /* Allocate new frame */
  536. if ((ret = ff_get_buffer(s, pic)))
  537. return ret;
  538. /* Copy image data from old buffer to new buffer */
  539. av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
  540. s->height);
  541. s->release_buffer(s, &temp_pic); // Release old frame
  542. return 0;
  543. }
  544. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  545. {
  546. int i;
  547. for (i = 0; i < count; i++) {
  548. int r = func(c, (char *)arg + i * size);
  549. if (ret)
  550. ret[i] = r;
  551. }
  552. return 0;
  553. }
  554. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  555. {
  556. int i;
  557. for (i = 0; i < count; i++) {
  558. int r = func(c, arg, i, 0);
  559. if (ret)
  560. ret[i] = r;
  561. }
  562. return 0;
  563. }
  564. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  565. {
  566. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  567. return desc->flags & PIX_FMT_HWACCEL;
  568. }
  569. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  570. {
  571. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  572. ++fmt;
  573. return fmt[0];
  574. }
  575. void avcodec_get_frame_defaults(AVFrame *frame)
  576. {
  577. #if LIBAVCODEC_VERSION_MAJOR >= 55
  578. // extended_data should explicitly be freed when needed, this code is unsafe currently
  579. // also this is not compatible to the <55 ABI/API
  580. if (frame->extended_data != frame->data && 0)
  581. av_freep(&frame->extended_data);
  582. #endif
  583. memset(frame, 0, sizeof(AVFrame));
  584. frame->pts =
  585. frame->pkt_dts =
  586. frame->pkt_pts =
  587. frame->best_effort_timestamp = AV_NOPTS_VALUE;
  588. frame->pkt_duration = 0;
  589. frame->pkt_pos = -1;
  590. frame->pkt_size = -1;
  591. frame->key_frame = 1;
  592. frame->sample_aspect_ratio = (AVRational) {0, 1 };
  593. frame->format = -1; /* unknown */
  594. frame->extended_data = frame->data;
  595. }
  596. AVFrame *avcodec_alloc_frame(void)
  597. {
  598. AVFrame *frame = av_malloc(sizeof(AVFrame));
  599. if (frame == NULL)
  600. return NULL;
  601. frame->extended_data = NULL;
  602. avcodec_get_frame_defaults(frame);
  603. return frame;
  604. }
  605. void avcodec_free_frame(AVFrame **frame)
  606. {
  607. AVFrame *f;
  608. if (!frame || !*frame)
  609. return;
  610. f = *frame;
  611. if (f->extended_data != f->data)
  612. av_freep(&f->extended_data);
  613. av_freep(frame);
  614. }
  615. #define MAKE_ACCESSORS(str, name, type, field) \
  616. type av_##name##_get_##field(const str *s) { return s->field; } \
  617. void av_##name##_set_##field(str *s, type v) { s->field = v; }
  618. MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
  619. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
  620. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
  621. MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
  622. MAKE_ACCESSORS(AVFrame, frame, int, channels)
  623. MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
  624. MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
  625. MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
  626. MAKE_ACCESSORS(AVFrame, frame, int, pkt_size)
  627. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  628. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  629. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  630. {
  631. memset(sub, 0, sizeof(*sub));
  632. sub->pts = AV_NOPTS_VALUE;
  633. }
  634. static int get_bit_rate(AVCodecContext *ctx)
  635. {
  636. int bit_rate;
  637. int bits_per_sample;
  638. switch (ctx->codec_type) {
  639. case AVMEDIA_TYPE_VIDEO:
  640. case AVMEDIA_TYPE_DATA:
  641. case AVMEDIA_TYPE_SUBTITLE:
  642. case AVMEDIA_TYPE_ATTACHMENT:
  643. bit_rate = ctx->bit_rate;
  644. break;
  645. case AVMEDIA_TYPE_AUDIO:
  646. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  647. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  648. break;
  649. default:
  650. bit_rate = 0;
  651. break;
  652. }
  653. return bit_rate;
  654. }
  655. #if FF_API_AVCODEC_OPEN
  656. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  657. {
  658. return avcodec_open2(avctx, codec, NULL);
  659. }
  660. #endif
  661. int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  662. {
  663. int ret = 0;
  664. ff_unlock_avcodec();
  665. ret = avcodec_open2(avctx, codec, options);
  666. ff_lock_avcodec(avctx);
  667. return ret;
  668. }
  669. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  670. {
  671. int ret = 0;
  672. AVDictionary *tmp = NULL;
  673. if (avcodec_is_open(avctx))
  674. return 0;
  675. if ((!codec && !avctx->codec)) {
  676. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  677. return AVERROR(EINVAL);
  678. }
  679. if ((codec && avctx->codec && codec != avctx->codec)) {
  680. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  681. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  682. return AVERROR(EINVAL);
  683. }
  684. if (!codec)
  685. codec = avctx->codec;
  686. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  687. return AVERROR(EINVAL);
  688. if (options)
  689. av_dict_copy(&tmp, *options, 0);
  690. ret = ff_lock_avcodec(avctx);
  691. if (ret < 0)
  692. return ret;
  693. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  694. if (!avctx->internal) {
  695. ret = AVERROR(ENOMEM);
  696. goto end;
  697. }
  698. if (codec->priv_data_size > 0) {
  699. if (!avctx->priv_data) {
  700. avctx->priv_data = av_mallocz(codec->priv_data_size);
  701. if (!avctx->priv_data) {
  702. ret = AVERROR(ENOMEM);
  703. goto end;
  704. }
  705. if (codec->priv_class) {
  706. *(const AVClass **)avctx->priv_data = codec->priv_class;
  707. av_opt_set_defaults(avctx->priv_data);
  708. }
  709. }
  710. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  711. goto free_and_end;
  712. } else {
  713. avctx->priv_data = NULL;
  714. }
  715. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  716. goto free_and_end;
  717. //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
  718. if (!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == AV_CODEC_ID_H264)){
  719. if (avctx->coded_width && avctx->coded_height)
  720. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  721. else if (avctx->width && avctx->height)
  722. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  723. }
  724. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  725. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  726. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  727. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  728. avcodec_set_dimensions(avctx, 0, 0);
  729. }
  730. /* if the decoder init function was already called previously,
  731. * free the already allocated subtitle_header before overwriting it */
  732. if (av_codec_is_decoder(codec))
  733. av_freep(&avctx->subtitle_header);
  734. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  735. ret = AVERROR(EINVAL);
  736. goto free_and_end;
  737. }
  738. avctx->codec = codec;
  739. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  740. avctx->codec_id == AV_CODEC_ID_NONE) {
  741. avctx->codec_type = codec->type;
  742. avctx->codec_id = codec->id;
  743. }
  744. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  745. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  746. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  747. ret = AVERROR(EINVAL);
  748. goto free_and_end;
  749. }
  750. avctx->frame_number = 0;
  751. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  752. if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
  753. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  754. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  755. AVCodec *codec2;
  756. av_log(NULL, AV_LOG_ERROR,
  757. "The %s '%s' is experimental but experimental codecs are not enabled, "
  758. "add '-strict %d' if you want to use it.\n",
  759. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  760. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  761. if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
  762. av_log(NULL, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  763. codec_string, codec2->name);
  764. ret = AVERROR_EXPERIMENTAL;
  765. goto free_and_end;
  766. }
  767. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  768. (!avctx->time_base.num || !avctx->time_base.den)) {
  769. avctx->time_base.num = 1;
  770. avctx->time_base.den = avctx->sample_rate;
  771. }
  772. if (!HAVE_THREADS)
  773. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  774. if (HAVE_THREADS) {
  775. ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  776. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  777. ff_lock_avcodec(avctx);
  778. if (ret < 0)
  779. goto free_and_end;
  780. }
  781. if (HAVE_THREADS && !avctx->thread_opaque
  782. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  783. ret = ff_thread_init(avctx);
  784. if (ret < 0) {
  785. goto free_and_end;
  786. }
  787. }
  788. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  789. avctx->thread_count = 1;
  790. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  791. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  792. avctx->codec->max_lowres);
  793. ret = AVERROR(EINVAL);
  794. goto free_and_end;
  795. }
  796. if (av_codec_is_encoder(avctx->codec)) {
  797. int i;
  798. if (avctx->codec->sample_fmts) {
  799. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  800. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  801. break;
  802. if (avctx->channels == 1 &&
  803. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  804. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  805. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  806. break;
  807. }
  808. }
  809. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  810. char buf[128];
  811. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  812. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  813. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  814. ret = AVERROR(EINVAL);
  815. goto free_and_end;
  816. }
  817. }
  818. if (avctx->codec->pix_fmts) {
  819. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  820. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  821. break;
  822. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  823. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  824. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  825. char buf[128];
  826. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  827. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  828. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  829. ret = AVERROR(EINVAL);
  830. goto free_and_end;
  831. }
  832. }
  833. if (avctx->codec->supported_samplerates) {
  834. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  835. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  836. break;
  837. if (avctx->codec->supported_samplerates[i] == 0) {
  838. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  839. avctx->sample_rate);
  840. ret = AVERROR(EINVAL);
  841. goto free_and_end;
  842. }
  843. }
  844. if (avctx->codec->channel_layouts) {
  845. if (!avctx->channel_layout) {
  846. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  847. } else {
  848. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  849. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  850. break;
  851. if (avctx->codec->channel_layouts[i] == 0) {
  852. char buf[512];
  853. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  854. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  855. ret = AVERROR(EINVAL);
  856. goto free_and_end;
  857. }
  858. }
  859. }
  860. if (avctx->channel_layout && avctx->channels) {
  861. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  862. if (channels != avctx->channels) {
  863. char buf[512];
  864. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  865. av_log(avctx, AV_LOG_ERROR,
  866. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  867. buf, channels, avctx->channels);
  868. ret = AVERROR(EINVAL);
  869. goto free_and_end;
  870. }
  871. } else if (avctx->channel_layout) {
  872. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  873. }
  874. if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
  875. avctx->codec_id != AV_CODEC_ID_PNG // For mplayer
  876. ) {
  877. if (avctx->width <= 0 || avctx->height <= 0) {
  878. av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
  879. ret = AVERROR(EINVAL);
  880. goto free_and_end;
  881. }
  882. }
  883. if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
  884. && avctx->bit_rate>0 && avctx->bit_rate<1000) {
  885. av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extreemly low, did you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
  886. }
  887. if (!avctx->rc_initial_buffer_occupancy)
  888. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  889. }
  890. avctx->pts_correction_num_faulty_pts =
  891. avctx->pts_correction_num_faulty_dts = 0;
  892. avctx->pts_correction_last_pts =
  893. avctx->pts_correction_last_dts = INT64_MIN;
  894. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  895. || avctx->internal->frame_thread_encoder)) {
  896. ret = avctx->codec->init(avctx);
  897. if (ret < 0) {
  898. goto free_and_end;
  899. }
  900. }
  901. ret=0;
  902. if (av_codec_is_decoder(avctx->codec)) {
  903. if (!avctx->bit_rate)
  904. avctx->bit_rate = get_bit_rate(avctx);
  905. /* validate channel layout from the decoder */
  906. if (avctx->channel_layout) {
  907. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  908. if (!avctx->channels)
  909. avctx->channels = channels;
  910. else if (channels != avctx->channels) {
  911. char buf[512];
  912. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  913. av_log(avctx, AV_LOG_WARNING,
  914. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  915. "ignoring specified channel layout\n",
  916. buf, channels, avctx->channels);
  917. avctx->channel_layout = 0;
  918. }
  919. }
  920. if (avctx->channels && avctx->channels < 0 ||
  921. avctx->channels > FF_SANE_NB_CHANNELS) {
  922. ret = AVERROR(EINVAL);
  923. goto free_and_end;
  924. }
  925. }
  926. end:
  927. ff_unlock_avcodec();
  928. if (options) {
  929. av_dict_free(options);
  930. *options = tmp;
  931. }
  932. return ret;
  933. free_and_end:
  934. av_dict_free(&tmp);
  935. av_freep(&avctx->priv_data);
  936. av_freep(&avctx->internal);
  937. avctx->codec = NULL;
  938. goto end;
  939. }
  940. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
  941. {
  942. if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  943. av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
  944. return AVERROR(EINVAL);
  945. }
  946. if (avctx) {
  947. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  948. if (!avpkt->data || avpkt->size < size) {
  949. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  950. avpkt->data = avctx->internal->byte_buffer;
  951. avpkt->size = avctx->internal->byte_buffer_size;
  952. avpkt->destruct = NULL;
  953. }
  954. }
  955. if (avpkt->data) {
  956. void *destruct = avpkt->destruct;
  957. if (avpkt->size < size) {
  958. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
  959. return AVERROR(EINVAL);
  960. }
  961. av_init_packet(avpkt);
  962. avpkt->destruct = destruct;
  963. avpkt->size = size;
  964. return 0;
  965. } else {
  966. int ret = av_new_packet(avpkt, size);
  967. if (ret < 0)
  968. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
  969. return ret;
  970. }
  971. }
  972. int ff_alloc_packet(AVPacket *avpkt, int size)
  973. {
  974. return ff_alloc_packet2(NULL, avpkt, size);
  975. }
  976. /**
  977. * Pad last frame with silence.
  978. */
  979. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  980. {
  981. AVFrame *frame = NULL;
  982. uint8_t *buf = NULL;
  983. int ret;
  984. if (!(frame = avcodec_alloc_frame()))
  985. return AVERROR(ENOMEM);
  986. *frame = *src;
  987. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  988. s->frame_size, s->sample_fmt, 0)) < 0)
  989. goto fail;
  990. if (!(buf = av_malloc(ret))) {
  991. ret = AVERROR(ENOMEM);
  992. goto fail;
  993. }
  994. frame->nb_samples = s->frame_size;
  995. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  996. buf, ret, 0)) < 0)
  997. goto fail;
  998. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  999. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1000. goto fail;
  1001. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1002. frame->nb_samples - src->nb_samples,
  1003. s->channels, s->sample_fmt)) < 0)
  1004. goto fail;
  1005. *dst = frame;
  1006. return 0;
  1007. fail:
  1008. if (frame->extended_data != frame->data)
  1009. av_freep(&frame->extended_data);
  1010. av_freep(&buf);
  1011. av_freep(&frame);
  1012. return ret;
  1013. }
  1014. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1015. AVPacket *avpkt,
  1016. const AVFrame *frame,
  1017. int *got_packet_ptr)
  1018. {
  1019. AVFrame tmp;
  1020. AVFrame *padded_frame = NULL;
  1021. int ret;
  1022. AVPacket user_pkt = *avpkt;
  1023. int needs_realloc = !user_pkt.data;
  1024. *got_packet_ptr = 0;
  1025. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1026. av_free_packet(avpkt);
  1027. av_init_packet(avpkt);
  1028. return 0;
  1029. }
  1030. /* ensure that extended_data is properly set */
  1031. if (frame && !frame->extended_data) {
  1032. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1033. avctx->channels > AV_NUM_DATA_POINTERS) {
  1034. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1035. "with more than %d channels, but extended_data is not set.\n",
  1036. AV_NUM_DATA_POINTERS);
  1037. return AVERROR(EINVAL);
  1038. }
  1039. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1040. tmp = *frame;
  1041. tmp.extended_data = tmp.data;
  1042. frame = &tmp;
  1043. }
  1044. /* check for valid frame size */
  1045. if (frame) {
  1046. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1047. if (frame->nb_samples > avctx->frame_size) {
  1048. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1049. return AVERROR(EINVAL);
  1050. }
  1051. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1052. if (frame->nb_samples < avctx->frame_size &&
  1053. !avctx->internal->last_audio_frame) {
  1054. ret = pad_last_frame(avctx, &padded_frame, frame);
  1055. if (ret < 0)
  1056. return ret;
  1057. frame = padded_frame;
  1058. avctx->internal->last_audio_frame = 1;
  1059. }
  1060. if (frame->nb_samples != avctx->frame_size) {
  1061. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1062. ret = AVERROR(EINVAL);
  1063. goto end;
  1064. }
  1065. }
  1066. }
  1067. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1068. if (!ret) {
  1069. if (*got_packet_ptr) {
  1070. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1071. if (avpkt->pts == AV_NOPTS_VALUE)
  1072. avpkt->pts = frame->pts;
  1073. if (!avpkt->duration)
  1074. avpkt->duration = ff_samples_to_time_base(avctx,
  1075. frame->nb_samples);
  1076. }
  1077. avpkt->dts = avpkt->pts;
  1078. } else {
  1079. avpkt->size = 0;
  1080. }
  1081. }
  1082. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1083. needs_realloc = 0;
  1084. if (user_pkt.data) {
  1085. if (user_pkt.size >= avpkt->size) {
  1086. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1087. } else {
  1088. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1089. avpkt->size = user_pkt.size;
  1090. ret = -1;
  1091. }
  1092. avpkt->data = user_pkt.data;
  1093. avpkt->destruct = user_pkt.destruct;
  1094. } else {
  1095. if (av_dup_packet(avpkt) < 0) {
  1096. ret = AVERROR(ENOMEM);
  1097. }
  1098. }
  1099. }
  1100. if (!ret) {
  1101. if (needs_realloc && avpkt->data) {
  1102. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1103. if (new_data)
  1104. avpkt->data = new_data;
  1105. }
  1106. avctx->frame_number++;
  1107. }
  1108. if (ret < 0 || !*got_packet_ptr) {
  1109. av_free_packet(avpkt);
  1110. av_init_packet(avpkt);
  1111. goto end;
  1112. }
  1113. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1114. * this needs to be moved to the encoders, but for now we can do it
  1115. * here to simplify things */
  1116. avpkt->flags |= AV_PKT_FLAG_KEY;
  1117. end:
  1118. if (padded_frame) {
  1119. av_freep(&padded_frame->data[0]);
  1120. if (padded_frame->extended_data != padded_frame->data)
  1121. av_freep(&padded_frame->extended_data);
  1122. av_freep(&padded_frame);
  1123. }
  1124. return ret;
  1125. }
  1126. #if FF_API_OLD_ENCODE_AUDIO
  1127. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1128. uint8_t *buf, int buf_size,
  1129. const short *samples)
  1130. {
  1131. AVPacket pkt;
  1132. AVFrame frame0 = { { 0 } };
  1133. AVFrame *frame;
  1134. int ret, samples_size, got_packet;
  1135. av_init_packet(&pkt);
  1136. pkt.data = buf;
  1137. pkt.size = buf_size;
  1138. if (samples) {
  1139. frame = &frame0;
  1140. avcodec_get_frame_defaults(frame);
  1141. if (avctx->frame_size) {
  1142. frame->nb_samples = avctx->frame_size;
  1143. } else {
  1144. /* if frame_size is not set, the number of samples must be
  1145. * calculated from the buffer size */
  1146. int64_t nb_samples;
  1147. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1148. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1149. "support this codec\n");
  1150. return AVERROR(EINVAL);
  1151. }
  1152. nb_samples = (int64_t)buf_size * 8 /
  1153. (av_get_bits_per_sample(avctx->codec_id) *
  1154. avctx->channels);
  1155. if (nb_samples >= INT_MAX)
  1156. return AVERROR(EINVAL);
  1157. frame->nb_samples = nb_samples;
  1158. }
  1159. /* it is assumed that the samples buffer is large enough based on the
  1160. * relevant parameters */
  1161. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1162. frame->nb_samples,
  1163. avctx->sample_fmt, 1);
  1164. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1165. avctx->sample_fmt,
  1166. (const uint8_t *)samples,
  1167. samples_size, 1)) < 0)
  1168. return ret;
  1169. /* fabricate frame pts from sample count.
  1170. * this is needed because the avcodec_encode_audio() API does not have
  1171. * a way for the user to provide pts */
  1172. if (avctx->sample_rate && avctx->time_base.num)
  1173. frame->pts = ff_samples_to_time_base(avctx,
  1174. avctx->internal->sample_count);
  1175. else
  1176. frame->pts = AV_NOPTS_VALUE;
  1177. avctx->internal->sample_count += frame->nb_samples;
  1178. } else {
  1179. frame = NULL;
  1180. }
  1181. got_packet = 0;
  1182. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1183. if (!ret && got_packet && avctx->coded_frame) {
  1184. avctx->coded_frame->pts = pkt.pts;
  1185. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1186. }
  1187. /* free any side data since we cannot return it */
  1188. ff_packet_free_side_data(&pkt);
  1189. if (frame && frame->extended_data != frame->data)
  1190. av_freep(&frame->extended_data);
  1191. return ret ? ret : pkt.size;
  1192. }
  1193. #endif
  1194. #if FF_API_OLD_ENCODE_VIDEO
  1195. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1196. const AVFrame *pict)
  1197. {
  1198. AVPacket pkt;
  1199. int ret, got_packet = 0;
  1200. if (buf_size < FF_MIN_BUFFER_SIZE) {
  1201. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1202. return -1;
  1203. }
  1204. av_init_packet(&pkt);
  1205. pkt.data = buf;
  1206. pkt.size = buf_size;
  1207. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1208. if (!ret && got_packet && avctx->coded_frame) {
  1209. avctx->coded_frame->pts = pkt.pts;
  1210. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1211. }
  1212. /* free any side data since we cannot return it */
  1213. if (pkt.side_data_elems > 0) {
  1214. int i;
  1215. for (i = 0; i < pkt.side_data_elems; i++)
  1216. av_free(pkt.side_data[i].data);
  1217. av_freep(&pkt.side_data);
  1218. pkt.side_data_elems = 0;
  1219. }
  1220. return ret ? ret : pkt.size;
  1221. }
  1222. #endif
  1223. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1224. AVPacket *avpkt,
  1225. const AVFrame *frame,
  1226. int *got_packet_ptr)
  1227. {
  1228. int ret;
  1229. AVPacket user_pkt = *avpkt;
  1230. int needs_realloc = !user_pkt.data;
  1231. *got_packet_ptr = 0;
  1232. if(HAVE_THREADS && avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1233. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1234. if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
  1235. avctx->stats_out[0] = '\0';
  1236. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1237. av_free_packet(avpkt);
  1238. av_init_packet(avpkt);
  1239. avpkt->size = 0;
  1240. return 0;
  1241. }
  1242. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1243. return AVERROR(EINVAL);
  1244. av_assert0(avctx->codec->encode2);
  1245. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1246. av_assert0(ret <= 0);
  1247. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1248. needs_realloc = 0;
  1249. if (user_pkt.data) {
  1250. if (user_pkt.size >= avpkt->size) {
  1251. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1252. } else {
  1253. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1254. avpkt->size = user_pkt.size;
  1255. ret = -1;
  1256. }
  1257. avpkt->data = user_pkt.data;
  1258. avpkt->destruct = user_pkt.destruct;
  1259. } else {
  1260. if (av_dup_packet(avpkt) < 0) {
  1261. ret = AVERROR(ENOMEM);
  1262. }
  1263. }
  1264. }
  1265. if (!ret) {
  1266. if (!*got_packet_ptr)
  1267. avpkt->size = 0;
  1268. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1269. avpkt->pts = avpkt->dts = frame->pts;
  1270. if (needs_realloc && avpkt->data &&
  1271. avpkt->destruct == av_destruct_packet) {
  1272. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1273. if (new_data)
  1274. avpkt->data = new_data;
  1275. }
  1276. avctx->frame_number++;
  1277. }
  1278. if (ret < 0 || !*got_packet_ptr)
  1279. av_free_packet(avpkt);
  1280. emms_c();
  1281. return ret;
  1282. }
  1283. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1284. const AVSubtitle *sub)
  1285. {
  1286. int ret;
  1287. if (sub->start_display_time) {
  1288. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1289. return -1;
  1290. }
  1291. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1292. avctx->frame_number++;
  1293. return ret;
  1294. }
  1295. /**
  1296. * Attempt to guess proper monotonic timestamps for decoded video frames
  1297. * which might have incorrect times. Input timestamps may wrap around, in
  1298. * which case the output will as well.
  1299. *
  1300. * @param pts the pts field of the decoded AVPacket, as passed through
  1301. * AVFrame.pkt_pts
  1302. * @param dts the dts field of the decoded AVPacket
  1303. * @return one of the input values, may be AV_NOPTS_VALUE
  1304. */
  1305. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1306. int64_t reordered_pts, int64_t dts)
  1307. {
  1308. int64_t pts = AV_NOPTS_VALUE;
  1309. if (dts != AV_NOPTS_VALUE) {
  1310. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1311. ctx->pts_correction_last_dts = dts;
  1312. }
  1313. if (reordered_pts != AV_NOPTS_VALUE) {
  1314. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1315. ctx->pts_correction_last_pts = reordered_pts;
  1316. }
  1317. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1318. && reordered_pts != AV_NOPTS_VALUE)
  1319. pts = reordered_pts;
  1320. else
  1321. pts = dts;
  1322. return pts;
  1323. }
  1324. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1325. {
  1326. int size = 0;
  1327. const uint8_t *data;
  1328. uint32_t flags;
  1329. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1330. return;
  1331. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1332. if (!data || size < 4)
  1333. return;
  1334. flags = bytestream_get_le32(&data);
  1335. size -= 4;
  1336. if (size < 4) /* Required for any of the changes */
  1337. return;
  1338. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1339. avctx->channels = bytestream_get_le32(&data);
  1340. size -= 4;
  1341. }
  1342. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1343. if (size < 8)
  1344. return;
  1345. avctx->channel_layout = bytestream_get_le64(&data);
  1346. size -= 8;
  1347. }
  1348. if (size < 4)
  1349. return;
  1350. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1351. avctx->sample_rate = bytestream_get_le32(&data);
  1352. size -= 4;
  1353. }
  1354. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1355. if (size < 8)
  1356. return;
  1357. avctx->width = bytestream_get_le32(&data);
  1358. avctx->height = bytestream_get_le32(&data);
  1359. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1360. size -= 8;
  1361. }
  1362. }
  1363. static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
  1364. {
  1365. int size, ret = 0;
  1366. const uint8_t *side_metadata;
  1367. const uint8_t *end;
  1368. av_dict_free(&avctx->metadata);
  1369. side_metadata = av_packet_get_side_data(avctx->pkt,
  1370. AV_PKT_DATA_STRINGS_METADATA, &size);
  1371. if (!side_metadata)
  1372. goto end;
  1373. end = side_metadata + size;
  1374. while (side_metadata < end) {
  1375. const uint8_t *key = side_metadata;
  1376. const uint8_t *val = side_metadata + strlen(key) + 1;
  1377. int ret = av_dict_set(&frame->metadata, key, val, 0);
  1378. if (ret < 0)
  1379. break;
  1380. side_metadata = val + strlen(val) + 1;
  1381. }
  1382. end:
  1383. avctx->metadata = frame->metadata;
  1384. return ret;
  1385. }
  1386. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1387. int *got_picture_ptr,
  1388. const AVPacket *avpkt)
  1389. {
  1390. int ret;
  1391. // copy to ensure we do not change avpkt
  1392. AVPacket tmp = *avpkt;
  1393. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1394. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1395. return AVERROR(EINVAL);
  1396. }
  1397. *got_picture_ptr = 0;
  1398. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1399. return AVERROR(EINVAL);
  1400. avcodec_get_frame_defaults(picture);
  1401. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1402. int did_split = av_packet_split_side_data(&tmp);
  1403. apply_param_change(avctx, &tmp);
  1404. avctx->pkt = &tmp;
  1405. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1406. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1407. &tmp);
  1408. else {
  1409. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1410. &tmp);
  1411. picture->pkt_dts = avpkt->dts;
  1412. if(!avctx->has_b_frames){
  1413. picture->pkt_pos = avpkt->pos;
  1414. }
  1415. //FIXME these should be under if(!avctx->has_b_frames)
  1416. /* get_buffer is supposed to set frame parameters */
  1417. if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
  1418. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1419. if (!picture->width) picture->width = avctx->width;
  1420. if (!picture->height) picture->height = avctx->height;
  1421. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  1422. }
  1423. }
  1424. add_metadata_from_side_data(avctx, picture);
  1425. emms_c(); //needed to avoid an emms_c() call before every return;
  1426. avctx->pkt = NULL;
  1427. if (did_split) {
  1428. ff_packet_free_side_data(&tmp);
  1429. if(ret == tmp.size)
  1430. ret = avpkt->size;
  1431. }
  1432. if (*got_picture_ptr){
  1433. avctx->frame_number++;
  1434. picture->best_effort_timestamp = guess_correct_pts(avctx,
  1435. picture->pkt_pts,
  1436. picture->pkt_dts);
  1437. }
  1438. } else
  1439. ret = 0;
  1440. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1441. * make sure it's set correctly */
  1442. picture->extended_data = picture->data;
  1443. return ret;
  1444. }
  1445. #if FF_API_OLD_DECODE_AUDIO
  1446. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1447. int *frame_size_ptr,
  1448. AVPacket *avpkt)
  1449. {
  1450. AVFrame frame = { { 0 } };
  1451. int ret, got_frame = 0;
  1452. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1453. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1454. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1455. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1456. "avcodec_decode_audio4()\n");
  1457. avctx->get_buffer = avcodec_default_get_buffer;
  1458. avctx->release_buffer = avcodec_default_release_buffer;
  1459. }
  1460. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1461. if (ret >= 0 && got_frame) {
  1462. int ch, plane_size;
  1463. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1464. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1465. frame.nb_samples,
  1466. avctx->sample_fmt, 1);
  1467. if (*frame_size_ptr < data_size) {
  1468. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1469. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1470. return AVERROR(EINVAL);
  1471. }
  1472. memcpy(samples, frame.extended_data[0], plane_size);
  1473. if (planar && avctx->channels > 1) {
  1474. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1475. for (ch = 1; ch < avctx->channels; ch++) {
  1476. memcpy(out, frame.extended_data[ch], plane_size);
  1477. out += plane_size;
  1478. }
  1479. }
  1480. *frame_size_ptr = data_size;
  1481. } else {
  1482. *frame_size_ptr = 0;
  1483. }
  1484. return ret;
  1485. }
  1486. #endif
  1487. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1488. AVFrame *frame,
  1489. int *got_frame_ptr,
  1490. const AVPacket *avpkt)
  1491. {
  1492. int planar, channels;
  1493. int ret = 0;
  1494. *got_frame_ptr = 0;
  1495. if (!avpkt->data && avpkt->size) {
  1496. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1497. return AVERROR(EINVAL);
  1498. }
  1499. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1500. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1501. return AVERROR(EINVAL);
  1502. }
  1503. avcodec_get_frame_defaults(frame);
  1504. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1505. uint8_t *side;
  1506. int side_size;
  1507. // copy to ensure we do not change avpkt
  1508. AVPacket tmp = *avpkt;
  1509. int did_split = av_packet_split_side_data(&tmp);
  1510. apply_param_change(avctx, &tmp);
  1511. avctx->pkt = &tmp;
  1512. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1513. if (ret >= 0 && *got_frame_ptr) {
  1514. avctx->frame_number++;
  1515. frame->pkt_dts = avpkt->dts;
  1516. frame->best_effort_timestamp = guess_correct_pts(avctx,
  1517. frame->pkt_pts,
  1518. frame->pkt_dts);
  1519. if (frame->format == AV_SAMPLE_FMT_NONE)
  1520. frame->format = avctx->sample_fmt;
  1521. if (!frame->channel_layout)
  1522. frame->channel_layout = avctx->channel_layout;
  1523. if (!frame->channels)
  1524. frame->channels = avctx->channels;
  1525. if (!frame->sample_rate)
  1526. frame->sample_rate = avctx->sample_rate;
  1527. }
  1528. add_metadata_from_side_data(avctx, frame);
  1529. side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  1530. if(side && side_size>=10) {
  1531. avctx->internal->skip_samples = AV_RL32(side);
  1532. av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
  1533. avctx->internal->skip_samples);
  1534. }
  1535. if (avctx->internal->skip_samples) {
  1536. if(frame->nb_samples <= avctx->internal->skip_samples){
  1537. *got_frame_ptr = 0;
  1538. avctx->internal->skip_samples -= frame->nb_samples;
  1539. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  1540. avctx->internal->skip_samples);
  1541. } else {
  1542. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  1543. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  1544. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1545. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  1546. (AVRational){1, avctx->sample_rate},
  1547. avctx->pkt_timebase);
  1548. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  1549. frame->pkt_pts += diff_ts;
  1550. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  1551. frame->pkt_dts += diff_ts;
  1552. if (frame->pkt_duration >= diff_ts)
  1553. frame->pkt_duration -= diff_ts;
  1554. } else {
  1555. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  1556. }
  1557. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  1558. avctx->internal->skip_samples, frame->nb_samples);
  1559. frame->nb_samples -= avctx->internal->skip_samples;
  1560. avctx->internal->skip_samples = 0;
  1561. }
  1562. }
  1563. avctx->pkt = NULL;
  1564. if (did_split) {
  1565. ff_packet_free_side_data(&tmp);
  1566. if(ret == tmp.size)
  1567. ret = avpkt->size;
  1568. }
  1569. }
  1570. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1571. * make sure it's set correctly; assume decoders that actually use
  1572. * extended_data are doing it correctly */
  1573. if (*got_frame_ptr) {
  1574. planar = av_sample_fmt_is_planar(frame->format);
  1575. channels = frame->channels;
  1576. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1577. frame->extended_data = frame->data;
  1578. } else {
  1579. frame->extended_data = NULL;
  1580. }
  1581. return ret;
  1582. }
  1583. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1584. int *got_sub_ptr,
  1585. AVPacket *avpkt)
  1586. {
  1587. int ret = 0;
  1588. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  1589. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  1590. return AVERROR(EINVAL);
  1591. }
  1592. *got_sub_ptr = 0;
  1593. avcodec_get_subtitle_defaults(sub);
  1594. if (avpkt->size) {
  1595. AVPacket tmp = *avpkt;
  1596. int did_split = av_packet_split_side_data(&tmp);
  1597. //apply_param_change(avctx, &tmp);
  1598. avctx->pkt = &tmp;
  1599. if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  1600. sub->pts = av_rescale_q(avpkt->pts,
  1601. avctx->pkt_timebase, AV_TIME_BASE_Q);
  1602. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &tmp);
  1603. sub->format = sub->num_rects && sub->rects[0]->ass;
  1604. avctx->pkt = NULL;
  1605. if (did_split) {
  1606. ff_packet_free_side_data(&tmp);
  1607. if(ret == tmp.size)
  1608. ret = avpkt->size;
  1609. }
  1610. if (*got_sub_ptr)
  1611. avctx->frame_number++;
  1612. }
  1613. return ret;
  1614. }
  1615. void avsubtitle_free(AVSubtitle *sub)
  1616. {
  1617. int i;
  1618. for (i = 0; i < sub->num_rects; i++) {
  1619. av_freep(&sub->rects[i]->pict.data[0]);
  1620. av_freep(&sub->rects[i]->pict.data[1]);
  1621. av_freep(&sub->rects[i]->pict.data[2]);
  1622. av_freep(&sub->rects[i]->pict.data[3]);
  1623. av_freep(&sub->rects[i]->text);
  1624. av_freep(&sub->rects[i]->ass);
  1625. av_freep(&sub->rects[i]);
  1626. }
  1627. av_freep(&sub->rects);
  1628. memset(sub, 0, sizeof(AVSubtitle));
  1629. }
  1630. av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
  1631. {
  1632. int ret = 0;
  1633. ff_unlock_avcodec();
  1634. ret = avcodec_close(avctx);
  1635. ff_lock_avcodec(NULL);
  1636. return ret;
  1637. }
  1638. av_cold int avcodec_close(AVCodecContext *avctx)
  1639. {
  1640. int ret = ff_lock_avcodec(avctx);
  1641. if (ret < 0)
  1642. return ret;
  1643. if (avcodec_is_open(avctx)) {
  1644. if (HAVE_THREADS && avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  1645. ff_unlock_avcodec();
  1646. ff_frame_thread_encoder_free(avctx);
  1647. ff_lock_avcodec(avctx);
  1648. }
  1649. if (HAVE_THREADS && avctx->thread_opaque)
  1650. ff_thread_free(avctx);
  1651. if (avctx->codec && avctx->codec->close)
  1652. avctx->codec->close(avctx);
  1653. avcodec_default_free_buffers(avctx);
  1654. avctx->coded_frame = NULL;
  1655. avctx->internal->byte_buffer_size = 0;
  1656. av_freep(&avctx->internal->byte_buffer);
  1657. av_freep(&avctx->internal);
  1658. av_dict_free(&avctx->metadata);
  1659. }
  1660. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1661. av_opt_free(avctx->priv_data);
  1662. av_opt_free(avctx);
  1663. av_freep(&avctx->priv_data);
  1664. if (av_codec_is_encoder(avctx->codec))
  1665. av_freep(&avctx->extradata);
  1666. avctx->codec = NULL;
  1667. avctx->active_thread_type = 0;
  1668. ff_unlock_avcodec();
  1669. return 0;
  1670. }
  1671. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  1672. {
  1673. switch(id){
  1674. //This is for future deprecatec codec ids, its empty since
  1675. //last major bump but will fill up again over time, please don't remove it
  1676. // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
  1677. case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
  1678. case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
  1679. default : return id;
  1680. }
  1681. }
  1682. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  1683. {
  1684. AVCodec *p, *experimental = NULL;
  1685. p = first_avcodec;
  1686. id= remap_deprecated_codec_id(id);
  1687. while (p) {
  1688. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  1689. p->id == id) {
  1690. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1691. experimental = p;
  1692. } else
  1693. return p;
  1694. }
  1695. p = p->next;
  1696. }
  1697. return experimental;
  1698. }
  1699. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1700. {
  1701. return find_encdec(id, 1);
  1702. }
  1703. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1704. {
  1705. AVCodec *p;
  1706. if (!name)
  1707. return NULL;
  1708. p = first_avcodec;
  1709. while (p) {
  1710. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1711. return p;
  1712. p = p->next;
  1713. }
  1714. return NULL;
  1715. }
  1716. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1717. {
  1718. return find_encdec(id, 0);
  1719. }
  1720. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1721. {
  1722. AVCodec *p;
  1723. if (!name)
  1724. return NULL;
  1725. p = first_avcodec;
  1726. while (p) {
  1727. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1728. return p;
  1729. p = p->next;
  1730. }
  1731. return NULL;
  1732. }
  1733. const char *avcodec_get_name(enum AVCodecID id)
  1734. {
  1735. const AVCodecDescriptor *cd;
  1736. AVCodec *codec;
  1737. if (id == AV_CODEC_ID_NONE)
  1738. return "none";
  1739. cd = avcodec_descriptor_get(id);
  1740. if (cd)
  1741. return cd->name;
  1742. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1743. codec = avcodec_find_decoder(id);
  1744. if (codec)
  1745. return codec->name;
  1746. codec = avcodec_find_encoder(id);
  1747. if (codec)
  1748. return codec->name;
  1749. return "unknown_codec";
  1750. }
  1751. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1752. {
  1753. int i, len, ret = 0;
  1754. #define IS_PRINT(x) \
  1755. (((x) >= '0' && (x) <= '9') || \
  1756. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1757. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  1758. for (i = 0; i < 4; i++) {
  1759. len = snprintf(buf, buf_size,
  1760. IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1761. buf += len;
  1762. buf_size = buf_size > len ? buf_size - len : 0;
  1763. ret += len;
  1764. codec_tag >>= 8;
  1765. }
  1766. return ret;
  1767. }
  1768. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1769. {
  1770. const char *codec_type;
  1771. const char *codec_name;
  1772. const char *profile = NULL;
  1773. const AVCodec *p;
  1774. int bitrate;
  1775. AVRational display_aspect_ratio;
  1776. if (!buf || buf_size <= 0)
  1777. return;
  1778. codec_type = av_get_media_type_string(enc->codec_type);
  1779. codec_name = avcodec_get_name(enc->codec_id);
  1780. if (enc->profile != FF_PROFILE_UNKNOWN) {
  1781. if (enc->codec)
  1782. p = enc->codec;
  1783. else
  1784. p = encode ? avcodec_find_encoder(enc->codec_id) :
  1785. avcodec_find_decoder(enc->codec_id);
  1786. if (p)
  1787. profile = av_get_profile_name(p, enc->profile);
  1788. }
  1789. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  1790. codec_name, enc->mb_decision ? " (hq)" : "");
  1791. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1792. if (profile)
  1793. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1794. if (enc->codec_tag) {
  1795. char tag_buf[32];
  1796. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1797. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1798. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  1799. }
  1800. switch (enc->codec_type) {
  1801. case AVMEDIA_TYPE_VIDEO:
  1802. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  1803. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1804. ", %s",
  1805. av_get_pix_fmt_name(enc->pix_fmt));
  1806. if (enc->bits_per_raw_sample &&
  1807. enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
  1808. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1809. " (%d bpc)", enc->bits_per_raw_sample);
  1810. }
  1811. if (enc->width) {
  1812. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1813. ", %dx%d",
  1814. enc->width, enc->height);
  1815. if (enc->sample_aspect_ratio.num) {
  1816. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1817. enc->width * enc->sample_aspect_ratio.num,
  1818. enc->height * enc->sample_aspect_ratio.den,
  1819. 1024 * 1024);
  1820. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1821. " [SAR %d:%d DAR %d:%d]",
  1822. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1823. display_aspect_ratio.num, display_aspect_ratio.den);
  1824. }
  1825. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1826. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1827. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1828. ", %d/%d",
  1829. enc->time_base.num / g, enc->time_base.den / g);
  1830. }
  1831. }
  1832. if (encode) {
  1833. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1834. ", q=%d-%d", enc->qmin, enc->qmax);
  1835. }
  1836. break;
  1837. case AVMEDIA_TYPE_AUDIO:
  1838. if (enc->sample_rate) {
  1839. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1840. ", %d Hz", enc->sample_rate);
  1841. }
  1842. av_strlcat(buf, ", ", buf_size);
  1843. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1844. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1845. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1846. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1847. }
  1848. break;
  1849. case AVMEDIA_TYPE_DATA:
  1850. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1851. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1852. if (g)
  1853. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1854. ", %d/%d",
  1855. enc->time_base.num / g, enc->time_base.den / g);
  1856. }
  1857. break;
  1858. default:
  1859. return;
  1860. }
  1861. if (encode) {
  1862. if (enc->flags & CODEC_FLAG_PASS1)
  1863. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1864. ", pass 1");
  1865. if (enc->flags & CODEC_FLAG_PASS2)
  1866. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1867. ", pass 2");
  1868. }
  1869. bitrate = get_bit_rate(enc);
  1870. if (bitrate != 0) {
  1871. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1872. ", %d kb/s", bitrate / 1000);
  1873. }
  1874. }
  1875. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1876. {
  1877. const AVProfile *p;
  1878. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1879. return NULL;
  1880. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1881. if (p->profile == profile)
  1882. return p->name;
  1883. return NULL;
  1884. }
  1885. unsigned avcodec_version(void)
  1886. {
  1887. // av_assert0(AV_CODEC_ID_V410==164);
  1888. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  1889. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  1890. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  1891. av_assert0(AV_CODEC_ID_SRT==94216);
  1892. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1893. return LIBAVCODEC_VERSION_INT;
  1894. }
  1895. const char *avcodec_configuration(void)
  1896. {
  1897. return FFMPEG_CONFIGURATION;
  1898. }
  1899. const char *avcodec_license(void)
  1900. {
  1901. #define LICENSE_PREFIX "libavcodec license: "
  1902. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1903. }
  1904. void avcodec_flush_buffers(AVCodecContext *avctx)
  1905. {
  1906. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1907. ff_thread_flush(avctx);
  1908. else if (avctx->codec->flush)
  1909. avctx->codec->flush(avctx);
  1910. avctx->pts_correction_last_pts =
  1911. avctx->pts_correction_last_dts = INT64_MIN;
  1912. }
  1913. static void video_free_buffers(AVCodecContext *s)
  1914. {
  1915. AVCodecInternal *avci = s->internal;
  1916. int i, j;
  1917. if (!avci->buffer)
  1918. return;
  1919. if (avci->buffer_count)
  1920. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1921. avci->buffer_count);
  1922. for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
  1923. InternalBuffer *buf = &avci->buffer[i];
  1924. for (j = 0; j < 4; j++) {
  1925. av_freep(&buf->base[j]);
  1926. buf->data[j] = NULL;
  1927. }
  1928. }
  1929. av_freep(&avci->buffer);
  1930. avci->buffer_count = 0;
  1931. }
  1932. static void audio_free_buffers(AVCodecContext *avctx)
  1933. {
  1934. AVCodecInternal *avci = avctx->internal;
  1935. av_freep(&avci->audio_data);
  1936. }
  1937. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1938. {
  1939. switch (avctx->codec_type) {
  1940. case AVMEDIA_TYPE_VIDEO:
  1941. video_free_buffers(avctx);
  1942. break;
  1943. case AVMEDIA_TYPE_AUDIO:
  1944. audio_free_buffers(avctx);
  1945. break;
  1946. default:
  1947. break;
  1948. }
  1949. }
  1950. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1951. {
  1952. switch (codec_id) {
  1953. case AV_CODEC_ID_8SVX_EXP:
  1954. case AV_CODEC_ID_8SVX_FIB:
  1955. case AV_CODEC_ID_ADPCM_CT:
  1956. case AV_CODEC_ID_ADPCM_IMA_APC:
  1957. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1958. case AV_CODEC_ID_ADPCM_IMA_OKI:
  1959. case AV_CODEC_ID_ADPCM_IMA_WS:
  1960. case AV_CODEC_ID_ADPCM_G722:
  1961. case AV_CODEC_ID_ADPCM_YAMAHA:
  1962. return 4;
  1963. case AV_CODEC_ID_PCM_ALAW:
  1964. case AV_CODEC_ID_PCM_MULAW:
  1965. case AV_CODEC_ID_PCM_S8:
  1966. case AV_CODEC_ID_PCM_S8_PLANAR:
  1967. case AV_CODEC_ID_PCM_U8:
  1968. case AV_CODEC_ID_PCM_ZORK:
  1969. return 8;
  1970. case AV_CODEC_ID_PCM_S16BE:
  1971. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  1972. case AV_CODEC_ID_PCM_S16LE:
  1973. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1974. case AV_CODEC_ID_PCM_U16BE:
  1975. case AV_CODEC_ID_PCM_U16LE:
  1976. return 16;
  1977. case AV_CODEC_ID_PCM_S24DAUD:
  1978. case AV_CODEC_ID_PCM_S24BE:
  1979. case AV_CODEC_ID_PCM_S24LE:
  1980. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  1981. case AV_CODEC_ID_PCM_U24BE:
  1982. case AV_CODEC_ID_PCM_U24LE:
  1983. return 24;
  1984. case AV_CODEC_ID_PCM_S32BE:
  1985. case AV_CODEC_ID_PCM_S32LE:
  1986. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  1987. case AV_CODEC_ID_PCM_U32BE:
  1988. case AV_CODEC_ID_PCM_U32LE:
  1989. case AV_CODEC_ID_PCM_F32BE:
  1990. case AV_CODEC_ID_PCM_F32LE:
  1991. return 32;
  1992. case AV_CODEC_ID_PCM_F64BE:
  1993. case AV_CODEC_ID_PCM_F64LE:
  1994. return 64;
  1995. default:
  1996. return 0;
  1997. }
  1998. }
  1999. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  2000. {
  2001. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  2002. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2003. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2004. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2005. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2006. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2007. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2008. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2009. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2010. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2011. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2012. };
  2013. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  2014. return AV_CODEC_ID_NONE;
  2015. if (be < 0 || be > 1)
  2016. be = AV_NE(1, 0);
  2017. return map[fmt][be];
  2018. }
  2019. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2020. {
  2021. switch (codec_id) {
  2022. case AV_CODEC_ID_ADPCM_SBPRO_2:
  2023. return 2;
  2024. case AV_CODEC_ID_ADPCM_SBPRO_3:
  2025. return 3;
  2026. case AV_CODEC_ID_ADPCM_SBPRO_4:
  2027. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2028. case AV_CODEC_ID_ADPCM_IMA_QT:
  2029. case AV_CODEC_ID_ADPCM_SWF:
  2030. case AV_CODEC_ID_ADPCM_MS:
  2031. return 4;
  2032. default:
  2033. return av_get_exact_bits_per_sample(codec_id);
  2034. }
  2035. }
  2036. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2037. {
  2038. int id, sr, ch, ba, tag, bps;
  2039. id = avctx->codec_id;
  2040. sr = avctx->sample_rate;
  2041. ch = avctx->channels;
  2042. ba = avctx->block_align;
  2043. tag = avctx->codec_tag;
  2044. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  2045. /* codecs with an exact constant bits per sample */
  2046. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  2047. return (frame_bytes * 8LL) / (bps * ch);
  2048. bps = avctx->bits_per_coded_sample;
  2049. /* codecs with a fixed packet duration */
  2050. switch (id) {
  2051. case AV_CODEC_ID_ADPCM_ADX: return 32;
  2052. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  2053. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  2054. case AV_CODEC_ID_AMR_NB:
  2055. case AV_CODEC_ID_EVRC:
  2056. case AV_CODEC_ID_GSM:
  2057. case AV_CODEC_ID_QCELP:
  2058. case AV_CODEC_ID_RA_288: return 160;
  2059. case AV_CODEC_ID_AMR_WB:
  2060. case AV_CODEC_ID_GSM_MS: return 320;
  2061. case AV_CODEC_ID_MP1: return 384;
  2062. case AV_CODEC_ID_ATRAC1: return 512;
  2063. case AV_CODEC_ID_ATRAC3: return 1024;
  2064. case AV_CODEC_ID_MP2:
  2065. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2066. case AV_CODEC_ID_AC3: return 1536;
  2067. }
  2068. if (sr > 0) {
  2069. /* calc from sample rate */
  2070. if (id == AV_CODEC_ID_TTA)
  2071. return 256 * sr / 245;
  2072. if (ch > 0) {
  2073. /* calc from sample rate and channels */
  2074. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2075. return (480 << (sr / 22050)) / ch;
  2076. }
  2077. }
  2078. if (ba > 0) {
  2079. /* calc from block_align */
  2080. if (id == AV_CODEC_ID_SIPR) {
  2081. switch (ba) {
  2082. case 20: return 160;
  2083. case 19: return 144;
  2084. case 29: return 288;
  2085. case 37: return 480;
  2086. }
  2087. } else if (id == AV_CODEC_ID_ILBC) {
  2088. switch (ba) {
  2089. case 38: return 160;
  2090. case 50: return 240;
  2091. }
  2092. }
  2093. }
  2094. if (frame_bytes > 0) {
  2095. /* calc from frame_bytes only */
  2096. if (id == AV_CODEC_ID_TRUESPEECH)
  2097. return 240 * (frame_bytes / 32);
  2098. if (id == AV_CODEC_ID_NELLYMOSER)
  2099. return 256 * (frame_bytes / 64);
  2100. if (id == AV_CODEC_ID_RA_144)
  2101. return 160 * (frame_bytes / 20);
  2102. if (id == AV_CODEC_ID_G723_1)
  2103. return 240 * (frame_bytes / 24);
  2104. if (bps > 0) {
  2105. /* calc from frame_bytes and bits_per_coded_sample */
  2106. if (id == AV_CODEC_ID_ADPCM_G726)
  2107. return frame_bytes * 8 / bps;
  2108. }
  2109. if (ch > 0) {
  2110. /* calc from frame_bytes and channels */
  2111. switch (id) {
  2112. case AV_CODEC_ID_ADPCM_AFC:
  2113. return frame_bytes / (9 * ch) * 16;
  2114. case AV_CODEC_ID_ADPCM_4XM:
  2115. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2116. return (frame_bytes - 4 * ch) * 2 / ch;
  2117. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2118. return (frame_bytes - 4) * 2 / ch;
  2119. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2120. return (frame_bytes - 8) * 2 / ch;
  2121. case AV_CODEC_ID_ADPCM_XA:
  2122. return (frame_bytes / 128) * 224 / ch;
  2123. case AV_CODEC_ID_INTERPLAY_DPCM:
  2124. return (frame_bytes - 6 - ch) / ch;
  2125. case AV_CODEC_ID_ROQ_DPCM:
  2126. return (frame_bytes - 8) / ch;
  2127. case AV_CODEC_ID_XAN_DPCM:
  2128. return (frame_bytes - 2 * ch) / ch;
  2129. case AV_CODEC_ID_MACE3:
  2130. return 3 * frame_bytes / ch;
  2131. case AV_CODEC_ID_MACE6:
  2132. return 6 * frame_bytes / ch;
  2133. case AV_CODEC_ID_PCM_LXF:
  2134. return 2 * (frame_bytes / (5 * ch));
  2135. case AV_CODEC_ID_IAC:
  2136. case AV_CODEC_ID_IMC:
  2137. return 4 * frame_bytes / ch;
  2138. }
  2139. if (tag) {
  2140. /* calc from frame_bytes, channels, and codec_tag */
  2141. if (id == AV_CODEC_ID_SOL_DPCM) {
  2142. if (tag == 3)
  2143. return frame_bytes / ch;
  2144. else
  2145. return frame_bytes * 2 / ch;
  2146. }
  2147. }
  2148. if (ba > 0) {
  2149. /* calc from frame_bytes, channels, and block_align */
  2150. int blocks = frame_bytes / ba;
  2151. switch (avctx->codec_id) {
  2152. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2153. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  2154. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2155. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2156. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2157. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2158. case AV_CODEC_ID_ADPCM_MS:
  2159. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2160. }
  2161. }
  2162. if (bps > 0) {
  2163. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2164. switch (avctx->codec_id) {
  2165. case AV_CODEC_ID_PCM_DVD:
  2166. if(bps<4)
  2167. return 0;
  2168. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2169. case AV_CODEC_ID_PCM_BLURAY:
  2170. if(bps<4)
  2171. return 0;
  2172. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2173. case AV_CODEC_ID_S302M:
  2174. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2175. }
  2176. }
  2177. }
  2178. }
  2179. return 0;
  2180. }
  2181. #if !HAVE_THREADS
  2182. int ff_thread_init(AVCodecContext *s)
  2183. {
  2184. return -1;
  2185. }
  2186. #endif
  2187. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2188. {
  2189. unsigned int n = 0;
  2190. while (v >= 0xff) {
  2191. *s++ = 0xff;
  2192. v -= 0xff;
  2193. n++;
  2194. }
  2195. *s = v;
  2196. n++;
  2197. return n;
  2198. }
  2199. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2200. {
  2201. int i;
  2202. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2203. return i;
  2204. }
  2205. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2206. {
  2207. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  2208. "version to the newest one from Git. If the problem still "
  2209. "occurs, it means that your file has a feature which has not "
  2210. "been implemented.\n", feature);
  2211. if(want_sample)
  2212. av_log_ask_for_sample(avc, NULL);
  2213. }
  2214. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2215. {
  2216. va_list argument_list;
  2217. va_start(argument_list, msg);
  2218. if (msg)
  2219. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2220. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2221. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  2222. "and contact the ffmpeg-devel mailing list.\n");
  2223. va_end(argument_list);
  2224. }
  2225. static AVHWAccel *first_hwaccel = NULL;
  2226. void av_register_hwaccel(AVHWAccel *hwaccel)
  2227. {
  2228. AVHWAccel **p = &first_hwaccel;
  2229. while (*p)
  2230. p = &(*p)->next;
  2231. *p = hwaccel;
  2232. hwaccel->next = NULL;
  2233. }
  2234. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  2235. {
  2236. return hwaccel ? hwaccel->next : first_hwaccel;
  2237. }
  2238. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
  2239. {
  2240. AVHWAccel *hwaccel = NULL;
  2241. while ((hwaccel = av_hwaccel_next(hwaccel)))
  2242. if (hwaccel->id == codec_id
  2243. && hwaccel->pix_fmt == pix_fmt)
  2244. return hwaccel;
  2245. return NULL;
  2246. }
  2247. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2248. {
  2249. if (ff_lockmgr_cb) {
  2250. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  2251. return -1;
  2252. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  2253. return -1;
  2254. }
  2255. ff_lockmgr_cb = cb;
  2256. if (ff_lockmgr_cb) {
  2257. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  2258. return -1;
  2259. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2260. return -1;
  2261. }
  2262. return 0;
  2263. }
  2264. int ff_lock_avcodec(AVCodecContext *log_ctx)
  2265. {
  2266. if (ff_lockmgr_cb) {
  2267. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  2268. return -1;
  2269. }
  2270. entangled_thread_counter++;
  2271. if (entangled_thread_counter != 1) {
  2272. av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
  2273. ff_avcodec_locked = 1;
  2274. ff_unlock_avcodec();
  2275. return AVERROR(EINVAL);
  2276. }
  2277. av_assert0(!ff_avcodec_locked);
  2278. ff_avcodec_locked = 1;
  2279. return 0;
  2280. }
  2281. int ff_unlock_avcodec(void)
  2282. {
  2283. av_assert0(ff_avcodec_locked);
  2284. ff_avcodec_locked = 0;
  2285. entangled_thread_counter--;
  2286. if (ff_lockmgr_cb) {
  2287. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
  2288. return -1;
  2289. }
  2290. return 0;
  2291. }
  2292. int avpriv_lock_avformat(void)
  2293. {
  2294. if (ff_lockmgr_cb) {
  2295. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2296. return -1;
  2297. }
  2298. return 0;
  2299. }
  2300. int avpriv_unlock_avformat(void)
  2301. {
  2302. if (ff_lockmgr_cb) {
  2303. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2304. return -1;
  2305. }
  2306. return 0;
  2307. }
  2308. unsigned int avpriv_toupper4(unsigned int x)
  2309. {
  2310. return toupper(x & 0xFF)
  2311. + (toupper((x >> 8) & 0xFF) << 8)
  2312. + (toupper((x >> 16) & 0xFF) << 16)
  2313. + (toupper((x >> 24) & 0xFF) << 24);
  2314. }
  2315. #if !HAVE_THREADS
  2316. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  2317. {
  2318. f->owner = avctx;
  2319. return ff_get_buffer(avctx, f);
  2320. }
  2321. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  2322. {
  2323. f->owner->release_buffer(f->owner, f);
  2324. }
  2325. void ff_thread_finish_setup(AVCodecContext *avctx)
  2326. {
  2327. }
  2328. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  2329. {
  2330. }
  2331. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  2332. {
  2333. }
  2334. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2335. {
  2336. return 1;
  2337. }
  2338. #endif
  2339. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  2340. {
  2341. AVCodec *c= avcodec_find_decoder(codec_id);
  2342. if(!c)
  2343. c= avcodec_find_encoder(codec_id);
  2344. if(c)
  2345. return c->type;
  2346. if (codec_id <= AV_CODEC_ID_NONE)
  2347. return AVMEDIA_TYPE_UNKNOWN;
  2348. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  2349. return AVMEDIA_TYPE_VIDEO;
  2350. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  2351. return AVMEDIA_TYPE_AUDIO;
  2352. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  2353. return AVMEDIA_TYPE_SUBTITLE;
  2354. return AVMEDIA_TYPE_UNKNOWN;
  2355. }
  2356. int avcodec_is_open(AVCodecContext *s)
  2357. {
  2358. return !!s->internal;
  2359. }
  2360. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
  2361. {
  2362. int ret;
  2363. char *str;
  2364. ret = av_bprint_finalize(buf, &str);
  2365. if (ret < 0)
  2366. return ret;
  2367. avctx->extradata = str;
  2368. /* Note: the string is NUL terminated (so extradata can be read as a
  2369. * string), but the ending character is not accounted in the size (in
  2370. * binary formats you are likely not supposed to mux that character). When
  2371. * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
  2372. * zeros. */
  2373. avctx->extradata_size = buf->len;
  2374. return 0;
  2375. }