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.

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