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.

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