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.

2154 lines
65KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * utils.
  25. */
  26. #include "config.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/avstring.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 "avcodec.h"
  37. #include "dsputil.h"
  38. #include "libavutil/opt.h"
  39. #include "thread.h"
  40. #include "internal.h"
  41. #include "bytestream.h"
  42. #include <stdlib.h>
  43. #include <stdarg.h>
  44. #include <limits.h>
  45. #include <float.h>
  46. static int volatile entangled_thread_counter = 0;
  47. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  48. static void *codec_mutex;
  49. static void *avformat_mutex;
  50. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  51. {
  52. if (min_size < *size)
  53. return ptr;
  54. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  55. ptr = av_realloc(ptr, min_size);
  56. /* we could set this to the unmodified min_size but this is safer
  57. * if the user lost the ptr and uses NULL now
  58. */
  59. if (!ptr)
  60. min_size = 0;
  61. *size = min_size;
  62. return ptr;
  63. }
  64. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  65. {
  66. void **p = ptr;
  67. if (min_size < *size)
  68. return;
  69. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  70. av_free(*p);
  71. *p = av_malloc(min_size);
  72. if (!*p)
  73. min_size = 0;
  74. *size = min_size;
  75. }
  76. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  77. {
  78. void **p = ptr;
  79. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  80. av_freep(p);
  81. *size = 0;
  82. return;
  83. }
  84. av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  85. if (*size)
  86. memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  87. }
  88. /* encoder management */
  89. static AVCodec *first_avcodec = NULL;
  90. AVCodec *av_codec_next(const AVCodec *c)
  91. {
  92. if (c)
  93. return c->next;
  94. else
  95. return first_avcodec;
  96. }
  97. static void avcodec_init(void)
  98. {
  99. static int initialized = 0;
  100. if (initialized != 0)
  101. return;
  102. initialized = 1;
  103. ff_dsputil_static_init();
  104. }
  105. int av_codec_is_encoder(const AVCodec *codec)
  106. {
  107. return codec && (codec->encode_sub || codec->encode2);
  108. }
  109. int av_codec_is_decoder(const AVCodec *codec)
  110. {
  111. return codec && codec->decode;
  112. }
  113. void avcodec_register(AVCodec *codec)
  114. {
  115. AVCodec **p;
  116. avcodec_init();
  117. p = &first_avcodec;
  118. while (*p != NULL)
  119. p = &(*p)->next;
  120. *p = codec;
  121. codec->next = NULL;
  122. if (codec->init_static_data)
  123. codec->init_static_data(codec);
  124. }
  125. unsigned avcodec_get_edge_width(void)
  126. {
  127. return EDGE_WIDTH;
  128. }
  129. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  130. {
  131. s->coded_width = width;
  132. s->coded_height = height;
  133. s->width = width;
  134. s->height = height;
  135. }
  136. #define INTERNAL_BUFFER_SIZE (32 + 1)
  137. #if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
  138. # define STRIDE_ALIGN 16
  139. #else
  140. # define STRIDE_ALIGN 8
  141. #endif
  142. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  143. int linesize_align[AV_NUM_DATA_POINTERS])
  144. {
  145. int i;
  146. int w_align = 1;
  147. int h_align = 1;
  148. switch (s->pix_fmt) {
  149. case AV_PIX_FMT_YUV420P:
  150. case AV_PIX_FMT_YUYV422:
  151. case AV_PIX_FMT_UYVY422:
  152. case AV_PIX_FMT_YUV422P:
  153. case AV_PIX_FMT_YUV440P:
  154. case AV_PIX_FMT_YUV444P:
  155. case AV_PIX_FMT_GBRP:
  156. case AV_PIX_FMT_GRAY8:
  157. case AV_PIX_FMT_GRAY16BE:
  158. case AV_PIX_FMT_GRAY16LE:
  159. case AV_PIX_FMT_YUVJ420P:
  160. case AV_PIX_FMT_YUVJ422P:
  161. case AV_PIX_FMT_YUVJ440P:
  162. case AV_PIX_FMT_YUVJ444P:
  163. case AV_PIX_FMT_YUVA420P:
  164. case AV_PIX_FMT_YUVA422P:
  165. case AV_PIX_FMT_YUVA444P:
  166. case AV_PIX_FMT_YUV420P9LE:
  167. case AV_PIX_FMT_YUV420P9BE:
  168. case AV_PIX_FMT_YUV420P10LE:
  169. case AV_PIX_FMT_YUV420P10BE:
  170. case AV_PIX_FMT_YUV422P9LE:
  171. case AV_PIX_FMT_YUV422P9BE:
  172. case AV_PIX_FMT_YUV422P10LE:
  173. case AV_PIX_FMT_YUV422P10BE:
  174. case AV_PIX_FMT_YUV444P9LE:
  175. case AV_PIX_FMT_YUV444P9BE:
  176. case AV_PIX_FMT_YUV444P10LE:
  177. case AV_PIX_FMT_YUV444P10BE:
  178. case AV_PIX_FMT_GBRP9LE:
  179. case AV_PIX_FMT_GBRP9BE:
  180. case AV_PIX_FMT_GBRP10LE:
  181. case AV_PIX_FMT_GBRP10BE:
  182. w_align = 16; //FIXME assume 16 pixel per macroblock
  183. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  184. break;
  185. case AV_PIX_FMT_YUV411P:
  186. case AV_PIX_FMT_UYYVYY411:
  187. w_align = 32;
  188. h_align = 8;
  189. break;
  190. case AV_PIX_FMT_YUV410P:
  191. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  192. w_align = 64;
  193. h_align = 64;
  194. }
  195. case AV_PIX_FMT_RGB555:
  196. if (s->codec_id == AV_CODEC_ID_RPZA) {
  197. w_align = 4;
  198. h_align = 4;
  199. }
  200. case AV_PIX_FMT_PAL8:
  201. case AV_PIX_FMT_BGR8:
  202. case AV_PIX_FMT_RGB8:
  203. if (s->codec_id == AV_CODEC_ID_SMC) {
  204. w_align = 4;
  205. h_align = 4;
  206. }
  207. break;
  208. case AV_PIX_FMT_BGR24:
  209. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  210. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  211. w_align = 4;
  212. h_align = 4;
  213. }
  214. break;
  215. default:
  216. w_align = 1;
  217. h_align = 1;
  218. break;
  219. }
  220. *width = FFALIGN(*width, w_align);
  221. *height = FFALIGN(*height, h_align);
  222. if (s->codec_id == AV_CODEC_ID_H264)
  223. // some of the optimized chroma MC reads one line too much
  224. *height += 2;
  225. for (i = 0; i < 4; i++)
  226. linesize_align[i] = STRIDE_ALIGN;
  227. }
  228. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  229. {
  230. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  231. int chroma_shift = desc->log2_chroma_w;
  232. int linesize_align[AV_NUM_DATA_POINTERS];
  233. int align;
  234. avcodec_align_dimensions2(s, width, height, linesize_align);
  235. align = FFMAX(linesize_align[0], linesize_align[3]);
  236. linesize_align[1] <<= chroma_shift;
  237. linesize_align[2] <<= chroma_shift;
  238. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  239. *width = FFALIGN(*width, align);
  240. }
  241. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  242. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  243. int buf_size, int align)
  244. {
  245. int ch, planar, needed_size, ret = 0;
  246. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  247. frame->nb_samples, sample_fmt,
  248. align);
  249. if (buf_size < needed_size)
  250. return AVERROR(EINVAL);
  251. planar = av_sample_fmt_is_planar(sample_fmt);
  252. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  253. if (!(frame->extended_data = av_mallocz(nb_channels *
  254. sizeof(*frame->extended_data))))
  255. return AVERROR(ENOMEM);
  256. } else {
  257. frame->extended_data = frame->data;
  258. }
  259. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  260. buf, nb_channels, frame->nb_samples,
  261. sample_fmt, align)) < 0) {
  262. if (frame->extended_data != frame->data)
  263. av_free(frame->extended_data);
  264. return ret;
  265. }
  266. if (frame->extended_data != frame->data) {
  267. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  268. frame->data[ch] = frame->extended_data[ch];
  269. }
  270. return ret;
  271. }
  272. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  273. {
  274. AVCodecInternal *avci = avctx->internal;
  275. int buf_size, ret;
  276. av_freep(&avci->audio_data);
  277. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  278. frame->nb_samples, avctx->sample_fmt,
  279. 0);
  280. if (buf_size < 0)
  281. return AVERROR(EINVAL);
  282. frame->data[0] = av_mallocz(buf_size);
  283. if (!frame->data[0])
  284. return AVERROR(ENOMEM);
  285. ret = avcodec_fill_audio_frame(frame, avctx->channels, avctx->sample_fmt,
  286. frame->data[0], buf_size, 0);
  287. if (ret < 0) {
  288. av_freep(&frame->data[0]);
  289. return ret;
  290. }
  291. avci->audio_data = frame->data[0];
  292. if (avctx->debug & FF_DEBUG_BUFFERS)
  293. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  294. "internal audio buffer used\n", frame);
  295. return 0;
  296. }
  297. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  298. {
  299. int i;
  300. int w = s->width;
  301. int h = s->height;
  302. InternalBuffer *buf;
  303. AVCodecInternal *avci = s->internal;
  304. if (pic->data[0] != NULL) {
  305. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  306. return -1;
  307. }
  308. if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  309. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  310. return -1;
  311. }
  312. if (av_image_check_size(w, h, 0, s))
  313. return -1;
  314. if (!avci->buffer) {
  315. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
  316. sizeof(InternalBuffer));
  317. }
  318. buf = &avci->buffer[avci->buffer_count];
  319. if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
  320. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  321. av_freep(&buf->base[i]);
  322. buf->data[i] = NULL;
  323. }
  324. }
  325. if (!buf->base[0]) {
  326. int h_chroma_shift, v_chroma_shift;
  327. int size[4] = { 0 };
  328. int tmpsize;
  329. int unaligned;
  330. AVPicture picture;
  331. int stride_align[AV_NUM_DATA_POINTERS];
  332. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  333. const int pixel_size = desc->comp[0].step_minus1 + 1;
  334. av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift,
  335. &v_chroma_shift);
  336. avcodec_align_dimensions2(s, &w, &h, stride_align);
  337. if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
  338. w += EDGE_WIDTH * 2;
  339. h += EDGE_WIDTH * 2;
  340. }
  341. do {
  342. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  343. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  344. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  345. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  346. w += w & ~(w - 1);
  347. unaligned = 0;
  348. for (i = 0; i < 4; i++)
  349. unaligned |= picture.linesize[i] % stride_align[i];
  350. } while (unaligned);
  351. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  352. if (tmpsize < 0)
  353. return -1;
  354. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  355. size[i] = picture.data[i + 1] - picture.data[i];
  356. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  357. memset(buf->base, 0, sizeof(buf->base));
  358. memset(buf->data, 0, sizeof(buf->data));
  359. for (i = 0; i < 4 && size[i]; i++) {
  360. const int h_shift = i == 0 ? 0 : h_chroma_shift;
  361. const int v_shift = i == 0 ? 0 : v_chroma_shift;
  362. buf->linesize[i] = picture.linesize[i];
  363. buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
  364. if (buf->base[i] == NULL)
  365. return -1;
  366. // no edge if EDGE EMU or not planar YUV
  367. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
  368. buf->data[i] = buf->base[i];
  369. else
  370. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
  371. }
  372. for (; i < AV_NUM_DATA_POINTERS; i++) {
  373. buf->base[i] = buf->data[i] = NULL;
  374. buf->linesize[i] = 0;
  375. }
  376. if (size[1] && !size[2])
  377. avpriv_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
  378. buf->width = s->width;
  379. buf->height = s->height;
  380. buf->pix_fmt = s->pix_fmt;
  381. }
  382. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  383. pic->base[i] = buf->base[i];
  384. pic->data[i] = buf->data[i];
  385. pic->linesize[i] = buf->linesize[i];
  386. }
  387. pic->extended_data = pic->data;
  388. avci->buffer_count++;
  389. if (s->debug & FF_DEBUG_BUFFERS)
  390. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  391. "buffers used\n", pic, avci->buffer_count);
  392. return 0;
  393. }
  394. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  395. {
  396. frame->type = FF_BUFFER_TYPE_INTERNAL;
  397. switch (avctx->codec_type) {
  398. case AVMEDIA_TYPE_VIDEO:
  399. return video_get_buffer(avctx, frame);
  400. case AVMEDIA_TYPE_AUDIO:
  401. return audio_get_buffer(avctx, frame);
  402. default:
  403. return -1;
  404. }
  405. }
  406. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  407. {
  408. switch (avctx->codec_type) {
  409. case AVMEDIA_TYPE_VIDEO:
  410. frame->width = avctx->width;
  411. frame->height = avctx->height;
  412. frame->format = avctx->pix_fmt;
  413. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  414. break;
  415. case AVMEDIA_TYPE_AUDIO:
  416. frame->sample_rate = avctx->sample_rate;
  417. frame->format = avctx->sample_fmt;
  418. frame->channel_layout = avctx->channel_layout;
  419. break;
  420. default: return AVERROR(EINVAL);
  421. }
  422. frame->pkt_pts = avctx->pkt ? avctx->pkt->pts : AV_NOPTS_VALUE;
  423. frame->reordered_opaque = avctx->reordered_opaque;
  424. return avctx->get_buffer(avctx, frame);
  425. }
  426. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  427. {
  428. int i;
  429. InternalBuffer *buf, *last;
  430. AVCodecInternal *avci = s->internal;
  431. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  432. assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
  433. assert(avci->buffer_count);
  434. if (avci->buffer) {
  435. buf = NULL; /* avoids warning */
  436. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  437. buf = &avci->buffer[i];
  438. if (buf->data[0] == pic->data[0])
  439. break;
  440. }
  441. assert(i < avci->buffer_count);
  442. avci->buffer_count--;
  443. last = &avci->buffer[avci->buffer_count];
  444. if (buf != last)
  445. FFSWAP(InternalBuffer, *buf, *last);
  446. }
  447. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  448. pic->data[i] = NULL;
  449. // pic->base[i]=NULL;
  450. if (s->debug & FF_DEBUG_BUFFERS)
  451. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  452. "buffers used\n", pic, avci->buffer_count);
  453. }
  454. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  455. {
  456. AVFrame temp_pic;
  457. int i;
  458. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  459. /* If no picture return a new buffer */
  460. if (pic->data[0] == NULL) {
  461. /* We will copy from buffer, so must be readable */
  462. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  463. return ff_get_buffer(s, pic);
  464. }
  465. assert(s->pix_fmt == pic->format);
  466. /* If internal buffer type return the same buffer */
  467. if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
  468. if (s->pkt)
  469. pic->pkt_pts = s->pkt->pts;
  470. else
  471. pic->pkt_pts = AV_NOPTS_VALUE;
  472. pic->reordered_opaque = s->reordered_opaque;
  473. return 0;
  474. }
  475. /*
  476. * Not internal type and reget_buffer not overridden, emulate cr buffer
  477. */
  478. temp_pic = *pic;
  479. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  480. pic->data[i] = pic->base[i] = NULL;
  481. pic->opaque = NULL;
  482. /* Allocate new frame */
  483. if (ff_get_buffer(s, pic))
  484. return -1;
  485. /* Copy image data from old buffer to new buffer */
  486. av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
  487. s->height);
  488. s->release_buffer(s, &temp_pic); // Release old frame
  489. return 0;
  490. }
  491. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  492. {
  493. int i;
  494. for (i = 0; i < count; i++) {
  495. int r = func(c, (char *)arg + i * size);
  496. if (ret)
  497. ret[i] = r;
  498. }
  499. return 0;
  500. }
  501. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  502. {
  503. int i;
  504. for (i = 0; i < count; i++) {
  505. int r = func(c, arg, i, 0);
  506. if (ret)
  507. ret[i] = r;
  508. }
  509. return 0;
  510. }
  511. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  512. {
  513. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  514. return desc->flags & PIX_FMT_HWACCEL;
  515. }
  516. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  517. {
  518. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  519. ++fmt;
  520. return fmt[0];
  521. }
  522. void avcodec_get_frame_defaults(AVFrame *frame)
  523. {
  524. if (frame->extended_data != frame->data)
  525. av_freep(&frame->extended_data);
  526. memset(frame, 0, sizeof(AVFrame));
  527. frame->pts = AV_NOPTS_VALUE;
  528. frame->key_frame = 1;
  529. frame->sample_aspect_ratio = (AVRational) {0, 1 };
  530. frame->format = -1; /* unknown */
  531. frame->extended_data = frame->data;
  532. }
  533. AVFrame *avcodec_alloc_frame(void)
  534. {
  535. AVFrame *frame = av_mallocz(sizeof(AVFrame));
  536. if (frame == NULL)
  537. return NULL;
  538. avcodec_get_frame_defaults(frame);
  539. return frame;
  540. }
  541. void avcodec_free_frame(AVFrame **frame)
  542. {
  543. AVFrame *f;
  544. if (!frame || !*frame)
  545. return;
  546. f = *frame;
  547. if (f->extended_data != f->data)
  548. av_freep(&f->extended_data);
  549. av_freep(frame);
  550. }
  551. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  552. {
  553. int ret = 0;
  554. AVDictionary *tmp = NULL;
  555. if (avcodec_is_open(avctx))
  556. return 0;
  557. if ((!codec && !avctx->codec)) {
  558. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  559. return AVERROR(EINVAL);
  560. }
  561. if ((codec && avctx->codec && codec != avctx->codec)) {
  562. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  563. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  564. return AVERROR(EINVAL);
  565. }
  566. if (!codec)
  567. codec = avctx->codec;
  568. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  569. return AVERROR(EINVAL);
  570. if (options)
  571. av_dict_copy(&tmp, *options, 0);
  572. /* If there is a user-supplied mutex locking routine, call it. */
  573. if (ff_lockmgr_cb) {
  574. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  575. return -1;
  576. }
  577. entangled_thread_counter++;
  578. if (entangled_thread_counter != 1) {
  579. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  580. ret = -1;
  581. goto end;
  582. }
  583. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  584. if (!avctx->internal) {
  585. ret = AVERROR(ENOMEM);
  586. goto end;
  587. }
  588. if (codec->priv_data_size > 0) {
  589. if (!avctx->priv_data) {
  590. avctx->priv_data = av_mallocz(codec->priv_data_size);
  591. if (!avctx->priv_data) {
  592. ret = AVERROR(ENOMEM);
  593. goto end;
  594. }
  595. if (codec->priv_class) {
  596. *(const AVClass **)avctx->priv_data = codec->priv_class;
  597. av_opt_set_defaults(avctx->priv_data);
  598. }
  599. }
  600. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  601. goto free_and_end;
  602. } else {
  603. avctx->priv_data = NULL;
  604. }
  605. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  606. goto free_and_end;
  607. if (avctx->coded_width && avctx->coded_height)
  608. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  609. else if (avctx->width && avctx->height)
  610. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  611. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  612. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  613. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  614. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  615. avcodec_set_dimensions(avctx, 0, 0);
  616. }
  617. /* if the decoder init function was already called previously,
  618. * free the already allocated subtitle_header before overwriting it */
  619. if (av_codec_is_decoder(codec))
  620. av_freep(&avctx->subtitle_header);
  621. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  622. ret = AVERROR(EINVAL);
  623. goto free_and_end;
  624. }
  625. avctx->codec = codec;
  626. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  627. avctx->codec_id == AV_CODEC_ID_NONE) {
  628. avctx->codec_type = codec->type;
  629. avctx->codec_id = codec->id;
  630. }
  631. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  632. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  633. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  634. ret = AVERROR(EINVAL);
  635. goto free_and_end;
  636. }
  637. avctx->frame_number = 0;
  638. if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
  639. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  640. ret = AVERROR_EXPERIMENTAL;
  641. goto free_and_end;
  642. }
  643. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  644. (!avctx->time_base.num || !avctx->time_base.den)) {
  645. avctx->time_base.num = 1;
  646. avctx->time_base.den = avctx->sample_rate;
  647. }
  648. if (HAVE_THREADS && !avctx->thread_opaque) {
  649. ret = ff_thread_init(avctx);
  650. if (ret < 0) {
  651. goto free_and_end;
  652. }
  653. }
  654. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  655. avctx->thread_count = 1;
  656. if (av_codec_is_encoder(avctx->codec)) {
  657. int i;
  658. if (avctx->codec->sample_fmts) {
  659. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  660. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  661. break;
  662. if (avctx->channels == 1 &&
  663. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  664. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  665. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  666. break;
  667. }
  668. }
  669. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  670. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  671. ret = AVERROR(EINVAL);
  672. goto free_and_end;
  673. }
  674. }
  675. if (avctx->codec->pix_fmts) {
  676. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  677. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  678. break;
  679. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
  680. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  681. ret = AVERROR(EINVAL);
  682. goto free_and_end;
  683. }
  684. }
  685. if (avctx->codec->supported_samplerates) {
  686. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  687. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  688. break;
  689. if (avctx->codec->supported_samplerates[i] == 0) {
  690. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  691. ret = AVERROR(EINVAL);
  692. goto free_and_end;
  693. }
  694. }
  695. if (avctx->codec->channel_layouts) {
  696. if (!avctx->channel_layout) {
  697. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  698. } else {
  699. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  700. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  701. break;
  702. if (avctx->codec->channel_layouts[i] == 0) {
  703. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  704. ret = AVERROR(EINVAL);
  705. goto free_and_end;
  706. }
  707. }
  708. }
  709. if (avctx->channel_layout && avctx->channels) {
  710. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  711. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  712. ret = AVERROR(EINVAL);
  713. goto free_and_end;
  714. }
  715. } else if (avctx->channel_layout) {
  716. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  717. }
  718. if (!avctx->rc_initial_buffer_occupancy)
  719. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  720. }
  721. if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
  722. ret = avctx->codec->init(avctx);
  723. if (ret < 0) {
  724. goto free_and_end;
  725. }
  726. }
  727. if (av_codec_is_decoder(avctx->codec)) {
  728. /* validate channel layout from the decoder */
  729. if (avctx->channel_layout) {
  730. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  731. if (!avctx->channels)
  732. avctx->channels = channels;
  733. else if (channels != avctx->channels) {
  734. av_log(avctx, AV_LOG_WARNING,
  735. "channel layout does not match number of channels\n");
  736. avctx->channel_layout = 0;
  737. }
  738. }
  739. if (avctx->channels && avctx->channels < 0 ||
  740. avctx->channels > FF_SANE_NB_CHANNELS) {
  741. ret = AVERROR(EINVAL);
  742. goto free_and_end;
  743. }
  744. }
  745. end:
  746. entangled_thread_counter--;
  747. /* Release any user-supplied mutex. */
  748. if (ff_lockmgr_cb) {
  749. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  750. }
  751. if (options) {
  752. av_dict_free(options);
  753. *options = tmp;
  754. }
  755. return ret;
  756. free_and_end:
  757. av_dict_free(&tmp);
  758. av_freep(&avctx->priv_data);
  759. av_freep(&avctx->internal);
  760. avctx->codec = NULL;
  761. goto end;
  762. }
  763. int ff_alloc_packet(AVPacket *avpkt, int size)
  764. {
  765. if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  766. return AVERROR(EINVAL);
  767. if (avpkt->data) {
  768. void *destruct = avpkt->destruct;
  769. if (avpkt->size < size)
  770. return AVERROR(EINVAL);
  771. av_init_packet(avpkt);
  772. avpkt->destruct = destruct;
  773. avpkt->size = size;
  774. return 0;
  775. } else {
  776. return av_new_packet(avpkt, size);
  777. }
  778. }
  779. /**
  780. * Pad last frame with silence.
  781. */
  782. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  783. {
  784. AVFrame *frame = NULL;
  785. uint8_t *buf = NULL;
  786. int ret;
  787. if (!(frame = avcodec_alloc_frame()))
  788. return AVERROR(ENOMEM);
  789. *frame = *src;
  790. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  791. s->frame_size, s->sample_fmt, 0)) < 0)
  792. goto fail;
  793. if (!(buf = av_malloc(ret))) {
  794. ret = AVERROR(ENOMEM);
  795. goto fail;
  796. }
  797. frame->nb_samples = s->frame_size;
  798. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  799. buf, ret, 0)) < 0)
  800. goto fail;
  801. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  802. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  803. goto fail;
  804. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  805. frame->nb_samples - src->nb_samples,
  806. s->channels, s->sample_fmt)) < 0)
  807. goto fail;
  808. *dst = frame;
  809. return 0;
  810. fail:
  811. if (frame->extended_data != frame->data)
  812. av_freep(&frame->extended_data);
  813. av_freep(&buf);
  814. av_freep(&frame);
  815. return ret;
  816. }
  817. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  818. AVPacket *avpkt,
  819. const AVFrame *frame,
  820. int *got_packet_ptr)
  821. {
  822. AVFrame tmp;
  823. AVFrame *padded_frame = NULL;
  824. int ret;
  825. int user_packet = !!avpkt->data;
  826. *got_packet_ptr = 0;
  827. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  828. av_free_packet(avpkt);
  829. av_init_packet(avpkt);
  830. return 0;
  831. }
  832. /* ensure that extended_data is properly set */
  833. if (frame && !frame->extended_data) {
  834. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  835. avctx->channels > AV_NUM_DATA_POINTERS) {
  836. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  837. "with more than %d channels, but extended_data is not set.\n",
  838. AV_NUM_DATA_POINTERS);
  839. return AVERROR(EINVAL);
  840. }
  841. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  842. tmp = *frame;
  843. tmp.extended_data = tmp.data;
  844. frame = &tmp;
  845. }
  846. /* check for valid frame size */
  847. if (frame) {
  848. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  849. if (frame->nb_samples > avctx->frame_size)
  850. return AVERROR(EINVAL);
  851. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  852. if (frame->nb_samples < avctx->frame_size &&
  853. !avctx->internal->last_audio_frame) {
  854. ret = pad_last_frame(avctx, &padded_frame, frame);
  855. if (ret < 0)
  856. return ret;
  857. frame = padded_frame;
  858. avctx->internal->last_audio_frame = 1;
  859. }
  860. if (frame->nb_samples != avctx->frame_size) {
  861. ret = AVERROR(EINVAL);
  862. goto end;
  863. }
  864. }
  865. }
  866. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  867. if (!ret) {
  868. if (*got_packet_ptr) {
  869. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  870. if (avpkt->pts == AV_NOPTS_VALUE)
  871. avpkt->pts = frame->pts;
  872. if (!avpkt->duration)
  873. avpkt->duration = ff_samples_to_time_base(avctx,
  874. frame->nb_samples);
  875. }
  876. avpkt->dts = avpkt->pts;
  877. } else {
  878. avpkt->size = 0;
  879. }
  880. if (!user_packet && avpkt->size) {
  881. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
  882. if (new_data)
  883. avpkt->data = new_data;
  884. }
  885. avctx->frame_number++;
  886. }
  887. if (ret < 0 || !*got_packet_ptr) {
  888. av_free_packet(avpkt);
  889. av_init_packet(avpkt);
  890. goto end;
  891. }
  892. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  893. * this needs to be moved to the encoders, but for now we can do it
  894. * here to simplify things */
  895. avpkt->flags |= AV_PKT_FLAG_KEY;
  896. end:
  897. if (padded_frame) {
  898. av_freep(&padded_frame->data[0]);
  899. if (padded_frame->extended_data != padded_frame->data)
  900. av_freep(&padded_frame->extended_data);
  901. av_freep(&padded_frame);
  902. }
  903. return ret;
  904. }
  905. #if FF_API_OLD_ENCODE_AUDIO
  906. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  907. uint8_t *buf, int buf_size,
  908. const short *samples)
  909. {
  910. AVPacket pkt;
  911. AVFrame frame0 = { { 0 } };
  912. AVFrame *frame;
  913. int ret, samples_size, got_packet;
  914. av_init_packet(&pkt);
  915. pkt.data = buf;
  916. pkt.size = buf_size;
  917. if (samples) {
  918. frame = &frame0;
  919. avcodec_get_frame_defaults(frame);
  920. if (avctx->frame_size) {
  921. frame->nb_samples = avctx->frame_size;
  922. } else {
  923. /* if frame_size is not set, the number of samples must be
  924. * calculated from the buffer size */
  925. int64_t nb_samples;
  926. if (!av_get_bits_per_sample(avctx->codec_id)) {
  927. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  928. "support this codec\n");
  929. return AVERROR(EINVAL);
  930. }
  931. nb_samples = (int64_t)buf_size * 8 /
  932. (av_get_bits_per_sample(avctx->codec_id) *
  933. avctx->channels);
  934. if (nb_samples >= INT_MAX)
  935. return AVERROR(EINVAL);
  936. frame->nb_samples = nb_samples;
  937. }
  938. /* it is assumed that the samples buffer is large enough based on the
  939. * relevant parameters */
  940. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  941. frame->nb_samples,
  942. avctx->sample_fmt, 1);
  943. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  944. avctx->sample_fmt,
  945. (const uint8_t *)samples,
  946. samples_size, 1)))
  947. return ret;
  948. /* fabricate frame pts from sample count.
  949. * this is needed because the avcodec_encode_audio() API does not have
  950. * a way for the user to provide pts */
  951. frame->pts = ff_samples_to_time_base(avctx,
  952. avctx->internal->sample_count);
  953. avctx->internal->sample_count += frame->nb_samples;
  954. } else {
  955. frame = NULL;
  956. }
  957. got_packet = 0;
  958. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  959. if (!ret && got_packet && avctx->coded_frame) {
  960. avctx->coded_frame->pts = pkt.pts;
  961. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  962. }
  963. /* free any side data since we cannot return it */
  964. if (pkt.side_data_elems > 0) {
  965. int i;
  966. for (i = 0; i < pkt.side_data_elems; i++)
  967. av_free(pkt.side_data[i].data);
  968. av_freep(&pkt.side_data);
  969. pkt.side_data_elems = 0;
  970. }
  971. if (frame && frame->extended_data != frame->data)
  972. av_free(frame->extended_data);
  973. return ret ? ret : pkt.size;
  974. }
  975. #endif
  976. #if FF_API_OLD_ENCODE_VIDEO
  977. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  978. const AVFrame *pict)
  979. {
  980. AVPacket pkt;
  981. int ret, got_packet = 0;
  982. if (buf_size < FF_MIN_BUFFER_SIZE) {
  983. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  984. return -1;
  985. }
  986. av_init_packet(&pkt);
  987. pkt.data = buf;
  988. pkt.size = buf_size;
  989. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  990. if (!ret && got_packet && avctx->coded_frame) {
  991. avctx->coded_frame->pts = pkt.pts;
  992. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  993. }
  994. /* free any side data since we cannot return it */
  995. if (pkt.side_data_elems > 0) {
  996. int i;
  997. for (i = 0; i < pkt.side_data_elems; i++)
  998. av_free(pkt.side_data[i].data);
  999. av_freep(&pkt.side_data);
  1000. pkt.side_data_elems = 0;
  1001. }
  1002. return ret ? ret : pkt.size;
  1003. }
  1004. #endif
  1005. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1006. AVPacket *avpkt,
  1007. const AVFrame *frame,
  1008. int *got_packet_ptr)
  1009. {
  1010. int ret;
  1011. int user_packet = !!avpkt->data;
  1012. *got_packet_ptr = 0;
  1013. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1014. av_free_packet(avpkt);
  1015. av_init_packet(avpkt);
  1016. avpkt->size = 0;
  1017. return 0;
  1018. }
  1019. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1020. return AVERROR(EINVAL);
  1021. av_assert0(avctx->codec->encode2);
  1022. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1023. if (!ret) {
  1024. if (!*got_packet_ptr)
  1025. avpkt->size = 0;
  1026. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1027. avpkt->pts = avpkt->dts = frame->pts;
  1028. if (!user_packet && avpkt->size) {
  1029. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
  1030. if (new_data)
  1031. avpkt->data = new_data;
  1032. }
  1033. avctx->frame_number++;
  1034. }
  1035. if (ret < 0 || !*got_packet_ptr)
  1036. av_free_packet(avpkt);
  1037. emms_c();
  1038. return ret;
  1039. }
  1040. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1041. const AVSubtitle *sub)
  1042. {
  1043. int ret;
  1044. if (sub->start_display_time) {
  1045. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1046. return -1;
  1047. }
  1048. if (sub->num_rects == 0 || !sub->rects)
  1049. return -1;
  1050. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1051. avctx->frame_number++;
  1052. return ret;
  1053. }
  1054. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1055. {
  1056. int size = 0;
  1057. const uint8_t *data;
  1058. uint32_t flags;
  1059. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1060. return;
  1061. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1062. if (!data || size < 4)
  1063. return;
  1064. flags = bytestream_get_le32(&data);
  1065. size -= 4;
  1066. if (size < 4) /* Required for any of the changes */
  1067. return;
  1068. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1069. avctx->channels = bytestream_get_le32(&data);
  1070. size -= 4;
  1071. }
  1072. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1073. if (size < 8)
  1074. return;
  1075. avctx->channel_layout = bytestream_get_le64(&data);
  1076. size -= 8;
  1077. }
  1078. if (size < 4)
  1079. return;
  1080. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1081. avctx->sample_rate = bytestream_get_le32(&data);
  1082. size -= 4;
  1083. }
  1084. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1085. if (size < 8)
  1086. return;
  1087. avctx->width = bytestream_get_le32(&data);
  1088. avctx->height = bytestream_get_le32(&data);
  1089. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1090. size -= 8;
  1091. }
  1092. }
  1093. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1094. int *got_picture_ptr,
  1095. AVPacket *avpkt)
  1096. {
  1097. int ret;
  1098. *got_picture_ptr = 0;
  1099. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1100. return -1;
  1101. avctx->pkt = avpkt;
  1102. apply_param_change(avctx, avpkt);
  1103. avcodec_get_frame_defaults(picture);
  1104. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1105. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1106. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1107. avpkt);
  1108. else {
  1109. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1110. avpkt);
  1111. picture->pkt_dts = avpkt->dts;
  1112. /* get_buffer is supposed to set frame parameters */
  1113. if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
  1114. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1115. picture->width = avctx->width;
  1116. picture->height = avctx->height;
  1117. picture->format = avctx->pix_fmt;
  1118. }
  1119. }
  1120. emms_c(); //needed to avoid an emms_c() call before every return;
  1121. if (*got_picture_ptr)
  1122. avctx->frame_number++;
  1123. } else
  1124. ret = 0;
  1125. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1126. * make sure it's set correctly */
  1127. picture->extended_data = picture->data;
  1128. return ret;
  1129. }
  1130. #if FF_API_OLD_DECODE_AUDIO
  1131. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1132. int *frame_size_ptr,
  1133. AVPacket *avpkt)
  1134. {
  1135. AVFrame frame = { { 0 } };
  1136. int ret, got_frame = 0;
  1137. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1138. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1139. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1140. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1141. "avcodec_decode_audio4()\n");
  1142. avctx->get_buffer = avcodec_default_get_buffer;
  1143. }
  1144. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1145. if (ret >= 0 && got_frame) {
  1146. int ch, plane_size;
  1147. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1148. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1149. frame.nb_samples,
  1150. avctx->sample_fmt, 1);
  1151. if (*frame_size_ptr < data_size) {
  1152. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1153. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1154. return AVERROR(EINVAL);
  1155. }
  1156. memcpy(samples, frame.extended_data[0], plane_size);
  1157. if (planar && avctx->channels > 1) {
  1158. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1159. for (ch = 1; ch < avctx->channels; ch++) {
  1160. memcpy(out, frame.extended_data[ch], plane_size);
  1161. out += plane_size;
  1162. }
  1163. }
  1164. *frame_size_ptr = data_size;
  1165. } else {
  1166. *frame_size_ptr = 0;
  1167. }
  1168. return ret;
  1169. }
  1170. #endif
  1171. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1172. AVFrame *frame,
  1173. int *got_frame_ptr,
  1174. AVPacket *avpkt)
  1175. {
  1176. int planar, channels;
  1177. int ret = 0;
  1178. *got_frame_ptr = 0;
  1179. avctx->pkt = avpkt;
  1180. if (!avpkt->data && avpkt->size) {
  1181. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1182. return AVERROR(EINVAL);
  1183. }
  1184. apply_param_change(avctx, avpkt);
  1185. avcodec_get_frame_defaults(frame);
  1186. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1187. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1188. if (ret >= 0 && *got_frame_ptr) {
  1189. avctx->frame_number++;
  1190. frame->pkt_dts = avpkt->dts;
  1191. if (frame->format == AV_SAMPLE_FMT_NONE)
  1192. frame->format = avctx->sample_fmt;
  1193. }
  1194. }
  1195. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1196. * make sure it's set correctly; assume decoders that actually use
  1197. * extended_data are doing it correctly */
  1198. planar = av_sample_fmt_is_planar(frame->format);
  1199. channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  1200. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1201. frame->extended_data = frame->data;
  1202. return ret;
  1203. }
  1204. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1205. int *got_sub_ptr,
  1206. AVPacket *avpkt)
  1207. {
  1208. int ret;
  1209. avctx->pkt = avpkt;
  1210. *got_sub_ptr = 0;
  1211. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1212. if (*got_sub_ptr)
  1213. avctx->frame_number++;
  1214. return ret;
  1215. }
  1216. void avsubtitle_free(AVSubtitle *sub)
  1217. {
  1218. int i;
  1219. for (i = 0; i < sub->num_rects; i++) {
  1220. av_freep(&sub->rects[i]->pict.data[0]);
  1221. av_freep(&sub->rects[i]->pict.data[1]);
  1222. av_freep(&sub->rects[i]->pict.data[2]);
  1223. av_freep(&sub->rects[i]->pict.data[3]);
  1224. av_freep(&sub->rects[i]->text);
  1225. av_freep(&sub->rects[i]->ass);
  1226. av_freep(&sub->rects[i]);
  1227. }
  1228. av_freep(&sub->rects);
  1229. memset(sub, 0, sizeof(AVSubtitle));
  1230. }
  1231. av_cold int avcodec_close(AVCodecContext *avctx)
  1232. {
  1233. /* If there is a user-supplied mutex locking routine, call it. */
  1234. if (ff_lockmgr_cb) {
  1235. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1236. return -1;
  1237. }
  1238. entangled_thread_counter++;
  1239. if (entangled_thread_counter != 1) {
  1240. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1241. entangled_thread_counter--;
  1242. return -1;
  1243. }
  1244. if (avcodec_is_open(avctx)) {
  1245. if (HAVE_THREADS && avctx->thread_opaque)
  1246. ff_thread_free(avctx);
  1247. if (avctx->codec && avctx->codec->close)
  1248. avctx->codec->close(avctx);
  1249. avcodec_default_free_buffers(avctx);
  1250. avctx->coded_frame = NULL;
  1251. av_freep(&avctx->internal);
  1252. }
  1253. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1254. av_opt_free(avctx->priv_data);
  1255. av_opt_free(avctx);
  1256. av_freep(&avctx->priv_data);
  1257. if (av_codec_is_encoder(avctx->codec))
  1258. av_freep(&avctx->extradata);
  1259. avctx->codec = NULL;
  1260. avctx->active_thread_type = 0;
  1261. entangled_thread_counter--;
  1262. /* Release any user-supplied mutex. */
  1263. if (ff_lockmgr_cb) {
  1264. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1265. }
  1266. return 0;
  1267. }
  1268. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  1269. {
  1270. AVCodec *p, *experimental = NULL;
  1271. p = first_avcodec;
  1272. while (p) {
  1273. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  1274. p->id == id) {
  1275. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1276. experimental = p;
  1277. } else
  1278. return p;
  1279. }
  1280. p = p->next;
  1281. }
  1282. return experimental;
  1283. }
  1284. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1285. {
  1286. return find_encdec(id, 1);
  1287. }
  1288. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1289. {
  1290. AVCodec *p;
  1291. if (!name)
  1292. return NULL;
  1293. p = first_avcodec;
  1294. while (p) {
  1295. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1296. return p;
  1297. p = p->next;
  1298. }
  1299. return NULL;
  1300. }
  1301. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1302. {
  1303. return find_encdec(id, 0);
  1304. }
  1305. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1306. {
  1307. AVCodec *p;
  1308. if (!name)
  1309. return NULL;
  1310. p = first_avcodec;
  1311. while (p) {
  1312. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1313. return p;
  1314. p = p->next;
  1315. }
  1316. return NULL;
  1317. }
  1318. static int get_bit_rate(AVCodecContext *ctx)
  1319. {
  1320. int bit_rate;
  1321. int bits_per_sample;
  1322. switch (ctx->codec_type) {
  1323. case AVMEDIA_TYPE_VIDEO:
  1324. case AVMEDIA_TYPE_DATA:
  1325. case AVMEDIA_TYPE_SUBTITLE:
  1326. case AVMEDIA_TYPE_ATTACHMENT:
  1327. bit_rate = ctx->bit_rate;
  1328. break;
  1329. case AVMEDIA_TYPE_AUDIO:
  1330. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1331. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1332. break;
  1333. default:
  1334. bit_rate = 0;
  1335. break;
  1336. }
  1337. return bit_rate;
  1338. }
  1339. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1340. {
  1341. int i, len, ret = 0;
  1342. for (i = 0; i < 4; i++) {
  1343. len = snprintf(buf, buf_size,
  1344. isprint(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  1345. buf += len;
  1346. buf_size = buf_size > len ? buf_size - len : 0;
  1347. ret += len;
  1348. codec_tag >>= 8;
  1349. }
  1350. return ret;
  1351. }
  1352. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1353. {
  1354. const char *codec_name;
  1355. const char *profile = NULL;
  1356. const AVCodec *p;
  1357. char buf1[32];
  1358. int bitrate;
  1359. AVRational display_aspect_ratio;
  1360. if (enc->codec)
  1361. p = enc->codec;
  1362. else if (encode)
  1363. p = avcodec_find_encoder(enc->codec_id);
  1364. else
  1365. p = avcodec_find_decoder(enc->codec_id);
  1366. if (p) {
  1367. codec_name = p->name;
  1368. profile = av_get_profile_name(p, enc->profile);
  1369. } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
  1370. /* fake mpeg2 transport stream codec (currently not
  1371. * registered) */
  1372. codec_name = "mpeg2ts";
  1373. } else if (enc->codec_name[0] != '\0') {
  1374. codec_name = enc->codec_name;
  1375. } else {
  1376. /* output avi tags */
  1377. char tag_buf[32];
  1378. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1379. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  1380. codec_name = buf1;
  1381. }
  1382. switch (enc->codec_type) {
  1383. case AVMEDIA_TYPE_VIDEO:
  1384. snprintf(buf, buf_size,
  1385. "Video: %s%s",
  1386. codec_name, enc->mb_decision ? " (hq)" : "");
  1387. if (profile)
  1388. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1389. " (%s)", profile);
  1390. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  1391. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1392. ", %s",
  1393. av_get_pix_fmt_name(enc->pix_fmt));
  1394. }
  1395. if (enc->width) {
  1396. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1397. ", %dx%d",
  1398. enc->width, enc->height);
  1399. if (enc->sample_aspect_ratio.num) {
  1400. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1401. enc->width * enc->sample_aspect_ratio.num,
  1402. enc->height * enc->sample_aspect_ratio.den,
  1403. 1024 * 1024);
  1404. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1405. " [PAR %d:%d DAR %d:%d]",
  1406. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1407. display_aspect_ratio.num, display_aspect_ratio.den);
  1408. }
  1409. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1410. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1411. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1412. ", %d/%d",
  1413. enc->time_base.num / g, enc->time_base.den / g);
  1414. }
  1415. }
  1416. if (encode) {
  1417. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1418. ", q=%d-%d", enc->qmin, enc->qmax);
  1419. }
  1420. break;
  1421. case AVMEDIA_TYPE_AUDIO:
  1422. snprintf(buf, buf_size,
  1423. "Audio: %s",
  1424. codec_name);
  1425. if (profile)
  1426. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1427. " (%s)", profile);
  1428. if (enc->sample_rate) {
  1429. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1430. ", %d Hz", enc->sample_rate);
  1431. }
  1432. av_strlcat(buf, ", ", buf_size);
  1433. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1434. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1435. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1436. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1437. }
  1438. break;
  1439. case AVMEDIA_TYPE_DATA:
  1440. snprintf(buf, buf_size, "Data: %s", codec_name);
  1441. break;
  1442. case AVMEDIA_TYPE_SUBTITLE:
  1443. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1444. break;
  1445. case AVMEDIA_TYPE_ATTACHMENT:
  1446. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1447. break;
  1448. default:
  1449. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1450. return;
  1451. }
  1452. if (encode) {
  1453. if (enc->flags & CODEC_FLAG_PASS1)
  1454. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1455. ", pass 1");
  1456. if (enc->flags & CODEC_FLAG_PASS2)
  1457. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1458. ", pass 2");
  1459. }
  1460. bitrate = get_bit_rate(enc);
  1461. if (bitrate != 0) {
  1462. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1463. ", %d kb/s", bitrate / 1000);
  1464. }
  1465. }
  1466. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1467. {
  1468. const AVProfile *p;
  1469. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1470. return NULL;
  1471. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1472. if (p->profile == profile)
  1473. return p->name;
  1474. return NULL;
  1475. }
  1476. unsigned avcodec_version(void)
  1477. {
  1478. return LIBAVCODEC_VERSION_INT;
  1479. }
  1480. const char *avcodec_configuration(void)
  1481. {
  1482. return LIBAV_CONFIGURATION;
  1483. }
  1484. const char *avcodec_license(void)
  1485. {
  1486. #define LICENSE_PREFIX "libavcodec license: "
  1487. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1488. }
  1489. void avcodec_flush_buffers(AVCodecContext *avctx)
  1490. {
  1491. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1492. ff_thread_flush(avctx);
  1493. else if (avctx->codec->flush)
  1494. avctx->codec->flush(avctx);
  1495. }
  1496. static void video_free_buffers(AVCodecContext *s)
  1497. {
  1498. AVCodecInternal *avci = s->internal;
  1499. int i, j;
  1500. if (!avci->buffer)
  1501. return;
  1502. if (avci->buffer_count)
  1503. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1504. avci->buffer_count);
  1505. for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
  1506. InternalBuffer *buf = &avci->buffer[i];
  1507. for (j = 0; j < 4; j++) {
  1508. av_freep(&buf->base[j]);
  1509. buf->data[j] = NULL;
  1510. }
  1511. }
  1512. av_freep(&avci->buffer);
  1513. avci->buffer_count = 0;
  1514. }
  1515. static void audio_free_buffers(AVCodecContext *avctx)
  1516. {
  1517. AVCodecInternal *avci = avctx->internal;
  1518. av_freep(&avci->audio_data);
  1519. }
  1520. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1521. {
  1522. switch (avctx->codec_type) {
  1523. case AVMEDIA_TYPE_VIDEO:
  1524. video_free_buffers(avctx);
  1525. break;
  1526. case AVMEDIA_TYPE_AUDIO:
  1527. audio_free_buffers(avctx);
  1528. break;
  1529. default:
  1530. break;
  1531. }
  1532. }
  1533. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1534. {
  1535. switch (codec_id) {
  1536. case AV_CODEC_ID_ADPCM_CT:
  1537. case AV_CODEC_ID_ADPCM_IMA_APC:
  1538. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1539. case AV_CODEC_ID_ADPCM_IMA_WS:
  1540. case AV_CODEC_ID_ADPCM_G722:
  1541. case AV_CODEC_ID_ADPCM_YAMAHA:
  1542. return 4;
  1543. case AV_CODEC_ID_PCM_ALAW:
  1544. case AV_CODEC_ID_PCM_MULAW:
  1545. case AV_CODEC_ID_PCM_S8:
  1546. case AV_CODEC_ID_PCM_U8:
  1547. case AV_CODEC_ID_PCM_ZORK:
  1548. return 8;
  1549. case AV_CODEC_ID_PCM_S16BE:
  1550. case AV_CODEC_ID_PCM_S16LE:
  1551. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1552. case AV_CODEC_ID_PCM_U16BE:
  1553. case AV_CODEC_ID_PCM_U16LE:
  1554. return 16;
  1555. case AV_CODEC_ID_PCM_S24DAUD:
  1556. case AV_CODEC_ID_PCM_S24BE:
  1557. case AV_CODEC_ID_PCM_S24LE:
  1558. case AV_CODEC_ID_PCM_U24BE:
  1559. case AV_CODEC_ID_PCM_U24LE:
  1560. return 24;
  1561. case AV_CODEC_ID_PCM_S32BE:
  1562. case AV_CODEC_ID_PCM_S32LE:
  1563. case AV_CODEC_ID_PCM_U32BE:
  1564. case AV_CODEC_ID_PCM_U32LE:
  1565. case AV_CODEC_ID_PCM_F32BE:
  1566. case AV_CODEC_ID_PCM_F32LE:
  1567. return 32;
  1568. case AV_CODEC_ID_PCM_F64BE:
  1569. case AV_CODEC_ID_PCM_F64LE:
  1570. return 64;
  1571. default:
  1572. return 0;
  1573. }
  1574. }
  1575. int av_get_bits_per_sample(enum AVCodecID codec_id)
  1576. {
  1577. switch (codec_id) {
  1578. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1579. return 2;
  1580. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1581. return 3;
  1582. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1583. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1584. case AV_CODEC_ID_ADPCM_IMA_QT:
  1585. case AV_CODEC_ID_ADPCM_SWF:
  1586. case AV_CODEC_ID_ADPCM_MS:
  1587. return 4;
  1588. default:
  1589. return av_get_exact_bits_per_sample(codec_id);
  1590. }
  1591. }
  1592. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1593. {
  1594. int id, sr, ch, ba, tag, bps;
  1595. id = avctx->codec_id;
  1596. sr = avctx->sample_rate;
  1597. ch = avctx->channels;
  1598. ba = avctx->block_align;
  1599. tag = avctx->codec_tag;
  1600. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1601. /* codecs with an exact constant bits per sample */
  1602. if (bps > 0 && ch > 0 && frame_bytes > 0)
  1603. return (frame_bytes * 8) / (bps * ch);
  1604. bps = avctx->bits_per_coded_sample;
  1605. /* codecs with a fixed packet duration */
  1606. switch (id) {
  1607. case AV_CODEC_ID_ADPCM_ADX: return 32;
  1608. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  1609. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  1610. case AV_CODEC_ID_AMR_NB:
  1611. case AV_CODEC_ID_GSM:
  1612. case AV_CODEC_ID_QCELP:
  1613. case AV_CODEC_ID_RA_144:
  1614. case AV_CODEC_ID_RA_288: return 160;
  1615. case AV_CODEC_ID_IMC: return 256;
  1616. case AV_CODEC_ID_AMR_WB:
  1617. case AV_CODEC_ID_GSM_MS: return 320;
  1618. case AV_CODEC_ID_MP1: return 384;
  1619. case AV_CODEC_ID_ATRAC1: return 512;
  1620. case AV_CODEC_ID_ATRAC3: return 1024;
  1621. case AV_CODEC_ID_MP2:
  1622. case AV_CODEC_ID_MUSEPACK7: return 1152;
  1623. case AV_CODEC_ID_AC3: return 1536;
  1624. }
  1625. if (sr > 0) {
  1626. /* calc from sample rate */
  1627. if (id == AV_CODEC_ID_TTA)
  1628. return 256 * sr / 245;
  1629. if (ch > 0) {
  1630. /* calc from sample rate and channels */
  1631. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  1632. return (480 << (sr / 22050)) / ch;
  1633. }
  1634. }
  1635. if (ba > 0) {
  1636. /* calc from block_align */
  1637. if (id == AV_CODEC_ID_SIPR) {
  1638. switch (ba) {
  1639. case 20: return 160;
  1640. case 19: return 144;
  1641. case 29: return 288;
  1642. case 37: return 480;
  1643. }
  1644. } else if (id == AV_CODEC_ID_ILBC) {
  1645. switch (ba) {
  1646. case 38: return 160;
  1647. case 50: return 240;
  1648. }
  1649. }
  1650. }
  1651. if (frame_bytes > 0) {
  1652. /* calc from frame_bytes only */
  1653. if (id == AV_CODEC_ID_TRUESPEECH)
  1654. return 240 * (frame_bytes / 32);
  1655. if (id == AV_CODEC_ID_NELLYMOSER)
  1656. return 256 * (frame_bytes / 64);
  1657. if (bps > 0) {
  1658. /* calc from frame_bytes and bits_per_coded_sample */
  1659. if (id == AV_CODEC_ID_ADPCM_G726)
  1660. return frame_bytes * 8 / bps;
  1661. }
  1662. if (ch > 0) {
  1663. /* calc from frame_bytes and channels */
  1664. switch (id) {
  1665. case AV_CODEC_ID_ADPCM_4XM:
  1666. case AV_CODEC_ID_ADPCM_IMA_ISS:
  1667. return (frame_bytes - 4 * ch) * 2 / ch;
  1668. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1669. return (frame_bytes - 4) * 2 / ch;
  1670. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1671. return (frame_bytes - 8) * 2 / ch;
  1672. case AV_CODEC_ID_ADPCM_XA:
  1673. return (frame_bytes / 128) * 224 / ch;
  1674. case AV_CODEC_ID_INTERPLAY_DPCM:
  1675. return (frame_bytes - 6 - ch) / ch;
  1676. case AV_CODEC_ID_ROQ_DPCM:
  1677. return (frame_bytes - 8) / ch;
  1678. case AV_CODEC_ID_XAN_DPCM:
  1679. return (frame_bytes - 2 * ch) / ch;
  1680. case AV_CODEC_ID_MACE3:
  1681. return 3 * frame_bytes / ch;
  1682. case AV_CODEC_ID_MACE6:
  1683. return 6 * frame_bytes / ch;
  1684. case AV_CODEC_ID_PCM_LXF:
  1685. return 2 * (frame_bytes / (5 * ch));
  1686. }
  1687. if (tag) {
  1688. /* calc from frame_bytes, channels, and codec_tag */
  1689. if (id == AV_CODEC_ID_SOL_DPCM) {
  1690. if (tag == 3)
  1691. return frame_bytes / ch;
  1692. else
  1693. return frame_bytes * 2 / ch;
  1694. }
  1695. }
  1696. if (ba > 0) {
  1697. /* calc from frame_bytes, channels, and block_align */
  1698. int blocks = frame_bytes / ba;
  1699. switch (avctx->codec_id) {
  1700. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1701. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1702. case AV_CODEC_ID_ADPCM_IMA_DK3:
  1703. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1704. case AV_CODEC_ID_ADPCM_IMA_DK4:
  1705. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1706. case AV_CODEC_ID_ADPCM_MS:
  1707. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1708. }
  1709. }
  1710. if (bps > 0) {
  1711. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1712. switch (avctx->codec_id) {
  1713. case AV_CODEC_ID_PCM_DVD:
  1714. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1715. case AV_CODEC_ID_PCM_BLURAY:
  1716. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  1717. case AV_CODEC_ID_S302M:
  1718. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1719. }
  1720. }
  1721. }
  1722. }
  1723. return 0;
  1724. }
  1725. #if !HAVE_THREADS
  1726. int ff_thread_init(AVCodecContext *s)
  1727. {
  1728. return -1;
  1729. }
  1730. #endif
  1731. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1732. {
  1733. unsigned int n = 0;
  1734. while (v >= 0xff) {
  1735. *s++ = 0xff;
  1736. v -= 0xff;
  1737. n++;
  1738. }
  1739. *s = v;
  1740. n++;
  1741. return n;
  1742. }
  1743. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  1744. {
  1745. int i;
  1746. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  1747. return i;
  1748. }
  1749. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1750. {
  1751. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
  1752. "version to the newest one from Git. If the problem still "
  1753. "occurs, it means that your file has a feature which has not "
  1754. "been implemented.\n", feature);
  1755. if(want_sample)
  1756. av_log_ask_for_sample(avc, NULL);
  1757. }
  1758. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1759. {
  1760. va_list argument_list;
  1761. va_start(argument_list, msg);
  1762. if (msg)
  1763. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1764. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1765. "of this file to ftp://upload.libav.org/incoming/ "
  1766. "and contact the libav-devel mailing list.\n");
  1767. va_end(argument_list);
  1768. }
  1769. static AVHWAccel *first_hwaccel = NULL;
  1770. void av_register_hwaccel(AVHWAccel *hwaccel)
  1771. {
  1772. AVHWAccel **p = &first_hwaccel;
  1773. while (*p)
  1774. p = &(*p)->next;
  1775. *p = hwaccel;
  1776. hwaccel->next = NULL;
  1777. }
  1778. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1779. {
  1780. return hwaccel ? hwaccel->next : first_hwaccel;
  1781. }
  1782. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
  1783. {
  1784. AVHWAccel *hwaccel = NULL;
  1785. while ((hwaccel = av_hwaccel_next(hwaccel)))
  1786. if (hwaccel->id == codec_id
  1787. && hwaccel->pix_fmt == pix_fmt)
  1788. return hwaccel;
  1789. return NULL;
  1790. }
  1791. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1792. {
  1793. if (ff_lockmgr_cb) {
  1794. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1795. return -1;
  1796. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1797. return -1;
  1798. }
  1799. ff_lockmgr_cb = cb;
  1800. if (ff_lockmgr_cb) {
  1801. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1802. return -1;
  1803. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1804. return -1;
  1805. }
  1806. return 0;
  1807. }
  1808. int avpriv_lock_avformat(void)
  1809. {
  1810. if (ff_lockmgr_cb) {
  1811. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1812. return -1;
  1813. }
  1814. return 0;
  1815. }
  1816. int avpriv_unlock_avformat(void)
  1817. {
  1818. if (ff_lockmgr_cb) {
  1819. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1820. return -1;
  1821. }
  1822. return 0;
  1823. }
  1824. unsigned int avpriv_toupper4(unsigned int x)
  1825. {
  1826. return toupper(x & 0xFF)
  1827. + (toupper((x >> 8) & 0xFF) << 8)
  1828. + (toupper((x >> 16) & 0xFF) << 16)
  1829. + (toupper((x >> 24) & 0xFF) << 24);
  1830. }
  1831. #if !HAVE_THREADS
  1832. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1833. {
  1834. f->owner = avctx;
  1835. return ff_get_buffer(avctx, f);
  1836. }
  1837. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1838. {
  1839. f->owner->release_buffer(f->owner, f);
  1840. }
  1841. void ff_thread_finish_setup(AVCodecContext *avctx)
  1842. {
  1843. }
  1844. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1845. {
  1846. }
  1847. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1848. {
  1849. }
  1850. #endif
  1851. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  1852. {
  1853. if (codec_id <= AV_CODEC_ID_NONE)
  1854. return AVMEDIA_TYPE_UNKNOWN;
  1855. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  1856. return AVMEDIA_TYPE_VIDEO;
  1857. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  1858. return AVMEDIA_TYPE_AUDIO;
  1859. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  1860. return AVMEDIA_TYPE_SUBTITLE;
  1861. return AVMEDIA_TYPE_UNKNOWN;
  1862. }
  1863. int avcodec_is_open(AVCodecContext *s)
  1864. {
  1865. return !!s->internal;
  1866. }