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.

2471 lines
77KB

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