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.

2678 lines
85KB

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