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.

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