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.

2706 lines
86KB

  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. }
  888. avctx->pts_correction_num_faulty_pts =
  889. avctx->pts_correction_num_faulty_dts = 0;
  890. avctx->pts_correction_last_pts =
  891. avctx->pts_correction_last_dts = INT64_MIN;
  892. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  893. || avctx->internal->frame_thread_encoder)) {
  894. ret = avctx->codec->init(avctx);
  895. if (ret < 0) {
  896. goto free_and_end;
  897. }
  898. }
  899. ret=0;
  900. if (av_codec_is_decoder(avctx->codec)) {
  901. if (!avctx->bit_rate)
  902. avctx->bit_rate = get_bit_rate(avctx);
  903. /* validate channel layout from the decoder */
  904. if (avctx->channel_layout) {
  905. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  906. if (!avctx->channels)
  907. avctx->channels = channels;
  908. else if (channels != avctx->channels) {
  909. char buf[512];
  910. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  911. av_log(avctx, AV_LOG_WARNING,
  912. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  913. "ignoring specified channel layout\n",
  914. buf, channels, avctx->channels);
  915. avctx->channel_layout = 0;
  916. }
  917. }
  918. if (avctx->channels && avctx->channels < 0 ||
  919. avctx->channels > FF_SANE_NB_CHANNELS) {
  920. ret = AVERROR(EINVAL);
  921. goto free_and_end;
  922. }
  923. }
  924. end:
  925. ff_unlock_avcodec();
  926. if (options) {
  927. av_dict_free(options);
  928. *options = tmp;
  929. }
  930. return ret;
  931. free_and_end:
  932. av_dict_free(&tmp);
  933. av_freep(&avctx->priv_data);
  934. av_freep(&avctx->internal);
  935. avctx->codec = NULL;
  936. goto end;
  937. }
  938. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
  939. {
  940. if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  941. av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
  942. return AVERROR(EINVAL);
  943. }
  944. if (avctx) {
  945. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  946. if (!avpkt->data || avpkt->size < size) {
  947. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  948. avpkt->data = avctx->internal->byte_buffer;
  949. avpkt->size = avctx->internal->byte_buffer_size;
  950. avpkt->destruct = NULL;
  951. }
  952. }
  953. if (avpkt->data) {
  954. void *destruct = avpkt->destruct;
  955. if (avpkt->size < size) {
  956. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
  957. return AVERROR(EINVAL);
  958. }
  959. av_init_packet(avpkt);
  960. avpkt->destruct = destruct;
  961. avpkt->size = size;
  962. return 0;
  963. } else {
  964. int ret = av_new_packet(avpkt, size);
  965. if (ret < 0)
  966. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
  967. return ret;
  968. }
  969. }
  970. int ff_alloc_packet(AVPacket *avpkt, int size)
  971. {
  972. return ff_alloc_packet2(NULL, avpkt, size);
  973. }
  974. /**
  975. * Pad last frame with silence.
  976. */
  977. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  978. {
  979. AVFrame *frame = NULL;
  980. uint8_t *buf = NULL;
  981. int ret;
  982. if (!(frame = avcodec_alloc_frame()))
  983. return AVERROR(ENOMEM);
  984. *frame = *src;
  985. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  986. s->frame_size, s->sample_fmt, 0)) < 0)
  987. goto fail;
  988. if (!(buf = av_malloc(ret))) {
  989. ret = AVERROR(ENOMEM);
  990. goto fail;
  991. }
  992. frame->nb_samples = s->frame_size;
  993. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  994. buf, ret, 0)) < 0)
  995. goto fail;
  996. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  997. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  998. goto fail;
  999. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1000. frame->nb_samples - src->nb_samples,
  1001. s->channels, s->sample_fmt)) < 0)
  1002. goto fail;
  1003. *dst = frame;
  1004. return 0;
  1005. fail:
  1006. if (frame->extended_data != frame->data)
  1007. av_freep(&frame->extended_data);
  1008. av_freep(&buf);
  1009. av_freep(&frame);
  1010. return ret;
  1011. }
  1012. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1013. AVPacket *avpkt,
  1014. const AVFrame *frame,
  1015. int *got_packet_ptr)
  1016. {
  1017. AVFrame tmp;
  1018. AVFrame *padded_frame = NULL;
  1019. int ret;
  1020. AVPacket user_pkt = *avpkt;
  1021. int needs_realloc = !user_pkt.data;
  1022. *got_packet_ptr = 0;
  1023. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1024. av_free_packet(avpkt);
  1025. av_init_packet(avpkt);
  1026. return 0;
  1027. }
  1028. /* ensure that extended_data is properly set */
  1029. if (frame && !frame->extended_data) {
  1030. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1031. avctx->channels > AV_NUM_DATA_POINTERS) {
  1032. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1033. "with more than %d channels, but extended_data is not set.\n",
  1034. AV_NUM_DATA_POINTERS);
  1035. return AVERROR(EINVAL);
  1036. }
  1037. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1038. tmp = *frame;
  1039. tmp.extended_data = tmp.data;
  1040. frame = &tmp;
  1041. }
  1042. /* check for valid frame size */
  1043. if (frame) {
  1044. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1045. if (frame->nb_samples > avctx->frame_size) {
  1046. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1047. return AVERROR(EINVAL);
  1048. }
  1049. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1050. if (frame->nb_samples < avctx->frame_size &&
  1051. !avctx->internal->last_audio_frame) {
  1052. ret = pad_last_frame(avctx, &padded_frame, frame);
  1053. if (ret < 0)
  1054. return ret;
  1055. frame = padded_frame;
  1056. avctx->internal->last_audio_frame = 1;
  1057. }
  1058. if (frame->nb_samples != avctx->frame_size) {
  1059. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1060. ret = AVERROR(EINVAL);
  1061. goto end;
  1062. }
  1063. }
  1064. }
  1065. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1066. if (!ret) {
  1067. if (*got_packet_ptr) {
  1068. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1069. if (avpkt->pts == AV_NOPTS_VALUE)
  1070. avpkt->pts = frame->pts;
  1071. if (!avpkt->duration)
  1072. avpkt->duration = ff_samples_to_time_base(avctx,
  1073. frame->nb_samples);
  1074. }
  1075. avpkt->dts = avpkt->pts;
  1076. } else {
  1077. avpkt->size = 0;
  1078. }
  1079. }
  1080. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1081. needs_realloc = 0;
  1082. if (user_pkt.data) {
  1083. if (user_pkt.size >= avpkt->size) {
  1084. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1085. } else {
  1086. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1087. avpkt->size = user_pkt.size;
  1088. ret = -1;
  1089. }
  1090. avpkt->data = user_pkt.data;
  1091. avpkt->destruct = user_pkt.destruct;
  1092. } else {
  1093. if (av_dup_packet(avpkt) < 0) {
  1094. ret = AVERROR(ENOMEM);
  1095. }
  1096. }
  1097. }
  1098. if (!ret) {
  1099. if (needs_realloc && avpkt->data) {
  1100. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1101. if (new_data)
  1102. avpkt->data = new_data;
  1103. }
  1104. avctx->frame_number++;
  1105. }
  1106. if (ret < 0 || !*got_packet_ptr) {
  1107. av_free_packet(avpkt);
  1108. av_init_packet(avpkt);
  1109. goto end;
  1110. }
  1111. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1112. * this needs to be moved to the encoders, but for now we can do it
  1113. * here to simplify things */
  1114. avpkt->flags |= AV_PKT_FLAG_KEY;
  1115. end:
  1116. if (padded_frame) {
  1117. av_freep(&padded_frame->data[0]);
  1118. if (padded_frame->extended_data != padded_frame->data)
  1119. av_freep(&padded_frame->extended_data);
  1120. av_freep(&padded_frame);
  1121. }
  1122. return ret;
  1123. }
  1124. #if FF_API_OLD_ENCODE_AUDIO
  1125. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1126. uint8_t *buf, int buf_size,
  1127. const short *samples)
  1128. {
  1129. AVPacket pkt;
  1130. AVFrame frame0 = {{0}};
  1131. AVFrame *frame;
  1132. int ret, samples_size, got_packet;
  1133. av_init_packet(&pkt);
  1134. pkt.data = buf;
  1135. pkt.size = buf_size;
  1136. if (samples) {
  1137. frame = &frame0;
  1138. avcodec_get_frame_defaults(frame);
  1139. if (avctx->frame_size) {
  1140. frame->nb_samples = avctx->frame_size;
  1141. } else {
  1142. /* if frame_size is not set, the number of samples must be
  1143. * calculated from the buffer size */
  1144. int64_t nb_samples;
  1145. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1146. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1147. "support this codec\n");
  1148. return AVERROR(EINVAL);
  1149. }
  1150. nb_samples = (int64_t)buf_size * 8 /
  1151. (av_get_bits_per_sample(avctx->codec_id) *
  1152. avctx->channels);
  1153. if (nb_samples >= INT_MAX)
  1154. return AVERROR(EINVAL);
  1155. frame->nb_samples = nb_samples;
  1156. }
  1157. /* it is assumed that the samples buffer is large enough based on the
  1158. * relevant parameters */
  1159. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1160. frame->nb_samples,
  1161. avctx->sample_fmt, 1);
  1162. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1163. avctx->sample_fmt,
  1164. (const uint8_t *)samples,
  1165. samples_size, 1)) < 0)
  1166. return ret;
  1167. /* fabricate frame pts from sample count.
  1168. * this is needed because the avcodec_encode_audio() API does not have
  1169. * a way for the user to provide pts */
  1170. if (avctx->sample_rate && avctx->time_base.num)
  1171. frame->pts = ff_samples_to_time_base(avctx,
  1172. avctx->internal->sample_count);
  1173. else
  1174. frame->pts = AV_NOPTS_VALUE;
  1175. avctx->internal->sample_count += frame->nb_samples;
  1176. } else {
  1177. frame = NULL;
  1178. }
  1179. got_packet = 0;
  1180. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1181. if (!ret && got_packet && avctx->coded_frame) {
  1182. avctx->coded_frame->pts = pkt.pts;
  1183. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1184. }
  1185. /* free any side data since we cannot return it */
  1186. ff_packet_free_side_data(&pkt);
  1187. if (frame && frame->extended_data != frame->data)
  1188. av_freep(&frame->extended_data);
  1189. return ret ? ret : pkt.size;
  1190. }
  1191. #endif
  1192. #if FF_API_OLD_ENCODE_VIDEO
  1193. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1194. const AVFrame *pict)
  1195. {
  1196. AVPacket pkt;
  1197. int ret, got_packet = 0;
  1198. if (buf_size < FF_MIN_BUFFER_SIZE) {
  1199. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1200. return -1;
  1201. }
  1202. av_init_packet(&pkt);
  1203. pkt.data = buf;
  1204. pkt.size = buf_size;
  1205. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1206. if (!ret && got_packet && avctx->coded_frame) {
  1207. avctx->coded_frame->pts = pkt.pts;
  1208. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1209. }
  1210. /* free any side data since we cannot return it */
  1211. if (pkt.side_data_elems > 0) {
  1212. int i;
  1213. for (i = 0; i < pkt.side_data_elems; i++)
  1214. av_free(pkt.side_data[i].data);
  1215. av_freep(&pkt.side_data);
  1216. pkt.side_data_elems = 0;
  1217. }
  1218. return ret ? ret : pkt.size;
  1219. }
  1220. #endif
  1221. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1222. AVPacket *avpkt,
  1223. const AVFrame *frame,
  1224. int *got_packet_ptr)
  1225. {
  1226. int ret;
  1227. AVPacket user_pkt = *avpkt;
  1228. int needs_realloc = !user_pkt.data;
  1229. *got_packet_ptr = 0;
  1230. if(HAVE_THREADS && avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1231. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1232. if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
  1233. avctx->stats_out[0] = '\0';
  1234. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1235. av_free_packet(avpkt);
  1236. av_init_packet(avpkt);
  1237. avpkt->size = 0;
  1238. return 0;
  1239. }
  1240. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1241. return AVERROR(EINVAL);
  1242. av_assert0(avctx->codec->encode2);
  1243. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1244. av_assert0(ret <= 0);
  1245. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1246. needs_realloc = 0;
  1247. if (user_pkt.data) {
  1248. if (user_pkt.size >= avpkt->size) {
  1249. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1250. } else {
  1251. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1252. avpkt->size = user_pkt.size;
  1253. ret = -1;
  1254. }
  1255. avpkt->data = user_pkt.data;
  1256. avpkt->destruct = user_pkt.destruct;
  1257. } else {
  1258. if (av_dup_packet(avpkt) < 0) {
  1259. ret = AVERROR(ENOMEM);
  1260. }
  1261. }
  1262. }
  1263. if (!ret) {
  1264. if (!*got_packet_ptr)
  1265. avpkt->size = 0;
  1266. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1267. avpkt->pts = avpkt->dts = frame->pts;
  1268. if (needs_realloc && avpkt->data &&
  1269. avpkt->destruct == av_destruct_packet) {
  1270. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1271. if (new_data)
  1272. avpkt->data = new_data;
  1273. }
  1274. avctx->frame_number++;
  1275. }
  1276. if (ret < 0 || !*got_packet_ptr)
  1277. av_free_packet(avpkt);
  1278. emms_c();
  1279. return ret;
  1280. }
  1281. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1282. const AVSubtitle *sub)
  1283. {
  1284. int ret;
  1285. if (sub->start_display_time) {
  1286. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1287. return -1;
  1288. }
  1289. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1290. avctx->frame_number++;
  1291. return ret;
  1292. }
  1293. /**
  1294. * Attempt to guess proper monotonic timestamps for decoded video frames
  1295. * which might have incorrect times. Input timestamps may wrap around, in
  1296. * which case the output will as well.
  1297. *
  1298. * @param pts the pts field of the decoded AVPacket, as passed through
  1299. * AVFrame.pkt_pts
  1300. * @param dts the dts field of the decoded AVPacket
  1301. * @return one of the input values, may be AV_NOPTS_VALUE
  1302. */
  1303. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1304. int64_t reordered_pts, int64_t dts)
  1305. {
  1306. int64_t pts = AV_NOPTS_VALUE;
  1307. if (dts != AV_NOPTS_VALUE) {
  1308. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1309. ctx->pts_correction_last_dts = dts;
  1310. }
  1311. if (reordered_pts != AV_NOPTS_VALUE) {
  1312. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1313. ctx->pts_correction_last_pts = reordered_pts;
  1314. }
  1315. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1316. && reordered_pts != AV_NOPTS_VALUE)
  1317. pts = reordered_pts;
  1318. else
  1319. pts = dts;
  1320. return pts;
  1321. }
  1322. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1323. {
  1324. int size = 0;
  1325. const uint8_t *data;
  1326. uint32_t flags;
  1327. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1328. return;
  1329. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1330. if (!data || size < 4)
  1331. return;
  1332. flags = bytestream_get_le32(&data);
  1333. size -= 4;
  1334. if (size < 4) /* Required for any of the changes */
  1335. return;
  1336. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1337. avctx->channels = bytestream_get_le32(&data);
  1338. size -= 4;
  1339. }
  1340. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1341. if (size < 8)
  1342. return;
  1343. avctx->channel_layout = bytestream_get_le64(&data);
  1344. size -= 8;
  1345. }
  1346. if (size < 4)
  1347. return;
  1348. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1349. avctx->sample_rate = bytestream_get_le32(&data);
  1350. size -= 4;
  1351. }
  1352. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1353. if (size < 8)
  1354. return;
  1355. avctx->width = bytestream_get_le32(&data);
  1356. avctx->height = bytestream_get_le32(&data);
  1357. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1358. size -= 8;
  1359. }
  1360. }
  1361. static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
  1362. {
  1363. int size, ret = 0;
  1364. const uint8_t *side_metadata;
  1365. const uint8_t *end;
  1366. av_dict_free(&avctx->metadata);
  1367. side_metadata = av_packet_get_side_data(avctx->pkt,
  1368. AV_PKT_DATA_STRINGS_METADATA, &size);
  1369. if (!side_metadata)
  1370. goto end;
  1371. end = side_metadata + size;
  1372. while (side_metadata < end) {
  1373. const uint8_t *key = side_metadata;
  1374. const uint8_t *val = side_metadata + strlen(key) + 1;
  1375. int ret = av_dict_set(&frame->metadata, key, val, 0);
  1376. if (ret < 0)
  1377. break;
  1378. side_metadata = val + strlen(val) + 1;
  1379. }
  1380. end:
  1381. avctx->metadata = frame->metadata;
  1382. return ret;
  1383. }
  1384. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1385. int *got_picture_ptr,
  1386. const AVPacket *avpkt)
  1387. {
  1388. int ret;
  1389. // copy to ensure we do not change avpkt
  1390. AVPacket tmp = *avpkt;
  1391. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1392. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1393. return AVERROR(EINVAL);
  1394. }
  1395. *got_picture_ptr = 0;
  1396. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1397. return AVERROR(EINVAL);
  1398. avcodec_get_frame_defaults(picture);
  1399. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1400. int did_split = av_packet_split_side_data(&tmp);
  1401. apply_param_change(avctx, &tmp);
  1402. avctx->pkt = &tmp;
  1403. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1404. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1405. &tmp);
  1406. else {
  1407. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1408. &tmp);
  1409. picture->pkt_dts = avpkt->dts;
  1410. if(!avctx->has_b_frames){
  1411. picture->pkt_pos = avpkt->pos;
  1412. }
  1413. //FIXME these should be under if(!avctx->has_b_frames)
  1414. /* get_buffer is supposed to set frame parameters */
  1415. if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
  1416. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1417. if (!picture->width) picture->width = avctx->width;
  1418. if (!picture->height) picture->height = avctx->height;
  1419. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  1420. }
  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. ff_unlock_avcodec();
  1631. ret = avcodec_close(avctx);
  1632. ff_lock_avcodec(NULL);
  1633. return ret;
  1634. }
  1635. av_cold int avcodec_close(AVCodecContext *avctx)
  1636. {
  1637. int ret = ff_lock_avcodec(avctx);
  1638. if (ret < 0)
  1639. return ret;
  1640. if (avcodec_is_open(avctx)) {
  1641. if (HAVE_THREADS && avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  1642. ff_unlock_avcodec();
  1643. ff_frame_thread_encoder_free(avctx);
  1644. ff_lock_avcodec(avctx);
  1645. }
  1646. if (HAVE_THREADS && avctx->thread_opaque)
  1647. ff_thread_free(avctx);
  1648. if (avctx->codec && avctx->codec->close)
  1649. avctx->codec->close(avctx);
  1650. avcodec_default_free_buffers(avctx);
  1651. avctx->coded_frame = NULL;
  1652. avctx->internal->byte_buffer_size = 0;
  1653. av_freep(&avctx->internal->byte_buffer);
  1654. av_freep(&avctx->internal);
  1655. av_dict_free(&avctx->metadata);
  1656. }
  1657. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1658. av_opt_free(avctx->priv_data);
  1659. av_opt_free(avctx);
  1660. av_freep(&avctx->priv_data);
  1661. if (av_codec_is_encoder(avctx->codec))
  1662. av_freep(&avctx->extradata);
  1663. avctx->codec = NULL;
  1664. avctx->active_thread_type = 0;
  1665. ff_unlock_avcodec();
  1666. return 0;
  1667. }
  1668. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  1669. {
  1670. switch(id){
  1671. //This is for future deprecatec codec ids, its empty since
  1672. //last major bump but will fill up again over time, please don't remove it
  1673. // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
  1674. case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
  1675. case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
  1676. default : return id;
  1677. }
  1678. }
  1679. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  1680. {
  1681. AVCodec *p, *experimental = NULL;
  1682. p = first_avcodec;
  1683. id= remap_deprecated_codec_id(id);
  1684. while (p) {
  1685. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  1686. p->id == id) {
  1687. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1688. experimental = p;
  1689. } else
  1690. return p;
  1691. }
  1692. p = p->next;
  1693. }
  1694. return experimental;
  1695. }
  1696. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1697. {
  1698. return find_encdec(id, 1);
  1699. }
  1700. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1701. {
  1702. AVCodec *p;
  1703. if (!name)
  1704. return NULL;
  1705. p = first_avcodec;
  1706. while (p) {
  1707. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1708. return p;
  1709. p = p->next;
  1710. }
  1711. return NULL;
  1712. }
  1713. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1714. {
  1715. return find_encdec(id, 0);
  1716. }
  1717. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1718. {
  1719. AVCodec *p;
  1720. if (!name)
  1721. return NULL;
  1722. p = first_avcodec;
  1723. while (p) {
  1724. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1725. return p;
  1726. p = p->next;
  1727. }
  1728. return NULL;
  1729. }
  1730. const char *avcodec_get_name(enum AVCodecID id)
  1731. {
  1732. const AVCodecDescriptor *cd;
  1733. AVCodec *codec;
  1734. if (id == AV_CODEC_ID_NONE)
  1735. return "none";
  1736. cd = avcodec_descriptor_get(id);
  1737. if (cd)
  1738. return cd->name;
  1739. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1740. codec = avcodec_find_decoder(id);
  1741. if (codec)
  1742. return codec->name;
  1743. codec = avcodec_find_encoder(id);
  1744. if (codec)
  1745. return codec->name;
  1746. return "unknown_codec";
  1747. }
  1748. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1749. {
  1750. int i, len, ret = 0;
  1751. #define IS_PRINT(x) \
  1752. (((x) >= '0' && (x) <= '9') || \
  1753. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1754. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  1755. for (i = 0; i < 4; i++) {
  1756. len = snprintf(buf, buf_size,
  1757. IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1758. buf += len;
  1759. buf_size = buf_size > len ? buf_size - len : 0;
  1760. ret += len;
  1761. codec_tag >>= 8;
  1762. }
  1763. return ret;
  1764. }
  1765. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1766. {
  1767. const char *codec_type;
  1768. const char *codec_name;
  1769. const char *profile = NULL;
  1770. const AVCodec *p;
  1771. int bitrate;
  1772. AVRational display_aspect_ratio;
  1773. if (!buf || buf_size <= 0)
  1774. return;
  1775. codec_type = av_get_media_type_string(enc->codec_type);
  1776. codec_name = avcodec_get_name(enc->codec_id);
  1777. if (enc->profile != FF_PROFILE_UNKNOWN) {
  1778. if (enc->codec)
  1779. p = enc->codec;
  1780. else
  1781. p = encode ? avcodec_find_encoder(enc->codec_id) :
  1782. avcodec_find_decoder(enc->codec_id);
  1783. if (p)
  1784. profile = av_get_profile_name(p, enc->profile);
  1785. }
  1786. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  1787. codec_name, enc->mb_decision ? " (hq)" : "");
  1788. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1789. if (profile)
  1790. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1791. if (enc->codec_tag) {
  1792. char tag_buf[32];
  1793. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1794. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1795. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  1796. }
  1797. switch (enc->codec_type) {
  1798. case AVMEDIA_TYPE_VIDEO:
  1799. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  1800. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1801. ", %s",
  1802. av_get_pix_fmt_name(enc->pix_fmt));
  1803. if (enc->bits_per_raw_sample &&
  1804. enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
  1805. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1806. " (%d bpc)", enc->bits_per_raw_sample);
  1807. }
  1808. if (enc->width) {
  1809. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1810. ", %dx%d",
  1811. enc->width, enc->height);
  1812. if (enc->sample_aspect_ratio.num) {
  1813. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1814. enc->width * enc->sample_aspect_ratio.num,
  1815. enc->height * enc->sample_aspect_ratio.den,
  1816. 1024 * 1024);
  1817. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1818. " [SAR %d:%d DAR %d:%d]",
  1819. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1820. display_aspect_ratio.num, display_aspect_ratio.den);
  1821. }
  1822. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1823. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1824. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1825. ", %d/%d",
  1826. enc->time_base.num / g, enc->time_base.den / g);
  1827. }
  1828. }
  1829. if (encode) {
  1830. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1831. ", q=%d-%d", enc->qmin, enc->qmax);
  1832. }
  1833. break;
  1834. case AVMEDIA_TYPE_AUDIO:
  1835. if (enc->sample_rate) {
  1836. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1837. ", %d Hz", enc->sample_rate);
  1838. }
  1839. av_strlcat(buf, ", ", buf_size);
  1840. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1841. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1842. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1843. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1844. }
  1845. break;
  1846. default:
  1847. return;
  1848. }
  1849. if (encode) {
  1850. if (enc->flags & CODEC_FLAG_PASS1)
  1851. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1852. ", pass 1");
  1853. if (enc->flags & CODEC_FLAG_PASS2)
  1854. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1855. ", pass 2");
  1856. }
  1857. bitrate = get_bit_rate(enc);
  1858. if (bitrate != 0) {
  1859. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1860. ", %d kb/s", bitrate / 1000);
  1861. }
  1862. }
  1863. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1864. {
  1865. const AVProfile *p;
  1866. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1867. return NULL;
  1868. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1869. if (p->profile == profile)
  1870. return p->name;
  1871. return NULL;
  1872. }
  1873. unsigned avcodec_version(void)
  1874. {
  1875. // av_assert0(AV_CODEC_ID_V410==164);
  1876. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  1877. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  1878. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  1879. av_assert0(AV_CODEC_ID_SRT==94216);
  1880. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1881. return LIBAVCODEC_VERSION_INT;
  1882. }
  1883. const char *avcodec_configuration(void)
  1884. {
  1885. return FFMPEG_CONFIGURATION;
  1886. }
  1887. const char *avcodec_license(void)
  1888. {
  1889. #define LICENSE_PREFIX "libavcodec license: "
  1890. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1891. }
  1892. void avcodec_flush_buffers(AVCodecContext *avctx)
  1893. {
  1894. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1895. ff_thread_flush(avctx);
  1896. else if (avctx->codec->flush)
  1897. avctx->codec->flush(avctx);
  1898. avctx->pts_correction_last_pts =
  1899. avctx->pts_correction_last_dts = INT64_MIN;
  1900. }
  1901. static void video_free_buffers(AVCodecContext *s)
  1902. {
  1903. AVCodecInternal *avci = s->internal;
  1904. int i, j;
  1905. if (!avci->buffer)
  1906. return;
  1907. if (avci->buffer_count)
  1908. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1909. avci->buffer_count);
  1910. for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
  1911. InternalBuffer *buf = &avci->buffer[i];
  1912. for (j = 0; j < 4; j++) {
  1913. av_freep(&buf->base[j]);
  1914. buf->data[j] = NULL;
  1915. }
  1916. }
  1917. av_freep(&avci->buffer);
  1918. avci->buffer_count = 0;
  1919. }
  1920. static void audio_free_buffers(AVCodecContext *avctx)
  1921. {
  1922. AVCodecInternal *avci = avctx->internal;
  1923. av_freep(&avci->audio_data);
  1924. }
  1925. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1926. {
  1927. switch (avctx->codec_type) {
  1928. case AVMEDIA_TYPE_VIDEO:
  1929. video_free_buffers(avctx);
  1930. break;
  1931. case AVMEDIA_TYPE_AUDIO:
  1932. audio_free_buffers(avctx);
  1933. break;
  1934. default:
  1935. break;
  1936. }
  1937. }
  1938. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1939. {
  1940. switch (codec_id) {
  1941. case AV_CODEC_ID_8SVX_EXP:
  1942. case AV_CODEC_ID_8SVX_FIB:
  1943. case AV_CODEC_ID_ADPCM_CT:
  1944. case AV_CODEC_ID_ADPCM_IMA_APC:
  1945. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1946. case AV_CODEC_ID_ADPCM_IMA_OKI:
  1947. case AV_CODEC_ID_ADPCM_IMA_WS:
  1948. case AV_CODEC_ID_ADPCM_G722:
  1949. case AV_CODEC_ID_ADPCM_YAMAHA:
  1950. return 4;
  1951. case AV_CODEC_ID_PCM_ALAW:
  1952. case AV_CODEC_ID_PCM_MULAW:
  1953. case AV_CODEC_ID_PCM_S8:
  1954. case AV_CODEC_ID_PCM_S8_PLANAR:
  1955. case AV_CODEC_ID_PCM_U8:
  1956. case AV_CODEC_ID_PCM_ZORK:
  1957. return 8;
  1958. case AV_CODEC_ID_PCM_S16BE:
  1959. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  1960. case AV_CODEC_ID_PCM_S16LE:
  1961. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1962. case AV_CODEC_ID_PCM_U16BE:
  1963. case AV_CODEC_ID_PCM_U16LE:
  1964. return 16;
  1965. case AV_CODEC_ID_PCM_S24DAUD:
  1966. case AV_CODEC_ID_PCM_S24BE:
  1967. case AV_CODEC_ID_PCM_S24LE:
  1968. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  1969. case AV_CODEC_ID_PCM_U24BE:
  1970. case AV_CODEC_ID_PCM_U24LE:
  1971. return 24;
  1972. case AV_CODEC_ID_PCM_S32BE:
  1973. case AV_CODEC_ID_PCM_S32LE:
  1974. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  1975. case AV_CODEC_ID_PCM_U32BE:
  1976. case AV_CODEC_ID_PCM_U32LE:
  1977. case AV_CODEC_ID_PCM_F32BE:
  1978. case AV_CODEC_ID_PCM_F32LE:
  1979. return 32;
  1980. case AV_CODEC_ID_PCM_F64BE:
  1981. case AV_CODEC_ID_PCM_F64LE:
  1982. return 64;
  1983. default:
  1984. return 0;
  1985. }
  1986. }
  1987. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  1988. {
  1989. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  1990. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  1991. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  1992. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  1993. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  1994. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  1995. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  1996. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  1997. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  1998. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  1999. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2000. };
  2001. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  2002. return AV_CODEC_ID_NONE;
  2003. if (be < 0 || be > 1)
  2004. be = AV_NE(1, 0);
  2005. return map[fmt][be];
  2006. }
  2007. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2008. {
  2009. switch (codec_id) {
  2010. case AV_CODEC_ID_ADPCM_SBPRO_2:
  2011. return 2;
  2012. case AV_CODEC_ID_ADPCM_SBPRO_3:
  2013. return 3;
  2014. case AV_CODEC_ID_ADPCM_SBPRO_4:
  2015. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2016. case AV_CODEC_ID_ADPCM_IMA_QT:
  2017. case AV_CODEC_ID_ADPCM_SWF:
  2018. case AV_CODEC_ID_ADPCM_MS:
  2019. return 4;
  2020. default:
  2021. return av_get_exact_bits_per_sample(codec_id);
  2022. }
  2023. }
  2024. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2025. {
  2026. int id, sr, ch, ba, tag, bps;
  2027. id = avctx->codec_id;
  2028. sr = avctx->sample_rate;
  2029. ch = avctx->channels;
  2030. ba = avctx->block_align;
  2031. tag = avctx->codec_tag;
  2032. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  2033. /* codecs with an exact constant bits per sample */
  2034. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  2035. return (frame_bytes * 8LL) / (bps * ch);
  2036. bps = avctx->bits_per_coded_sample;
  2037. /* codecs with a fixed packet duration */
  2038. switch (id) {
  2039. case AV_CODEC_ID_ADPCM_ADX: return 32;
  2040. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  2041. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  2042. case AV_CODEC_ID_AMR_NB:
  2043. case AV_CODEC_ID_GSM:
  2044. case AV_CODEC_ID_QCELP:
  2045. case AV_CODEC_ID_RA_288: return 160;
  2046. case AV_CODEC_ID_AMR_WB:
  2047. case AV_CODEC_ID_GSM_MS: return 320;
  2048. case AV_CODEC_ID_MP1: return 384;
  2049. case AV_CODEC_ID_ATRAC1: return 512;
  2050. case AV_CODEC_ID_ATRAC3: return 1024;
  2051. case AV_CODEC_ID_MP2:
  2052. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2053. case AV_CODEC_ID_AC3: return 1536;
  2054. }
  2055. if (sr > 0) {
  2056. /* calc from sample rate */
  2057. if (id == AV_CODEC_ID_TTA)
  2058. return 256 * sr / 245;
  2059. if (ch > 0) {
  2060. /* calc from sample rate and channels */
  2061. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2062. return (480 << (sr / 22050)) / ch;
  2063. }
  2064. }
  2065. if (ba > 0) {
  2066. /* calc from block_align */
  2067. if (id == AV_CODEC_ID_SIPR) {
  2068. switch (ba) {
  2069. case 20: return 160;
  2070. case 19: return 144;
  2071. case 29: return 288;
  2072. case 37: return 480;
  2073. }
  2074. } else if (id == AV_CODEC_ID_ILBC) {
  2075. switch (ba) {
  2076. case 38: return 160;
  2077. case 50: return 240;
  2078. }
  2079. }
  2080. }
  2081. if (frame_bytes > 0) {
  2082. /* calc from frame_bytes only */
  2083. if (id == AV_CODEC_ID_TRUESPEECH)
  2084. return 240 * (frame_bytes / 32);
  2085. if (id == AV_CODEC_ID_NELLYMOSER)
  2086. return 256 * (frame_bytes / 64);
  2087. if (id == AV_CODEC_ID_RA_144)
  2088. return 160 * (frame_bytes / 20);
  2089. if (id == AV_CODEC_ID_G723_1)
  2090. return 240 * (frame_bytes / 24);
  2091. if (bps > 0) {
  2092. /* calc from frame_bytes and bits_per_coded_sample */
  2093. if (id == AV_CODEC_ID_ADPCM_G726)
  2094. return frame_bytes * 8 / bps;
  2095. }
  2096. if (ch > 0) {
  2097. /* calc from frame_bytes and channels */
  2098. switch (id) {
  2099. case AV_CODEC_ID_ADPCM_AFC:
  2100. return frame_bytes / (9 * ch) * 16;
  2101. case AV_CODEC_ID_ADPCM_4XM:
  2102. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2103. return (frame_bytes - 4 * ch) * 2 / ch;
  2104. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2105. return (frame_bytes - 4) * 2 / ch;
  2106. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2107. return (frame_bytes - 8) * 2 / ch;
  2108. case AV_CODEC_ID_ADPCM_XA:
  2109. return (frame_bytes / 128) * 224 / ch;
  2110. case AV_CODEC_ID_INTERPLAY_DPCM:
  2111. return (frame_bytes - 6 - ch) / ch;
  2112. case AV_CODEC_ID_ROQ_DPCM:
  2113. return (frame_bytes - 8) / ch;
  2114. case AV_CODEC_ID_XAN_DPCM:
  2115. return (frame_bytes - 2 * ch) / ch;
  2116. case AV_CODEC_ID_MACE3:
  2117. return 3 * frame_bytes / ch;
  2118. case AV_CODEC_ID_MACE6:
  2119. return 6 * frame_bytes / ch;
  2120. case AV_CODEC_ID_PCM_LXF:
  2121. return 2 * (frame_bytes / (5 * ch));
  2122. case AV_CODEC_ID_IAC:
  2123. case AV_CODEC_ID_IMC:
  2124. return 4 * frame_bytes / ch;
  2125. }
  2126. if (tag) {
  2127. /* calc from frame_bytes, channels, and codec_tag */
  2128. if (id == AV_CODEC_ID_SOL_DPCM) {
  2129. if (tag == 3)
  2130. return frame_bytes / ch;
  2131. else
  2132. return frame_bytes * 2 / ch;
  2133. }
  2134. }
  2135. if (ba > 0) {
  2136. /* calc from frame_bytes, channels, and block_align */
  2137. int blocks = frame_bytes / ba;
  2138. switch (avctx->codec_id) {
  2139. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2140. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  2141. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2142. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2143. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2144. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2145. case AV_CODEC_ID_ADPCM_MS:
  2146. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2147. }
  2148. }
  2149. if (bps > 0) {
  2150. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2151. switch (avctx->codec_id) {
  2152. case AV_CODEC_ID_PCM_DVD:
  2153. if(bps<4)
  2154. return 0;
  2155. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2156. case AV_CODEC_ID_PCM_BLURAY:
  2157. if(bps<4)
  2158. return 0;
  2159. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2160. case AV_CODEC_ID_S302M:
  2161. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2162. }
  2163. }
  2164. }
  2165. }
  2166. return 0;
  2167. }
  2168. #if !HAVE_THREADS
  2169. int ff_thread_init(AVCodecContext *s)
  2170. {
  2171. return -1;
  2172. }
  2173. #endif
  2174. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2175. {
  2176. unsigned int n = 0;
  2177. while (v >= 0xff) {
  2178. *s++ = 0xff;
  2179. v -= 0xff;
  2180. n++;
  2181. }
  2182. *s = v;
  2183. n++;
  2184. return n;
  2185. }
  2186. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2187. {
  2188. int i;
  2189. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2190. return i;
  2191. }
  2192. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2193. {
  2194. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  2195. "version to the newest one from Git. If the problem still "
  2196. "occurs, it means that your file has a feature which has not "
  2197. "been implemented.\n", feature);
  2198. if(want_sample)
  2199. av_log_ask_for_sample(avc, NULL);
  2200. }
  2201. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2202. {
  2203. va_list argument_list;
  2204. va_start(argument_list, msg);
  2205. if (msg)
  2206. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2207. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2208. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  2209. "and contact the ffmpeg-devel mailing list.\n");
  2210. va_end(argument_list);
  2211. }
  2212. static AVHWAccel *first_hwaccel = NULL;
  2213. void av_register_hwaccel(AVHWAccel *hwaccel)
  2214. {
  2215. AVHWAccel **p = &first_hwaccel;
  2216. while (*p)
  2217. p = &(*p)->next;
  2218. *p = hwaccel;
  2219. hwaccel->next = NULL;
  2220. }
  2221. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  2222. {
  2223. return hwaccel ? hwaccel->next : first_hwaccel;
  2224. }
  2225. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
  2226. {
  2227. AVHWAccel *hwaccel = NULL;
  2228. while ((hwaccel = av_hwaccel_next(hwaccel)))
  2229. if (hwaccel->id == codec_id
  2230. && hwaccel->pix_fmt == pix_fmt)
  2231. return hwaccel;
  2232. return NULL;
  2233. }
  2234. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2235. {
  2236. if (ff_lockmgr_cb) {
  2237. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  2238. return -1;
  2239. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  2240. return -1;
  2241. }
  2242. ff_lockmgr_cb = cb;
  2243. if (ff_lockmgr_cb) {
  2244. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  2245. return -1;
  2246. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2247. return -1;
  2248. }
  2249. return 0;
  2250. }
  2251. int ff_lock_avcodec(AVCodecContext *log_ctx)
  2252. {
  2253. if (ff_lockmgr_cb) {
  2254. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  2255. return -1;
  2256. }
  2257. entangled_thread_counter++;
  2258. if (entangled_thread_counter != 1) {
  2259. av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
  2260. ff_avcodec_locked = 1;
  2261. ff_unlock_avcodec();
  2262. return AVERROR(EINVAL);
  2263. }
  2264. av_assert0(!ff_avcodec_locked);
  2265. ff_avcodec_locked = 1;
  2266. return 0;
  2267. }
  2268. int ff_unlock_avcodec(void)
  2269. {
  2270. av_assert0(ff_avcodec_locked);
  2271. ff_avcodec_locked = 0;
  2272. entangled_thread_counter--;
  2273. if (ff_lockmgr_cb) {
  2274. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
  2275. return -1;
  2276. }
  2277. return 0;
  2278. }
  2279. int avpriv_lock_avformat(void)
  2280. {
  2281. if (ff_lockmgr_cb) {
  2282. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2283. return -1;
  2284. }
  2285. return 0;
  2286. }
  2287. int avpriv_unlock_avformat(void)
  2288. {
  2289. if (ff_lockmgr_cb) {
  2290. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2291. return -1;
  2292. }
  2293. return 0;
  2294. }
  2295. unsigned int avpriv_toupper4(unsigned int x)
  2296. {
  2297. return toupper(x & 0xFF)
  2298. + (toupper((x >> 8) & 0xFF) << 8)
  2299. + (toupper((x >> 16) & 0xFF) << 16)
  2300. + (toupper((x >> 24) & 0xFF) << 24);
  2301. }
  2302. #if !HAVE_THREADS
  2303. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  2304. {
  2305. f->owner = avctx;
  2306. return ff_get_buffer(avctx, f);
  2307. }
  2308. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  2309. {
  2310. f->owner->release_buffer(f->owner, f);
  2311. }
  2312. void ff_thread_finish_setup(AVCodecContext *avctx)
  2313. {
  2314. }
  2315. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  2316. {
  2317. }
  2318. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  2319. {
  2320. }
  2321. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2322. {
  2323. return 1;
  2324. }
  2325. #endif
  2326. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  2327. {
  2328. AVCodec *c= avcodec_find_decoder(codec_id);
  2329. if(!c)
  2330. c= avcodec_find_encoder(codec_id);
  2331. if(c)
  2332. return c->type;
  2333. if (codec_id <= AV_CODEC_ID_NONE)
  2334. return AVMEDIA_TYPE_UNKNOWN;
  2335. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  2336. return AVMEDIA_TYPE_VIDEO;
  2337. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  2338. return AVMEDIA_TYPE_AUDIO;
  2339. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  2340. return AVMEDIA_TYPE_SUBTITLE;
  2341. return AVMEDIA_TYPE_UNKNOWN;
  2342. }
  2343. int avcodec_is_open(AVCodecContext *s)
  2344. {
  2345. return !!s->internal;
  2346. }
  2347. int ff_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
  2348. {
  2349. int ret;
  2350. char *str;
  2351. ret = av_bprint_finalize(buf, &str);
  2352. if (ret < 0)
  2353. return ret;
  2354. avctx->extradata = str;
  2355. /* Note: the string is NUL terminated (so extradata can be read as a
  2356. * string), but the ending character is not accounted in the size (in
  2357. * binary formats you are likely not supposed to mux that character). When
  2358. * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
  2359. * zeros. */
  2360. avctx->extradata_size = buf->len;
  2361. return 0;
  2362. }