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.

2539 lines
76KB

  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/attributes.h"
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/avstring.h"
  30. #include "libavutil/channel_layout.h"
  31. #include "libavutil/crc.h"
  32. #include "libavutil/frame.h"
  33. #include "libavutil/internal.h"
  34. #include "libavutil/mathematics.h"
  35. #include "libavutil/pixdesc.h"
  36. #include "libavutil/imgutils.h"
  37. #include "libavutil/samplefmt.h"
  38. #include "libavutil/dict.h"
  39. #include "avcodec.h"
  40. #include "libavutil/opt.h"
  41. #include "me_cmp.h"
  42. #include "mpegvideo.h"
  43. #include "thread.h"
  44. #include "internal.h"
  45. #include "bytestream.h"
  46. #include "version.h"
  47. #include <stdlib.h>
  48. #include <stdarg.h>
  49. #include <limits.h>
  50. #include <float.h>
  51. static int volatile entangled_thread_counter = 0;
  52. static int (*lockmgr_cb)(void **mutex, enum AVLockOp op);
  53. static void *codec_mutex;
  54. static void *avformat_mutex;
  55. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  56. {
  57. void **p = ptr;
  58. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  59. av_freep(p);
  60. *size = 0;
  61. return;
  62. }
  63. av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  64. if (*size)
  65. memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  66. }
  67. /* encoder management */
  68. static AVCodec *first_avcodec = NULL;
  69. AVCodec *av_codec_next(const AVCodec *c)
  70. {
  71. if (c)
  72. return c->next;
  73. else
  74. return first_avcodec;
  75. }
  76. static av_cold void avcodec_init(void)
  77. {
  78. static int initialized = 0;
  79. if (initialized != 0)
  80. return;
  81. initialized = 1;
  82. if (CONFIG_ME_CMP)
  83. ff_me_cmp_init_static();
  84. }
  85. int av_codec_is_encoder(const AVCodec *codec)
  86. {
  87. return codec && (codec->encode_sub || codec->encode2);
  88. }
  89. int av_codec_is_decoder(const AVCodec *codec)
  90. {
  91. return codec && codec->decode;
  92. }
  93. av_cold void avcodec_register(AVCodec *codec)
  94. {
  95. AVCodec **p;
  96. avcodec_init();
  97. p = &first_avcodec;
  98. while (*p)
  99. p = &(*p)->next;
  100. *p = codec;
  101. codec->next = NULL;
  102. if (codec->init_static_data)
  103. codec->init_static_data(codec);
  104. }
  105. #if FF_API_EMU_EDGE
  106. unsigned avcodec_get_edge_width(void)
  107. {
  108. return EDGE_WIDTH;
  109. }
  110. #endif
  111. #if FF_API_SET_DIMENSIONS
  112. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  113. {
  114. ff_set_dimensions(s, width, height);
  115. }
  116. #endif
  117. int ff_set_dimensions(AVCodecContext *s, int width, int height)
  118. {
  119. int ret = av_image_check_size(width, height, 0, s);
  120. if (ret < 0)
  121. width = height = 0;
  122. s->width = s->coded_width = width;
  123. s->height = s->coded_height = height;
  124. return ret;
  125. }
  126. int ff_set_sar(AVCodecContext *avctx, AVRational sar)
  127. {
  128. int ret = av_image_check_sar(avctx->width, avctx->height, sar);
  129. if (ret < 0) {
  130. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  131. sar.num, sar.den);
  132. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  133. return ret;
  134. } else {
  135. avctx->sample_aspect_ratio = sar;
  136. }
  137. return 0;
  138. }
  139. int ff_side_data_update_matrix_encoding(AVFrame *frame,
  140. enum AVMatrixEncoding matrix_encoding)
  141. {
  142. AVFrameSideData *side_data;
  143. enum AVMatrixEncoding *data;
  144. side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
  145. if (!side_data)
  146. side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
  147. sizeof(enum AVMatrixEncoding));
  148. if (!side_data)
  149. return AVERROR(ENOMEM);
  150. data = (enum AVMatrixEncoding*)side_data->data;
  151. *data = matrix_encoding;
  152. return 0;
  153. }
  154. #if HAVE_SIMD_ALIGN_16
  155. # define STRIDE_ALIGN 16
  156. #else
  157. # define STRIDE_ALIGN 8
  158. #endif
  159. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  160. int linesize_align[AV_NUM_DATA_POINTERS])
  161. {
  162. int i;
  163. int w_align = 1;
  164. int h_align = 1;
  165. switch (s->pix_fmt) {
  166. case AV_PIX_FMT_YUV420P:
  167. case AV_PIX_FMT_YUYV422:
  168. case AV_PIX_FMT_YVYU422:
  169. case AV_PIX_FMT_UYVY422:
  170. case AV_PIX_FMT_YUV422P:
  171. case AV_PIX_FMT_YUV440P:
  172. case AV_PIX_FMT_YUV444P:
  173. case AV_PIX_FMT_GBRP:
  174. case AV_PIX_FMT_GBRAP:
  175. case AV_PIX_FMT_GRAY8:
  176. case AV_PIX_FMT_GRAY16BE:
  177. case AV_PIX_FMT_GRAY16LE:
  178. case AV_PIX_FMT_YUVJ420P:
  179. case AV_PIX_FMT_YUVJ422P:
  180. case AV_PIX_FMT_YUVJ440P:
  181. case AV_PIX_FMT_YUVJ444P:
  182. case AV_PIX_FMT_YUVA420P:
  183. case AV_PIX_FMT_YUVA422P:
  184. case AV_PIX_FMT_YUVA444P:
  185. case AV_PIX_FMT_YUV420P9LE:
  186. case AV_PIX_FMT_YUV420P9BE:
  187. case AV_PIX_FMT_YUV420P10LE:
  188. case AV_PIX_FMT_YUV420P10BE:
  189. case AV_PIX_FMT_YUV422P9LE:
  190. case AV_PIX_FMT_YUV422P9BE:
  191. case AV_PIX_FMT_YUV422P10LE:
  192. case AV_PIX_FMT_YUV422P10BE:
  193. case AV_PIX_FMT_YUVA422P10LE:
  194. case AV_PIX_FMT_YUVA422P10BE:
  195. case AV_PIX_FMT_YUV444P9LE:
  196. case AV_PIX_FMT_YUV444P9BE:
  197. case AV_PIX_FMT_YUV444P10LE:
  198. case AV_PIX_FMT_YUV444P10BE:
  199. case AV_PIX_FMT_YUVA444P10LE:
  200. case AV_PIX_FMT_YUVA444P10BE:
  201. case AV_PIX_FMT_GBRP9LE:
  202. case AV_PIX_FMT_GBRP9BE:
  203. case AV_PIX_FMT_GBRP10LE:
  204. case AV_PIX_FMT_GBRP10BE:
  205. w_align = 16; //FIXME assume 16 pixel per macroblock
  206. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  207. break;
  208. case AV_PIX_FMT_YUV411P:
  209. case AV_PIX_FMT_UYYVYY411:
  210. w_align = 32;
  211. h_align = 8;
  212. break;
  213. case AV_PIX_FMT_YUV410P:
  214. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  215. w_align = 64;
  216. h_align = 64;
  217. }
  218. case AV_PIX_FMT_RGB555:
  219. if (s->codec_id == AV_CODEC_ID_RPZA) {
  220. w_align = 4;
  221. h_align = 4;
  222. }
  223. case AV_PIX_FMT_PAL8:
  224. case AV_PIX_FMT_BGR8:
  225. case AV_PIX_FMT_RGB8:
  226. if (s->codec_id == AV_CODEC_ID_SMC) {
  227. w_align = 4;
  228. h_align = 4;
  229. }
  230. break;
  231. case AV_PIX_FMT_BGR24:
  232. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  233. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  234. w_align = 4;
  235. h_align = 4;
  236. }
  237. break;
  238. default:
  239. w_align = 1;
  240. h_align = 1;
  241. break;
  242. }
  243. *width = FFALIGN(*width, w_align);
  244. *height = FFALIGN(*height, h_align);
  245. if (s->codec_id == AV_CODEC_ID_H264)
  246. // some of the optimized chroma MC reads one line too much
  247. *height += 2;
  248. for (i = 0; i < 4; i++)
  249. linesize_align[i] = STRIDE_ALIGN;
  250. }
  251. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  252. {
  253. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  254. int chroma_shift = desc->log2_chroma_w;
  255. int linesize_align[AV_NUM_DATA_POINTERS];
  256. int align;
  257. avcodec_align_dimensions2(s, width, height, linesize_align);
  258. align = FFMAX(linesize_align[0], linesize_align[3]);
  259. linesize_align[1] <<= chroma_shift;
  260. linesize_align[2] <<= chroma_shift;
  261. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  262. *width = FFALIGN(*width, align);
  263. }
  264. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  265. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  266. int buf_size, int align)
  267. {
  268. int ch, planar, needed_size, ret = 0;
  269. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  270. frame->nb_samples, sample_fmt,
  271. align);
  272. if (buf_size < needed_size)
  273. return AVERROR(EINVAL);
  274. planar = av_sample_fmt_is_planar(sample_fmt);
  275. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  276. if (!(frame->extended_data = av_mallocz(nb_channels *
  277. sizeof(*frame->extended_data))))
  278. return AVERROR(ENOMEM);
  279. } else {
  280. frame->extended_data = frame->data;
  281. }
  282. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  283. buf, nb_channels, frame->nb_samples,
  284. sample_fmt, align)) < 0) {
  285. if (frame->extended_data != frame->data)
  286. av_free(frame->extended_data);
  287. return ret;
  288. }
  289. if (frame->extended_data != frame->data) {
  290. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  291. frame->data[ch] = frame->extended_data[ch];
  292. }
  293. return ret;
  294. }
  295. static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
  296. {
  297. FramePool *pool = avctx->internal->pool;
  298. int i, ret;
  299. switch (avctx->codec_type) {
  300. case AVMEDIA_TYPE_VIDEO: {
  301. AVPicture picture;
  302. int size[4] = { 0 };
  303. int w = frame->width;
  304. int h = frame->height;
  305. int tmpsize, unaligned;
  306. if (pool->format == frame->format &&
  307. pool->width == frame->width && pool->height == frame->height)
  308. return 0;
  309. avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
  310. do {
  311. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  312. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  313. av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
  314. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  315. w += w & ~(w - 1);
  316. unaligned = 0;
  317. for (i = 0; i < 4; i++)
  318. unaligned |= picture.linesize[i] % pool->stride_align[i];
  319. } while (unaligned);
  320. tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
  321. NULL, picture.linesize);
  322. if (tmpsize < 0)
  323. return -1;
  324. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  325. size[i] = picture.data[i + 1] - picture.data[i];
  326. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  327. for (i = 0; i < 4; i++) {
  328. av_buffer_pool_uninit(&pool->pools[i]);
  329. pool->linesize[i] = picture.linesize[i];
  330. if (size[i]) {
  331. pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
  332. if (!pool->pools[i]) {
  333. ret = AVERROR(ENOMEM);
  334. goto fail;
  335. }
  336. }
  337. }
  338. pool->format = frame->format;
  339. pool->width = frame->width;
  340. pool->height = frame->height;
  341. break;
  342. }
  343. case AVMEDIA_TYPE_AUDIO: {
  344. int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
  345. int planar = av_sample_fmt_is_planar(frame->format);
  346. int planes = planar ? ch : 1;
  347. if (pool->format == frame->format && pool->planes == planes &&
  348. pool->channels == ch && frame->nb_samples == pool->samples)
  349. return 0;
  350. av_buffer_pool_uninit(&pool->pools[0]);
  351. ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
  352. frame->nb_samples, frame->format, 0);
  353. if (ret < 0)
  354. goto fail;
  355. pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
  356. if (!pool->pools[0]) {
  357. ret = AVERROR(ENOMEM);
  358. goto fail;
  359. }
  360. pool->format = frame->format;
  361. pool->planes = planes;
  362. pool->channels = ch;
  363. pool->samples = frame->nb_samples;
  364. break;
  365. }
  366. default: av_assert0(0);
  367. }
  368. return 0;
  369. fail:
  370. for (i = 0; i < 4; i++)
  371. av_buffer_pool_uninit(&pool->pools[i]);
  372. pool->format = -1;
  373. pool->planes = pool->channels = pool->samples = 0;
  374. pool->width = pool->height = 0;
  375. return ret;
  376. }
  377. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  378. {
  379. FramePool *pool = avctx->internal->pool;
  380. int planes = pool->planes;
  381. int i;
  382. frame->linesize[0] = pool->linesize[0];
  383. if (planes > AV_NUM_DATA_POINTERS) {
  384. frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
  385. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  386. frame->extended_buf = av_mallocz(frame->nb_extended_buf *
  387. sizeof(*frame->extended_buf));
  388. if (!frame->extended_data || !frame->extended_buf) {
  389. av_freep(&frame->extended_data);
  390. av_freep(&frame->extended_buf);
  391. return AVERROR(ENOMEM);
  392. }
  393. } else
  394. frame->extended_data = frame->data;
  395. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  396. frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
  397. if (!frame->buf[i])
  398. goto fail;
  399. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  400. }
  401. for (i = 0; i < frame->nb_extended_buf; i++) {
  402. frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
  403. if (!frame->extended_buf[i])
  404. goto fail;
  405. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  406. }
  407. if (avctx->debug & FF_DEBUG_BUFFERS)
  408. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
  409. return 0;
  410. fail:
  411. av_frame_unref(frame);
  412. return AVERROR(ENOMEM);
  413. }
  414. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  415. {
  416. FramePool *pool = s->internal->pool;
  417. int i;
  418. if (pic->data[0]) {
  419. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  420. return -1;
  421. }
  422. memset(pic->data, 0, sizeof(pic->data));
  423. pic->extended_data = pic->data;
  424. for (i = 0; i < 4 && pool->pools[i]; i++) {
  425. pic->linesize[i] = pool->linesize[i];
  426. pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
  427. if (!pic->buf[i])
  428. goto fail;
  429. pic->data[i] = pic->buf[i]->data;
  430. }
  431. for (; i < AV_NUM_DATA_POINTERS; i++) {
  432. pic->data[i] = NULL;
  433. pic->linesize[i] = 0;
  434. }
  435. if (pic->data[1] && !pic->data[2])
  436. avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
  437. if (s->debug & FF_DEBUG_BUFFERS)
  438. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
  439. return 0;
  440. fail:
  441. av_frame_unref(pic);
  442. return AVERROR(ENOMEM);
  443. }
  444. int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
  445. {
  446. int ret;
  447. if ((ret = update_frame_pool(avctx, frame)) < 0)
  448. return ret;
  449. #if FF_API_GET_BUFFER
  450. FF_DISABLE_DEPRECATION_WARNINGS
  451. frame->type = FF_BUFFER_TYPE_INTERNAL;
  452. FF_ENABLE_DEPRECATION_WARNINGS
  453. #endif
  454. switch (avctx->codec_type) {
  455. case AVMEDIA_TYPE_VIDEO:
  456. return video_get_buffer(avctx, frame);
  457. case AVMEDIA_TYPE_AUDIO:
  458. return audio_get_buffer(avctx, frame);
  459. default:
  460. return -1;
  461. }
  462. }
  463. #if FF_API_GET_BUFFER
  464. FF_DISABLE_DEPRECATION_WARNINGS
  465. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  466. {
  467. return avcodec_default_get_buffer2(avctx, frame, 0);
  468. }
  469. typedef struct CompatReleaseBufPriv {
  470. AVCodecContext avctx;
  471. AVFrame frame;
  472. } CompatReleaseBufPriv;
  473. static void compat_free_buffer(void *opaque, uint8_t *data)
  474. {
  475. CompatReleaseBufPriv *priv = opaque;
  476. if (priv->avctx.release_buffer)
  477. priv->avctx.release_buffer(&priv->avctx, &priv->frame);
  478. av_freep(&priv);
  479. }
  480. static void compat_release_buffer(void *opaque, uint8_t *data)
  481. {
  482. AVBufferRef *buf = opaque;
  483. av_buffer_unref(&buf);
  484. }
  485. FF_ENABLE_DEPRECATION_WARNINGS
  486. #endif
  487. int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
  488. {
  489. AVPacket *pkt = avctx->internal->pkt;
  490. int i;
  491. struct {
  492. enum AVPacketSideDataType packet;
  493. enum AVFrameSideDataType frame;
  494. } sd[] = {
  495. { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
  496. { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
  497. { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
  498. };
  499. frame->color_primaries = avctx->color_primaries;
  500. frame->color_trc = avctx->color_trc;
  501. frame->colorspace = avctx->colorspace;
  502. frame->color_range = avctx->color_range;
  503. frame->chroma_location = avctx->chroma_sample_location;
  504. frame->reordered_opaque = avctx->reordered_opaque;
  505. if (!pkt) {
  506. frame->pkt_pts = AV_NOPTS_VALUE;
  507. return 0;
  508. }
  509. frame->pkt_pts = pkt->pts;
  510. for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
  511. int size;
  512. uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
  513. if (packet_sd) {
  514. AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
  515. sd[i].frame,
  516. size);
  517. if (!frame_sd)
  518. return AVERROR(ENOMEM);
  519. memcpy(frame_sd->data, packet_sd, size);
  520. }
  521. }
  522. return 0;
  523. }
  524. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  525. {
  526. const AVHWAccel *hwaccel = avctx->hwaccel;
  527. int override_dimensions = 1;
  528. int ret;
  529. switch (avctx->codec_type) {
  530. case AVMEDIA_TYPE_VIDEO:
  531. if (frame->width <= 0 || frame->height <= 0) {
  532. frame->width = FFMAX(avctx->width, avctx->coded_width);
  533. frame->height = FFMAX(avctx->height, avctx->coded_height);
  534. override_dimensions = 0;
  535. }
  536. if (frame->format < 0)
  537. frame->format = avctx->pix_fmt;
  538. if (!frame->sample_aspect_ratio.num)
  539. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  540. if (av_image_check_sar(frame->width, frame->height,
  541. frame->sample_aspect_ratio) < 0) {
  542. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  543. frame->sample_aspect_ratio.num,
  544. frame->sample_aspect_ratio.den);
  545. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  546. }
  547. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
  548. return ret;
  549. break;
  550. case AVMEDIA_TYPE_AUDIO:
  551. if (!frame->sample_rate)
  552. frame->sample_rate = avctx->sample_rate;
  553. if (frame->format < 0)
  554. frame->format = avctx->sample_fmt;
  555. if (!frame->channel_layout) {
  556. if (avctx->channel_layout) {
  557. if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  558. avctx->channels) {
  559. av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  560. "configuration.\n");
  561. return AVERROR(EINVAL);
  562. }
  563. frame->channel_layout = avctx->channel_layout;
  564. } else {
  565. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  566. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  567. avctx->channels);
  568. return AVERROR(ENOSYS);
  569. }
  570. frame->channel_layout = av_get_default_channel_layout(avctx->channels);
  571. if (!frame->channel_layout)
  572. frame->channel_layout = (1ULL << avctx->channels) - 1;
  573. }
  574. }
  575. break;
  576. default: return AVERROR(EINVAL);
  577. }
  578. ret = ff_decode_frame_props(avctx, frame);
  579. if (ret < 0)
  580. return ret;
  581. if (hwaccel) {
  582. if (hwaccel->alloc_frame) {
  583. ret = hwaccel->alloc_frame(avctx, frame);
  584. goto end;
  585. }
  586. } else
  587. avctx->sw_pix_fmt = avctx->pix_fmt;
  588. #if FF_API_GET_BUFFER
  589. FF_DISABLE_DEPRECATION_WARNINGS
  590. /*
  591. * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
  592. * We wrap each plane in its own AVBuffer. Each of those has a reference to
  593. * a dummy AVBuffer as its private data, unreffing it on free.
  594. * When all the planes are freed, the dummy buffer's free callback calls
  595. * release_buffer().
  596. */
  597. if (avctx->get_buffer) {
  598. CompatReleaseBufPriv *priv = NULL;
  599. AVBufferRef *dummy_buf = NULL;
  600. int planes, i, ret;
  601. if (flags & AV_GET_BUFFER_FLAG_REF)
  602. frame->reference = 1;
  603. ret = avctx->get_buffer(avctx, frame);
  604. if (ret < 0)
  605. return ret;
  606. /* return if the buffers are already set up
  607. * this would happen e.g. when a custom get_buffer() calls
  608. * avcodec_default_get_buffer
  609. */
  610. if (frame->buf[0])
  611. return 0;
  612. priv = av_mallocz(sizeof(*priv));
  613. if (!priv) {
  614. ret = AVERROR(ENOMEM);
  615. goto fail;
  616. }
  617. priv->avctx = *avctx;
  618. priv->frame = *frame;
  619. dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
  620. if (!dummy_buf) {
  621. ret = AVERROR(ENOMEM);
  622. goto fail;
  623. }
  624. #define WRAP_PLANE(ref_out, data, data_size) \
  625. do { \
  626. AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
  627. if (!dummy_ref) { \
  628. ret = AVERROR(ENOMEM); \
  629. goto fail; \
  630. } \
  631. ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
  632. dummy_ref, 0); \
  633. if (!ref_out) { \
  634. av_frame_unref(frame); \
  635. ret = AVERROR(ENOMEM); \
  636. goto fail; \
  637. } \
  638. } while (0)
  639. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  640. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  641. planes = av_pix_fmt_count_planes(frame->format);
  642. /* workaround for AVHWAccel plane count of 0, buf[0] is used as
  643. check for allocated buffers: make libavcodec happy */
  644. if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  645. planes = 1;
  646. if (!desc || planes <= 0) {
  647. ret = AVERROR(EINVAL);
  648. goto fail;
  649. }
  650. for (i = 0; i < planes; i++) {
  651. int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  652. int plane_size = (frame->height >> v_shift) * frame->linesize[i];
  653. WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
  654. }
  655. } else {
  656. int planar = av_sample_fmt_is_planar(frame->format);
  657. planes = planar ? avctx->channels : 1;
  658. if (planes > FF_ARRAY_ELEMS(frame->buf)) {
  659. frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
  660. frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
  661. frame->nb_extended_buf);
  662. if (!frame->extended_buf) {
  663. ret = AVERROR(ENOMEM);
  664. goto fail;
  665. }
  666. }
  667. for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
  668. WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
  669. for (i = 0; i < frame->nb_extended_buf; i++)
  670. WRAP_PLANE(frame->extended_buf[i],
  671. frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
  672. frame->linesize[0]);
  673. }
  674. av_buffer_unref(&dummy_buf);
  675. frame->width = avctx->width;
  676. frame->height = avctx->height;
  677. return 0;
  678. fail:
  679. avctx->release_buffer(avctx, frame);
  680. av_freep(&priv);
  681. av_buffer_unref(&dummy_buf);
  682. return ret;
  683. }
  684. FF_ENABLE_DEPRECATION_WARNINGS
  685. #endif
  686. ret = avctx->get_buffer2(avctx, frame, flags);
  687. end:
  688. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
  689. frame->width = avctx->width;
  690. frame->height = avctx->height;
  691. }
  692. return ret;
  693. }
  694. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  695. {
  696. AVFrame *tmp;
  697. int ret;
  698. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  699. if (!frame->data[0])
  700. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  701. if (av_frame_is_writable(frame))
  702. return ff_decode_frame_props(avctx, frame);
  703. tmp = av_frame_alloc();
  704. if (!tmp)
  705. return AVERROR(ENOMEM);
  706. av_frame_move_ref(tmp, frame);
  707. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  708. if (ret < 0) {
  709. av_frame_free(&tmp);
  710. return ret;
  711. }
  712. av_frame_copy(frame, tmp);
  713. av_frame_free(&tmp);
  714. return 0;
  715. }
  716. #if FF_API_GET_BUFFER
  717. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  718. {
  719. av_frame_unref(pic);
  720. }
  721. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  722. {
  723. av_assert0(0);
  724. return AVERROR_BUG;
  725. }
  726. #endif
  727. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  728. {
  729. int i;
  730. for (i = 0; i < count; i++) {
  731. int r = func(c, (char *)arg + i * size);
  732. if (ret)
  733. ret[i] = r;
  734. }
  735. return 0;
  736. }
  737. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  738. {
  739. int i;
  740. for (i = 0; i < count; i++) {
  741. int r = func(c, arg, i, 0);
  742. if (ret)
  743. ret[i] = r;
  744. }
  745. return 0;
  746. }
  747. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  748. {
  749. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  750. return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
  751. }
  752. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  753. {
  754. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  755. ++fmt;
  756. return fmt[0];
  757. }
  758. static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
  759. enum AVPixelFormat pix_fmt)
  760. {
  761. AVHWAccel *hwaccel = NULL;
  762. while ((hwaccel = av_hwaccel_next(hwaccel)))
  763. if (hwaccel->id == codec_id
  764. && hwaccel->pix_fmt == pix_fmt)
  765. return hwaccel;
  766. return NULL;
  767. }
  768. static int setup_hwaccel(AVCodecContext *avctx,
  769. const enum AVPixelFormat fmt,
  770. const char *name)
  771. {
  772. AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
  773. int ret = 0;
  774. if (!hwa) {
  775. av_log(avctx, AV_LOG_ERROR,
  776. "Could not find an AVHWAccel for the pixel format: %s",
  777. name);
  778. return AVERROR(ENOENT);
  779. }
  780. if (hwa->priv_data_size) {
  781. avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
  782. if (!avctx->internal->hwaccel_priv_data)
  783. return AVERROR(ENOMEM);
  784. }
  785. if (hwa->init) {
  786. ret = hwa->init(avctx);
  787. if (ret < 0) {
  788. av_freep(&avctx->internal->hwaccel_priv_data);
  789. return ret;
  790. }
  791. }
  792. avctx->hwaccel = hwa;
  793. return 0;
  794. }
  795. int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  796. {
  797. const AVPixFmtDescriptor *desc;
  798. enum AVPixelFormat *choices;
  799. enum AVPixelFormat ret;
  800. unsigned n = 0;
  801. while (fmt[n] != AV_PIX_FMT_NONE)
  802. ++n;
  803. av_assert0(n >= 1);
  804. avctx->sw_pix_fmt = fmt[n - 1];
  805. av_assert2(!is_hwaccel_pix_fmt(avctx->sw_pix_fmt));
  806. choices = av_malloc_array(n + 1, sizeof(*choices));
  807. if (!choices)
  808. return AV_PIX_FMT_NONE;
  809. memcpy(choices, fmt, (n + 1) * sizeof(*choices));
  810. for (;;) {
  811. if (avctx->hwaccel && avctx->hwaccel->uninit)
  812. avctx->hwaccel->uninit(avctx);
  813. av_freep(&avctx->internal->hwaccel_priv_data);
  814. avctx->hwaccel = NULL;
  815. ret = avctx->get_format(avctx, choices);
  816. desc = av_pix_fmt_desc_get(ret);
  817. if (!desc) {
  818. ret = AV_PIX_FMT_NONE;
  819. break;
  820. }
  821. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
  822. break;
  823. if (!setup_hwaccel(avctx, ret, desc->name))
  824. break;
  825. /* Remove failed hwaccel from choices */
  826. for (n = 0; choices[n] != ret; n++)
  827. av_assert0(choices[n] != AV_PIX_FMT_NONE);
  828. do
  829. choices[n] = choices[n + 1];
  830. while (choices[n++] != AV_PIX_FMT_NONE);
  831. }
  832. av_freep(&choices);
  833. return ret;
  834. }
  835. #if FF_API_AVFRAME_LAVC
  836. void avcodec_get_frame_defaults(AVFrame *frame)
  837. {
  838. if (frame->extended_data != frame->data)
  839. av_freep(&frame->extended_data);
  840. memset(frame, 0, sizeof(AVFrame));
  841. frame->pts = AV_NOPTS_VALUE;
  842. frame->key_frame = 1;
  843. frame->sample_aspect_ratio = (AVRational) {0, 1 };
  844. frame->format = -1; /* unknown */
  845. frame->extended_data = frame->data;
  846. }
  847. AVFrame *avcodec_alloc_frame(void)
  848. {
  849. AVFrame *frame = av_mallocz(sizeof(AVFrame));
  850. if (!frame)
  851. return NULL;
  852. FF_DISABLE_DEPRECATION_WARNINGS
  853. avcodec_get_frame_defaults(frame);
  854. FF_ENABLE_DEPRECATION_WARNINGS
  855. return frame;
  856. }
  857. void avcodec_free_frame(AVFrame **frame)
  858. {
  859. av_frame_free(frame);
  860. }
  861. #endif
  862. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  863. {
  864. int ret = 0;
  865. AVDictionary *tmp = NULL;
  866. if (avcodec_is_open(avctx))
  867. return 0;
  868. if ((!codec && !avctx->codec)) {
  869. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  870. return AVERROR(EINVAL);
  871. }
  872. if ((codec && avctx->codec && codec != avctx->codec)) {
  873. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  874. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  875. return AVERROR(EINVAL);
  876. }
  877. if (!codec)
  878. codec = avctx->codec;
  879. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  880. return AVERROR(EINVAL);
  881. if (options)
  882. av_dict_copy(&tmp, *options, 0);
  883. /* If there is a user-supplied mutex locking routine, call it. */
  884. if (lockmgr_cb) {
  885. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  886. return -1;
  887. }
  888. entangled_thread_counter++;
  889. if (entangled_thread_counter != 1) {
  890. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  891. ret = -1;
  892. goto end;
  893. }
  894. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  895. if (!avctx->internal) {
  896. ret = AVERROR(ENOMEM);
  897. goto end;
  898. }
  899. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  900. if (!avctx->internal->pool) {
  901. ret = AVERROR(ENOMEM);
  902. goto free_and_end;
  903. }
  904. avctx->internal->to_free = av_frame_alloc();
  905. if (!avctx->internal->to_free) {
  906. ret = AVERROR(ENOMEM);
  907. goto free_and_end;
  908. }
  909. if (codec->priv_data_size > 0) {
  910. if (!avctx->priv_data) {
  911. avctx->priv_data = av_mallocz(codec->priv_data_size);
  912. if (!avctx->priv_data) {
  913. ret = AVERROR(ENOMEM);
  914. goto end;
  915. }
  916. if (codec->priv_class) {
  917. *(const AVClass **)avctx->priv_data = codec->priv_class;
  918. av_opt_set_defaults(avctx->priv_data);
  919. }
  920. }
  921. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  922. goto free_and_end;
  923. } else {
  924. avctx->priv_data = NULL;
  925. }
  926. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  927. goto free_and_end;
  928. if (avctx->coded_width && avctx->coded_height && !avctx->width && !avctx->height)
  929. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  930. else if (avctx->width && avctx->height)
  931. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  932. if (ret < 0)
  933. goto free_and_end;
  934. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  935. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  936. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  937. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  938. ff_set_dimensions(avctx, 0, 0);
  939. }
  940. if (avctx->width > 0 && avctx->height > 0) {
  941. if (av_image_check_sar(avctx->width, avctx->height,
  942. avctx->sample_aspect_ratio) < 0) {
  943. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  944. avctx->sample_aspect_ratio.num,
  945. avctx->sample_aspect_ratio.den);
  946. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  947. }
  948. }
  949. /* if the decoder init function was already called previously,
  950. * free the already allocated subtitle_header before overwriting it */
  951. if (av_codec_is_decoder(codec))
  952. av_freep(&avctx->subtitle_header);
  953. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  954. ret = AVERROR(EINVAL);
  955. goto free_and_end;
  956. }
  957. avctx->codec = codec;
  958. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  959. avctx->codec_id == AV_CODEC_ID_NONE) {
  960. avctx->codec_type = codec->type;
  961. avctx->codec_id = codec->id;
  962. }
  963. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  964. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  965. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  966. ret = AVERROR(EINVAL);
  967. goto free_and_end;
  968. }
  969. avctx->frame_number = 0;
  970. if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
  971. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  972. ret = AVERROR_EXPERIMENTAL;
  973. goto free_and_end;
  974. }
  975. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  976. (!avctx->time_base.num || !avctx->time_base.den)) {
  977. avctx->time_base.num = 1;
  978. avctx->time_base.den = avctx->sample_rate;
  979. }
  980. if (HAVE_THREADS) {
  981. ret = ff_thread_init(avctx);
  982. if (ret < 0) {
  983. goto free_and_end;
  984. }
  985. }
  986. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  987. avctx->thread_count = 1;
  988. if (av_codec_is_encoder(avctx->codec)) {
  989. int i;
  990. if (avctx->codec->sample_fmts) {
  991. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  992. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  993. break;
  994. if (avctx->channels == 1 &&
  995. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  996. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  997. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  998. break;
  999. }
  1000. }
  1001. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  1002. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  1003. ret = AVERROR(EINVAL);
  1004. goto free_and_end;
  1005. }
  1006. }
  1007. if (avctx->codec->pix_fmts) {
  1008. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  1009. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  1010. break;
  1011. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
  1012. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  1013. ret = AVERROR(EINVAL);
  1014. goto free_and_end;
  1015. }
  1016. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
  1017. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
  1018. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
  1019. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
  1020. avctx->color_range = AVCOL_RANGE_JPEG;
  1021. }
  1022. if (avctx->codec->supported_samplerates) {
  1023. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  1024. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  1025. break;
  1026. if (avctx->codec->supported_samplerates[i] == 0) {
  1027. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  1028. ret = AVERROR(EINVAL);
  1029. goto free_and_end;
  1030. }
  1031. }
  1032. if (avctx->codec->channel_layouts) {
  1033. if (!avctx->channel_layout) {
  1034. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  1035. } else {
  1036. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  1037. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  1038. break;
  1039. if (avctx->codec->channel_layouts[i] == 0) {
  1040. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  1041. ret = AVERROR(EINVAL);
  1042. goto free_and_end;
  1043. }
  1044. }
  1045. }
  1046. if (avctx->channel_layout && avctx->channels) {
  1047. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  1048. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  1049. ret = AVERROR(EINVAL);
  1050. goto free_and_end;
  1051. }
  1052. } else if (avctx->channel_layout) {
  1053. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1054. }
  1055. if (!avctx->rc_initial_buffer_occupancy)
  1056. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  1057. }
  1058. if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
  1059. ret = avctx->codec->init(avctx);
  1060. if (ret < 0) {
  1061. goto free_and_end;
  1062. }
  1063. }
  1064. #if FF_API_AUDIOENC_DELAY
  1065. if (av_codec_is_encoder(avctx->codec))
  1066. avctx->delay = avctx->initial_padding;
  1067. #endif
  1068. if (av_codec_is_decoder(avctx->codec)) {
  1069. /* validate channel layout from the decoder */
  1070. if (avctx->channel_layout) {
  1071. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1072. if (!avctx->channels)
  1073. avctx->channels = channels;
  1074. else if (channels != avctx->channels) {
  1075. av_log(avctx, AV_LOG_WARNING,
  1076. "channel layout does not match number of channels\n");
  1077. avctx->channel_layout = 0;
  1078. }
  1079. }
  1080. if (avctx->channels && avctx->channels < 0 ||
  1081. avctx->channels > FF_SANE_NB_CHANNELS) {
  1082. ret = AVERROR(EINVAL);
  1083. goto free_and_end;
  1084. }
  1085. #if FF_API_AVCTX_TIMEBASE
  1086. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  1087. avctx->time_base = av_inv_q(avctx->framerate);
  1088. #endif
  1089. }
  1090. end:
  1091. entangled_thread_counter--;
  1092. /* Release any user-supplied mutex. */
  1093. if (lockmgr_cb) {
  1094. (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1095. }
  1096. if (options) {
  1097. av_dict_free(options);
  1098. *options = tmp;
  1099. }
  1100. return ret;
  1101. free_and_end:
  1102. av_dict_free(&tmp);
  1103. av_freep(&avctx->priv_data);
  1104. if (avctx->internal) {
  1105. av_frame_free(&avctx->internal->to_free);
  1106. av_freep(&avctx->internal->pool);
  1107. }
  1108. av_freep(&avctx->internal);
  1109. avctx->codec = NULL;
  1110. goto end;
  1111. }
  1112. int ff_alloc_packet(AVPacket *avpkt, int size)
  1113. {
  1114. if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  1115. return AVERROR(EINVAL);
  1116. if (avpkt->data) {
  1117. AVBufferRef *buf = avpkt->buf;
  1118. #if FF_API_DESTRUCT_PACKET
  1119. FF_DISABLE_DEPRECATION_WARNINGS
  1120. void *destruct = avpkt->destruct;
  1121. FF_ENABLE_DEPRECATION_WARNINGS
  1122. #endif
  1123. if (avpkt->size < size)
  1124. return AVERROR(EINVAL);
  1125. av_init_packet(avpkt);
  1126. #if FF_API_DESTRUCT_PACKET
  1127. FF_DISABLE_DEPRECATION_WARNINGS
  1128. avpkt->destruct = destruct;
  1129. FF_ENABLE_DEPRECATION_WARNINGS
  1130. #endif
  1131. avpkt->buf = buf;
  1132. avpkt->size = size;
  1133. return 0;
  1134. } else {
  1135. return av_new_packet(avpkt, size);
  1136. }
  1137. }
  1138. /**
  1139. * Pad last frame with silence.
  1140. */
  1141. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1142. {
  1143. AVFrame *frame = NULL;
  1144. int ret;
  1145. if (!(frame = av_frame_alloc()))
  1146. return AVERROR(ENOMEM);
  1147. frame->format = src->format;
  1148. frame->channel_layout = src->channel_layout;
  1149. frame->nb_samples = s->frame_size;
  1150. ret = av_frame_get_buffer(frame, 32);
  1151. if (ret < 0)
  1152. goto fail;
  1153. ret = av_frame_copy_props(frame, src);
  1154. if (ret < 0)
  1155. goto fail;
  1156. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1157. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1158. goto fail;
  1159. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1160. frame->nb_samples - src->nb_samples,
  1161. s->channels, s->sample_fmt)) < 0)
  1162. goto fail;
  1163. *dst = frame;
  1164. return 0;
  1165. fail:
  1166. av_frame_free(&frame);
  1167. return ret;
  1168. }
  1169. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1170. AVPacket *avpkt,
  1171. const AVFrame *frame,
  1172. int *got_packet_ptr)
  1173. {
  1174. AVFrame tmp;
  1175. AVFrame *padded_frame = NULL;
  1176. int ret;
  1177. int user_packet = !!avpkt->data;
  1178. *got_packet_ptr = 0;
  1179. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1180. av_free_packet(avpkt);
  1181. av_init_packet(avpkt);
  1182. return 0;
  1183. }
  1184. /* ensure that extended_data is properly set */
  1185. if (frame && !frame->extended_data) {
  1186. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1187. avctx->channels > AV_NUM_DATA_POINTERS) {
  1188. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1189. "with more than %d channels, but extended_data is not set.\n",
  1190. AV_NUM_DATA_POINTERS);
  1191. return AVERROR(EINVAL);
  1192. }
  1193. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1194. tmp = *frame;
  1195. tmp.extended_data = tmp.data;
  1196. frame = &tmp;
  1197. }
  1198. /* check for valid frame size */
  1199. if (frame) {
  1200. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1201. if (frame->nb_samples > avctx->frame_size)
  1202. return AVERROR(EINVAL);
  1203. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1204. if (frame->nb_samples < avctx->frame_size &&
  1205. !avctx->internal->last_audio_frame) {
  1206. ret = pad_last_frame(avctx, &padded_frame, frame);
  1207. if (ret < 0)
  1208. return ret;
  1209. frame = padded_frame;
  1210. avctx->internal->last_audio_frame = 1;
  1211. }
  1212. if (frame->nb_samples != avctx->frame_size) {
  1213. ret = AVERROR(EINVAL);
  1214. goto end;
  1215. }
  1216. }
  1217. }
  1218. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1219. if (!ret) {
  1220. if (*got_packet_ptr) {
  1221. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1222. if (avpkt->pts == AV_NOPTS_VALUE)
  1223. avpkt->pts = frame->pts;
  1224. if (!avpkt->duration)
  1225. avpkt->duration = ff_samples_to_time_base(avctx,
  1226. frame->nb_samples);
  1227. }
  1228. avpkt->dts = avpkt->pts;
  1229. } else {
  1230. avpkt->size = 0;
  1231. }
  1232. if (!user_packet && avpkt->size) {
  1233. ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
  1234. if (ret >= 0)
  1235. avpkt->data = avpkt->buf->data;
  1236. }
  1237. avctx->frame_number++;
  1238. }
  1239. if (ret < 0 || !*got_packet_ptr) {
  1240. av_free_packet(avpkt);
  1241. av_init_packet(avpkt);
  1242. goto end;
  1243. }
  1244. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1245. * this needs to be moved to the encoders, but for now we can do it
  1246. * here to simplify things */
  1247. avpkt->flags |= AV_PKT_FLAG_KEY;
  1248. end:
  1249. av_frame_free(&padded_frame);
  1250. #if FF_API_AUDIOENC_DELAY
  1251. avctx->delay = avctx->initial_padding;
  1252. #endif
  1253. return ret;
  1254. }
  1255. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1256. AVPacket *avpkt,
  1257. const AVFrame *frame,
  1258. int *got_packet_ptr)
  1259. {
  1260. int ret;
  1261. int user_packet = !!avpkt->data;
  1262. *got_packet_ptr = 0;
  1263. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1264. av_free_packet(avpkt);
  1265. av_init_packet(avpkt);
  1266. avpkt->size = 0;
  1267. return 0;
  1268. }
  1269. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1270. return AVERROR(EINVAL);
  1271. av_assert0(avctx->codec->encode2);
  1272. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1273. if (!ret) {
  1274. if (!*got_packet_ptr)
  1275. avpkt->size = 0;
  1276. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1277. avpkt->pts = avpkt->dts = frame->pts;
  1278. if (!user_packet && avpkt->size) {
  1279. ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
  1280. if (ret >= 0)
  1281. avpkt->data = avpkt->buf->data;
  1282. }
  1283. avctx->frame_number++;
  1284. }
  1285. if (ret < 0 || !*got_packet_ptr)
  1286. av_free_packet(avpkt);
  1287. emms_c();
  1288. return ret;
  1289. }
  1290. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1291. const AVSubtitle *sub)
  1292. {
  1293. int ret;
  1294. if (sub->start_display_time) {
  1295. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1296. return -1;
  1297. }
  1298. if (sub->num_rects == 0 || !sub->rects)
  1299. return -1;
  1300. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1301. avctx->frame_number++;
  1302. return ret;
  1303. }
  1304. static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1305. {
  1306. int size = 0, ret;
  1307. const uint8_t *data;
  1308. uint32_t flags;
  1309. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1310. if (!data)
  1311. return 0;
  1312. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE)) {
  1313. av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
  1314. "changes, but PARAM_CHANGE side data was sent to it.\n");
  1315. return AVERROR(EINVAL);
  1316. }
  1317. if (size < 4)
  1318. goto fail;
  1319. flags = bytestream_get_le32(&data);
  1320. size -= 4;
  1321. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1322. if (size < 4)
  1323. goto fail;
  1324. avctx->channels = bytestream_get_le32(&data);
  1325. size -= 4;
  1326. }
  1327. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1328. if (size < 8)
  1329. goto fail;
  1330. avctx->channel_layout = bytestream_get_le64(&data);
  1331. size -= 8;
  1332. }
  1333. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1334. if (size < 4)
  1335. goto fail;
  1336. avctx->sample_rate = bytestream_get_le32(&data);
  1337. size -= 4;
  1338. }
  1339. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1340. if (size < 8)
  1341. goto fail;
  1342. avctx->width = bytestream_get_le32(&data);
  1343. avctx->height = bytestream_get_le32(&data);
  1344. size -= 8;
  1345. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1346. if (ret < 0)
  1347. return ret;
  1348. }
  1349. return 0;
  1350. fail:
  1351. av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
  1352. return AVERROR_INVALIDDATA;
  1353. }
  1354. static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
  1355. {
  1356. int ret;
  1357. /* move the original frame to our backup */
  1358. av_frame_unref(avci->to_free);
  1359. av_frame_move_ref(avci->to_free, frame);
  1360. /* now copy everything except the AVBufferRefs back
  1361. * note that we make a COPY of the side data, so calling av_frame_free() on
  1362. * the caller's frame will work properly */
  1363. ret = av_frame_copy_props(frame, avci->to_free);
  1364. if (ret < 0)
  1365. return ret;
  1366. memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
  1367. memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
  1368. if (avci->to_free->extended_data != avci->to_free->data) {
  1369. int planes = av_get_channel_layout_nb_channels(avci->to_free->channel_layout);
  1370. int size = planes * sizeof(*frame->extended_data);
  1371. if (!size) {
  1372. av_frame_unref(frame);
  1373. return AVERROR_BUG;
  1374. }
  1375. frame->extended_data = av_malloc(size);
  1376. if (!frame->extended_data) {
  1377. av_frame_unref(frame);
  1378. return AVERROR(ENOMEM);
  1379. }
  1380. memcpy(frame->extended_data, avci->to_free->extended_data,
  1381. size);
  1382. } else
  1383. frame->extended_data = frame->data;
  1384. frame->format = avci->to_free->format;
  1385. frame->width = avci->to_free->width;
  1386. frame->height = avci->to_free->height;
  1387. frame->channel_layout = avci->to_free->channel_layout;
  1388. frame->nb_samples = avci->to_free->nb_samples;
  1389. return 0;
  1390. }
  1391. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1392. int *got_picture_ptr,
  1393. AVPacket *avpkt)
  1394. {
  1395. AVCodecInternal *avci = avctx->internal;
  1396. int ret;
  1397. *got_picture_ptr = 0;
  1398. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1399. return -1;
  1400. avctx->internal->pkt = avpkt;
  1401. ret = apply_param_change(avctx, avpkt);
  1402. if (ret < 0) {
  1403. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1404. if (avctx->err_recognition & AV_EF_EXPLODE)
  1405. return ret;
  1406. }
  1407. av_frame_unref(picture);
  1408. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1409. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1410. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1411. avpkt);
  1412. else {
  1413. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1414. avpkt);
  1415. picture->pkt_dts = avpkt->dts;
  1416. /* get_buffer is supposed to set frame parameters */
  1417. if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
  1418. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1419. picture->width = avctx->width;
  1420. picture->height = avctx->height;
  1421. picture->format = avctx->pix_fmt;
  1422. }
  1423. }
  1424. emms_c(); //needed to avoid an emms_c() call before every return;
  1425. if (*got_picture_ptr) {
  1426. if (!avctx->refcounted_frames) {
  1427. int err = unrefcount_frame(avci, picture);
  1428. if (err < 0)
  1429. return err;
  1430. }
  1431. avctx->frame_number++;
  1432. } else
  1433. av_frame_unref(picture);
  1434. } else
  1435. ret = 0;
  1436. #if FF_API_AVCTX_TIMEBASE
  1437. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  1438. avctx->time_base = av_inv_q(avctx->framerate);
  1439. #endif
  1440. return ret;
  1441. }
  1442. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1443. AVFrame *frame,
  1444. int *got_frame_ptr,
  1445. AVPacket *avpkt)
  1446. {
  1447. AVCodecInternal *avci = avctx->internal;
  1448. int ret = 0;
  1449. *got_frame_ptr = 0;
  1450. avctx->internal->pkt = avpkt;
  1451. if (!avpkt->data && avpkt->size) {
  1452. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1453. return AVERROR(EINVAL);
  1454. }
  1455. ret = apply_param_change(avctx, avpkt);
  1456. if (ret < 0) {
  1457. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1458. if (avctx->err_recognition & AV_EF_EXPLODE)
  1459. return ret;
  1460. }
  1461. av_frame_unref(frame);
  1462. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1463. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1464. if (ret >= 0 && *got_frame_ptr) {
  1465. avctx->frame_number++;
  1466. frame->pkt_dts = avpkt->dts;
  1467. if (frame->format == AV_SAMPLE_FMT_NONE)
  1468. frame->format = avctx->sample_fmt;
  1469. if (!avctx->refcounted_frames) {
  1470. int err = unrefcount_frame(avci, frame);
  1471. if (err < 0)
  1472. return err;
  1473. }
  1474. } else
  1475. av_frame_unref(frame);
  1476. }
  1477. return ret;
  1478. }
  1479. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1480. int *got_sub_ptr,
  1481. AVPacket *avpkt)
  1482. {
  1483. int ret;
  1484. avctx->internal->pkt = avpkt;
  1485. *got_sub_ptr = 0;
  1486. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1487. if (*got_sub_ptr)
  1488. avctx->frame_number++;
  1489. return ret;
  1490. }
  1491. void avsubtitle_free(AVSubtitle *sub)
  1492. {
  1493. int i;
  1494. for (i = 0; i < sub->num_rects; i++) {
  1495. av_freep(&sub->rects[i]->pict.data[0]);
  1496. av_freep(&sub->rects[i]->pict.data[1]);
  1497. av_freep(&sub->rects[i]->pict.data[2]);
  1498. av_freep(&sub->rects[i]->pict.data[3]);
  1499. av_freep(&sub->rects[i]->text);
  1500. av_freep(&sub->rects[i]->ass);
  1501. av_freep(&sub->rects[i]);
  1502. }
  1503. av_freep(&sub->rects);
  1504. memset(sub, 0, sizeof(AVSubtitle));
  1505. }
  1506. av_cold int avcodec_close(AVCodecContext *avctx)
  1507. {
  1508. if (avcodec_is_open(avctx)) {
  1509. FramePool *pool = avctx->internal->pool;
  1510. int i;
  1511. if (HAVE_THREADS && avctx->internal->thread_ctx)
  1512. ff_thread_free(avctx);
  1513. if (avctx->codec && avctx->codec->close)
  1514. avctx->codec->close(avctx);
  1515. avctx->coded_frame = NULL;
  1516. av_frame_free(&avctx->internal->to_free);
  1517. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  1518. av_buffer_pool_uninit(&pool->pools[i]);
  1519. av_freep(&avctx->internal->pool);
  1520. if (avctx->hwaccel && avctx->hwaccel->uninit)
  1521. avctx->hwaccel->uninit(avctx);
  1522. av_freep(&avctx->internal->hwaccel_priv_data);
  1523. av_freep(&avctx->internal);
  1524. }
  1525. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1526. av_opt_free(avctx->priv_data);
  1527. av_opt_free(avctx);
  1528. av_freep(&avctx->priv_data);
  1529. if (av_codec_is_encoder(avctx->codec))
  1530. av_freep(&avctx->extradata);
  1531. avctx->codec = NULL;
  1532. avctx->active_thread_type = 0;
  1533. return 0;
  1534. }
  1535. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  1536. {
  1537. AVCodec *p, *experimental = NULL;
  1538. p = first_avcodec;
  1539. while (p) {
  1540. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  1541. p->id == id) {
  1542. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1543. experimental = p;
  1544. } else
  1545. return p;
  1546. }
  1547. p = p->next;
  1548. }
  1549. return experimental;
  1550. }
  1551. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1552. {
  1553. return find_encdec(id, 1);
  1554. }
  1555. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1556. {
  1557. AVCodec *p;
  1558. if (!name)
  1559. return NULL;
  1560. p = first_avcodec;
  1561. while (p) {
  1562. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1563. return p;
  1564. p = p->next;
  1565. }
  1566. return NULL;
  1567. }
  1568. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1569. {
  1570. return find_encdec(id, 0);
  1571. }
  1572. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1573. {
  1574. AVCodec *p;
  1575. if (!name)
  1576. return NULL;
  1577. p = first_avcodec;
  1578. while (p) {
  1579. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1580. return p;
  1581. p = p->next;
  1582. }
  1583. return NULL;
  1584. }
  1585. static int get_bit_rate(AVCodecContext *ctx)
  1586. {
  1587. int bit_rate;
  1588. int bits_per_sample;
  1589. switch (ctx->codec_type) {
  1590. case AVMEDIA_TYPE_VIDEO:
  1591. case AVMEDIA_TYPE_DATA:
  1592. case AVMEDIA_TYPE_SUBTITLE:
  1593. case AVMEDIA_TYPE_ATTACHMENT:
  1594. bit_rate = ctx->bit_rate;
  1595. break;
  1596. case AVMEDIA_TYPE_AUDIO:
  1597. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1598. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1599. break;
  1600. default:
  1601. bit_rate = 0;
  1602. break;
  1603. }
  1604. return bit_rate;
  1605. }
  1606. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1607. {
  1608. int i, len, ret = 0;
  1609. #define TAG_PRINT(x) \
  1610. (((x) >= '0' && (x) <= '9') || \
  1611. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1612. ((x) == '.' || (x) == ' '))
  1613. for (i = 0; i < 4; i++) {
  1614. len = snprintf(buf, buf_size,
  1615. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  1616. buf += len;
  1617. buf_size = buf_size > len ? buf_size - len : 0;
  1618. ret += len;
  1619. codec_tag >>= 8;
  1620. }
  1621. return ret;
  1622. }
  1623. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1624. {
  1625. const char *codec_name;
  1626. const char *profile = NULL;
  1627. const AVCodec *p;
  1628. char buf1[32];
  1629. int bitrate;
  1630. int new_line = 0;
  1631. AVRational display_aspect_ratio;
  1632. if (enc->codec)
  1633. p = enc->codec;
  1634. else if (encode)
  1635. p = avcodec_find_encoder(enc->codec_id);
  1636. else
  1637. p = avcodec_find_decoder(enc->codec_id);
  1638. if (p) {
  1639. codec_name = p->name;
  1640. profile = av_get_profile_name(p, enc->profile);
  1641. } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
  1642. /* fake mpeg2 transport stream codec (currently not
  1643. * registered) */
  1644. codec_name = "mpeg2ts";
  1645. } else {
  1646. /* output avi tags */
  1647. char tag_buf[32];
  1648. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1649. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  1650. codec_name = buf1;
  1651. }
  1652. switch (enc->codec_type) {
  1653. case AVMEDIA_TYPE_VIDEO:
  1654. snprintf(buf, buf_size,
  1655. "Video: %s%s",
  1656. codec_name, enc->mb_decision ? " (hq)" : "");
  1657. if (profile)
  1658. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1659. " (%s)", profile);
  1660. if (enc->codec_tag) {
  1661. char tag_buf[32];
  1662. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1663. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1664. " [%s / 0x%04X]", tag_buf, enc->codec_tag);
  1665. }
  1666. av_strlcat(buf, "\n ", buf_size);
  1667. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1668. "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
  1669. av_get_pix_fmt_name(enc->pix_fmt));
  1670. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  1671. snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
  1672. av_color_range_name(enc->color_range));
  1673. if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
  1674. enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  1675. enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
  1676. new_line = 1;
  1677. snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s/%s/%s",
  1678. av_color_space_name(enc->colorspace),
  1679. av_color_primaries_name(enc->color_primaries),
  1680. av_color_transfer_name(enc->color_trc));
  1681. }
  1682. if (av_log_get_level() >= AV_LOG_DEBUG &&
  1683. enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
  1684. snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
  1685. av_chroma_location_name(enc->chroma_sample_location));
  1686. if (enc->width) {
  1687. av_strlcat(buf, new_line ? "\n " : ", ", buf_size);
  1688. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1689. "%dx%d",
  1690. enc->width, enc->height);
  1691. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  1692. (enc->width != enc->coded_width ||
  1693. enc->height != enc->coded_height))
  1694. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1695. " (%dx%d)", enc->coded_width, enc->coded_height);
  1696. if (enc->sample_aspect_ratio.num) {
  1697. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1698. enc->width * enc->sample_aspect_ratio.num,
  1699. enc->height * enc->sample_aspect_ratio.den,
  1700. 1024 * 1024);
  1701. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1702. " [PAR %d:%d DAR %d:%d]",
  1703. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1704. display_aspect_ratio.num, display_aspect_ratio.den);
  1705. }
  1706. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1707. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1708. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1709. ", %d/%d",
  1710. enc->time_base.num / g, enc->time_base.den / g);
  1711. }
  1712. }
  1713. if (encode) {
  1714. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1715. ", q=%d-%d", enc->qmin, enc->qmax);
  1716. }
  1717. break;
  1718. case AVMEDIA_TYPE_AUDIO:
  1719. snprintf(buf, buf_size,
  1720. "Audio: %s",
  1721. codec_name);
  1722. if (profile)
  1723. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1724. " (%s)", profile);
  1725. if (enc->codec_tag) {
  1726. char tag_buf[32];
  1727. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1728. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1729. " [%s / 0x%04X]", tag_buf, enc->codec_tag);
  1730. }
  1731. av_strlcat(buf, "\n ", buf_size);
  1732. if (enc->sample_rate) {
  1733. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1734. "%d Hz, ", enc->sample_rate);
  1735. }
  1736. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1737. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1738. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1739. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1740. }
  1741. break;
  1742. case AVMEDIA_TYPE_DATA:
  1743. snprintf(buf, buf_size, "Data: %s", codec_name);
  1744. break;
  1745. case AVMEDIA_TYPE_SUBTITLE:
  1746. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1747. break;
  1748. case AVMEDIA_TYPE_ATTACHMENT:
  1749. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1750. break;
  1751. default:
  1752. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1753. return;
  1754. }
  1755. if (encode) {
  1756. if (enc->flags & CODEC_FLAG_PASS1)
  1757. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1758. ", pass 1");
  1759. if (enc->flags & CODEC_FLAG_PASS2)
  1760. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1761. ", pass 2");
  1762. }
  1763. bitrate = get_bit_rate(enc);
  1764. if (bitrate != 0) {
  1765. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1766. ", %d kb/s", bitrate / 1000);
  1767. }
  1768. }
  1769. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1770. {
  1771. const AVProfile *p;
  1772. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1773. return NULL;
  1774. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1775. if (p->profile == profile)
  1776. return p->name;
  1777. return NULL;
  1778. }
  1779. unsigned avcodec_version(void)
  1780. {
  1781. return LIBAVCODEC_VERSION_INT;
  1782. }
  1783. const char *avcodec_configuration(void)
  1784. {
  1785. return LIBAV_CONFIGURATION;
  1786. }
  1787. const char *avcodec_license(void)
  1788. {
  1789. #define LICENSE_PREFIX "libavcodec license: "
  1790. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1791. }
  1792. void avcodec_flush_buffers(AVCodecContext *avctx)
  1793. {
  1794. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1795. ff_thread_flush(avctx);
  1796. else if (avctx->codec->flush)
  1797. avctx->codec->flush(avctx);
  1798. if (!avctx->refcounted_frames)
  1799. av_frame_unref(avctx->internal->to_free);
  1800. }
  1801. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1802. {
  1803. switch (codec_id) {
  1804. case AV_CODEC_ID_ADPCM_CT:
  1805. case AV_CODEC_ID_ADPCM_IMA_APC:
  1806. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1807. case AV_CODEC_ID_ADPCM_IMA_WS:
  1808. case AV_CODEC_ID_ADPCM_G722:
  1809. case AV_CODEC_ID_ADPCM_YAMAHA:
  1810. return 4;
  1811. case AV_CODEC_ID_PCM_ALAW:
  1812. case AV_CODEC_ID_PCM_MULAW:
  1813. case AV_CODEC_ID_PCM_S8:
  1814. case AV_CODEC_ID_PCM_U8:
  1815. case AV_CODEC_ID_PCM_ZORK:
  1816. return 8;
  1817. case AV_CODEC_ID_PCM_S16BE:
  1818. case AV_CODEC_ID_PCM_S16LE:
  1819. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1820. case AV_CODEC_ID_PCM_U16BE:
  1821. case AV_CODEC_ID_PCM_U16LE:
  1822. return 16;
  1823. case AV_CODEC_ID_PCM_S24DAUD:
  1824. case AV_CODEC_ID_PCM_S24BE:
  1825. case AV_CODEC_ID_PCM_S24LE:
  1826. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  1827. case AV_CODEC_ID_PCM_U24BE:
  1828. case AV_CODEC_ID_PCM_U24LE:
  1829. return 24;
  1830. case AV_CODEC_ID_PCM_S32BE:
  1831. case AV_CODEC_ID_PCM_S32LE:
  1832. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  1833. case AV_CODEC_ID_PCM_U32BE:
  1834. case AV_CODEC_ID_PCM_U32LE:
  1835. case AV_CODEC_ID_PCM_F32BE:
  1836. case AV_CODEC_ID_PCM_F32LE:
  1837. return 32;
  1838. case AV_CODEC_ID_PCM_F64BE:
  1839. case AV_CODEC_ID_PCM_F64LE:
  1840. return 64;
  1841. default:
  1842. return 0;
  1843. }
  1844. }
  1845. int av_get_bits_per_sample(enum AVCodecID codec_id)
  1846. {
  1847. switch (codec_id) {
  1848. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1849. return 2;
  1850. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1851. return 3;
  1852. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1853. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1854. case AV_CODEC_ID_ADPCM_IMA_QT:
  1855. case AV_CODEC_ID_ADPCM_SWF:
  1856. case AV_CODEC_ID_ADPCM_MS:
  1857. return 4;
  1858. default:
  1859. return av_get_exact_bits_per_sample(codec_id);
  1860. }
  1861. }
  1862. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1863. {
  1864. int id, sr, ch, ba, tag, bps;
  1865. id = avctx->codec_id;
  1866. sr = avctx->sample_rate;
  1867. ch = avctx->channels;
  1868. ba = avctx->block_align;
  1869. tag = avctx->codec_tag;
  1870. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1871. /* codecs with an exact constant bits per sample */
  1872. if (bps > 0 && ch > 0 && frame_bytes > 0)
  1873. return (frame_bytes * 8) / (bps * ch);
  1874. bps = avctx->bits_per_coded_sample;
  1875. /* codecs with a fixed packet duration */
  1876. switch (id) {
  1877. case AV_CODEC_ID_ADPCM_ADX: return 32;
  1878. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  1879. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  1880. case AV_CODEC_ID_AMR_NB:
  1881. case AV_CODEC_ID_GSM:
  1882. case AV_CODEC_ID_QCELP:
  1883. case AV_CODEC_ID_RA_144:
  1884. case AV_CODEC_ID_RA_288: return 160;
  1885. case AV_CODEC_ID_IMC: return 256;
  1886. case AV_CODEC_ID_AMR_WB:
  1887. case AV_CODEC_ID_GSM_MS: return 320;
  1888. case AV_CODEC_ID_MP1: return 384;
  1889. case AV_CODEC_ID_ATRAC1: return 512;
  1890. case AV_CODEC_ID_ATRAC3: return 1024;
  1891. case AV_CODEC_ID_MP2:
  1892. case AV_CODEC_ID_MUSEPACK7: return 1152;
  1893. case AV_CODEC_ID_AC3: return 1536;
  1894. }
  1895. if (sr > 0) {
  1896. /* calc from sample rate */
  1897. if (id == AV_CODEC_ID_TTA)
  1898. return 256 * sr / 245;
  1899. if (ch > 0) {
  1900. /* calc from sample rate and channels */
  1901. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  1902. return (480 << (sr / 22050)) / ch;
  1903. }
  1904. }
  1905. if (ba > 0) {
  1906. /* calc from block_align */
  1907. if (id == AV_CODEC_ID_SIPR) {
  1908. switch (ba) {
  1909. case 20: return 160;
  1910. case 19: return 144;
  1911. case 29: return 288;
  1912. case 37: return 480;
  1913. }
  1914. } else if (id == AV_CODEC_ID_ILBC) {
  1915. switch (ba) {
  1916. case 38: return 160;
  1917. case 50: return 240;
  1918. }
  1919. }
  1920. }
  1921. if (frame_bytes > 0) {
  1922. /* calc from frame_bytes only */
  1923. if (id == AV_CODEC_ID_TRUESPEECH)
  1924. return 240 * (frame_bytes / 32);
  1925. if (id == AV_CODEC_ID_NELLYMOSER)
  1926. return 256 * (frame_bytes / 64);
  1927. if (bps > 0) {
  1928. /* calc from frame_bytes and bits_per_coded_sample */
  1929. if (id == AV_CODEC_ID_ADPCM_G726)
  1930. return frame_bytes * 8 / bps;
  1931. }
  1932. if (ch > 0) {
  1933. /* calc from frame_bytes and channels */
  1934. switch (id) {
  1935. case AV_CODEC_ID_ADPCM_4XM:
  1936. case AV_CODEC_ID_ADPCM_IMA_ISS:
  1937. return (frame_bytes - 4 * ch) * 2 / ch;
  1938. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1939. return (frame_bytes - 4) * 2 / ch;
  1940. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1941. return (frame_bytes - 8) * 2 / ch;
  1942. case AV_CODEC_ID_ADPCM_XA:
  1943. return (frame_bytes / 128) * 224 / ch;
  1944. case AV_CODEC_ID_INTERPLAY_DPCM:
  1945. return (frame_bytes - 6 - ch) / ch;
  1946. case AV_CODEC_ID_ROQ_DPCM:
  1947. return (frame_bytes - 8) / ch;
  1948. case AV_CODEC_ID_XAN_DPCM:
  1949. return (frame_bytes - 2 * ch) / ch;
  1950. case AV_CODEC_ID_MACE3:
  1951. return 3 * frame_bytes / ch;
  1952. case AV_CODEC_ID_MACE6:
  1953. return 6 * frame_bytes / ch;
  1954. case AV_CODEC_ID_PCM_LXF:
  1955. return 2 * (frame_bytes / (5 * ch));
  1956. }
  1957. if (tag) {
  1958. /* calc from frame_bytes, channels, and codec_tag */
  1959. if (id == AV_CODEC_ID_SOL_DPCM) {
  1960. if (tag == 3)
  1961. return frame_bytes / ch;
  1962. else
  1963. return frame_bytes * 2 / ch;
  1964. }
  1965. }
  1966. if (ba > 0) {
  1967. /* calc from frame_bytes, channels, and block_align */
  1968. int blocks = frame_bytes / ba;
  1969. switch (avctx->codec_id) {
  1970. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1971. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1972. case AV_CODEC_ID_ADPCM_IMA_DK3:
  1973. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1974. case AV_CODEC_ID_ADPCM_IMA_DK4:
  1975. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1976. case AV_CODEC_ID_ADPCM_MS:
  1977. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1978. }
  1979. }
  1980. if (bps > 0) {
  1981. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1982. switch (avctx->codec_id) {
  1983. case AV_CODEC_ID_PCM_DVD:
  1984. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1985. case AV_CODEC_ID_PCM_BLURAY:
  1986. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  1987. case AV_CODEC_ID_S302M:
  1988. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1989. }
  1990. }
  1991. }
  1992. }
  1993. return 0;
  1994. }
  1995. #if !HAVE_THREADS
  1996. int ff_thread_init(AVCodecContext *s)
  1997. {
  1998. return -1;
  1999. }
  2000. #endif
  2001. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2002. {
  2003. unsigned int n = 0;
  2004. while (v >= 0xff) {
  2005. *s++ = 0xff;
  2006. v -= 0xff;
  2007. n++;
  2008. }
  2009. *s = v;
  2010. n++;
  2011. return n;
  2012. }
  2013. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2014. {
  2015. int i;
  2016. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2017. return i;
  2018. }
  2019. #if FF_API_MISSING_SAMPLE
  2020. FF_DISABLE_DEPRECATION_WARNINGS
  2021. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2022. {
  2023. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
  2024. "version to the newest one from Git. If the problem still "
  2025. "occurs, it means that your file has a feature which has not "
  2026. "been implemented.\n", feature);
  2027. if(want_sample)
  2028. av_log_ask_for_sample(avc, NULL);
  2029. }
  2030. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2031. {
  2032. va_list argument_list;
  2033. va_start(argument_list, msg);
  2034. if (msg)
  2035. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2036. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2037. "of this file to ftp://upload.libav.org/incoming/ "
  2038. "and contact the libav-devel mailing list.\n");
  2039. va_end(argument_list);
  2040. }
  2041. FF_ENABLE_DEPRECATION_WARNINGS
  2042. #endif /* FF_API_MISSING_SAMPLE */
  2043. static AVHWAccel *first_hwaccel = NULL;
  2044. void av_register_hwaccel(AVHWAccel *hwaccel)
  2045. {
  2046. AVHWAccel **p = &first_hwaccel;
  2047. while (*p)
  2048. p = &(*p)->next;
  2049. *p = hwaccel;
  2050. hwaccel->next = NULL;
  2051. }
  2052. AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
  2053. {
  2054. return hwaccel ? hwaccel->next : first_hwaccel;
  2055. }
  2056. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2057. {
  2058. if (lockmgr_cb) {
  2059. // There is no good way to rollback a failure to destroy the
  2060. // mutex, so we ignore failures.
  2061. lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
  2062. lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
  2063. lockmgr_cb = NULL;
  2064. codec_mutex = NULL;
  2065. avformat_mutex = NULL;
  2066. }
  2067. if (cb) {
  2068. void *new_codec_mutex = NULL;
  2069. void *new_avformat_mutex = NULL;
  2070. int err;
  2071. if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
  2072. return err > 0 ? AVERROR_UNKNOWN : err;
  2073. }
  2074. if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
  2075. // Ignore failures to destroy the newly created mutex.
  2076. cb(&new_codec_mutex, AV_LOCK_DESTROY);
  2077. return err > 0 ? AVERROR_UNKNOWN : err;
  2078. }
  2079. lockmgr_cb = cb;
  2080. codec_mutex = new_codec_mutex;
  2081. avformat_mutex = new_avformat_mutex;
  2082. }
  2083. return 0;
  2084. }
  2085. int avpriv_lock_avformat(void)
  2086. {
  2087. if (lockmgr_cb) {
  2088. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2089. return -1;
  2090. }
  2091. return 0;
  2092. }
  2093. int avpriv_unlock_avformat(void)
  2094. {
  2095. if (lockmgr_cb) {
  2096. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2097. return -1;
  2098. }
  2099. return 0;
  2100. }
  2101. unsigned int avpriv_toupper4(unsigned int x)
  2102. {
  2103. return av_toupper(x & 0xFF) +
  2104. (av_toupper((x >> 8) & 0xFF) << 8) +
  2105. (av_toupper((x >> 16) & 0xFF) << 16) +
  2106. (av_toupper((x >> 24) & 0xFF) << 24);
  2107. }
  2108. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  2109. {
  2110. int ret;
  2111. dst->owner = src->owner;
  2112. ret = av_frame_ref(dst->f, src->f);
  2113. if (ret < 0)
  2114. return ret;
  2115. if (src->progress &&
  2116. !(dst->progress = av_buffer_ref(src->progress))) {
  2117. ff_thread_release_buffer(dst->owner, dst);
  2118. return AVERROR(ENOMEM);
  2119. }
  2120. return 0;
  2121. }
  2122. #if !HAVE_THREADS
  2123. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  2124. {
  2125. f->owner = avctx;
  2126. return ff_get_buffer(avctx, f->f, flags);
  2127. }
  2128. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  2129. {
  2130. if (f->f)
  2131. av_frame_unref(f->f);
  2132. }
  2133. void ff_thread_finish_setup(AVCodecContext *avctx)
  2134. {
  2135. }
  2136. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  2137. {
  2138. }
  2139. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  2140. {
  2141. }
  2142. #endif
  2143. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  2144. {
  2145. if (codec_id <= AV_CODEC_ID_NONE)
  2146. return AVMEDIA_TYPE_UNKNOWN;
  2147. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  2148. return AVMEDIA_TYPE_VIDEO;
  2149. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  2150. return AVMEDIA_TYPE_AUDIO;
  2151. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  2152. return AVMEDIA_TYPE_SUBTITLE;
  2153. return AVMEDIA_TYPE_UNKNOWN;
  2154. }
  2155. int avcodec_is_open(AVCodecContext *s)
  2156. {
  2157. return !!s->internal;
  2158. }
  2159. const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
  2160. const uint8_t *end,
  2161. uint32_t * restrict state)
  2162. {
  2163. int i;
  2164. assert(p <= end);
  2165. if (p >= end)
  2166. return end;
  2167. for (i = 0; i < 3; i++) {
  2168. uint32_t tmp = *state << 8;
  2169. *state = tmp + *(p++);
  2170. if (tmp == 0x100 || p == end)
  2171. return p;
  2172. }
  2173. while (p < end) {
  2174. if (p[-1] > 1 ) p += 3;
  2175. else if (p[-2] ) p += 2;
  2176. else if (p[-3]|(p[-1]-1)) p++;
  2177. else {
  2178. p++;
  2179. break;
  2180. }
  2181. }
  2182. p = FFMIN(p, end) - 4;
  2183. *state = AV_RB32(p);
  2184. return p + 4;
  2185. }