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.

2519 lines
79KB

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