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.

1997 lines
61KB

  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 "audioconvert.h"
  42. #include "internal.h"
  43. #include "bytestream.h"
  44. #include <stdlib.h>
  45. #include <stdarg.h>
  46. #include <limits.h>
  47. #include <float.h>
  48. static int volatile entangled_thread_counter=0;
  49. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  50. static void *codec_mutex;
  51. static void *avformat_mutex;
  52. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  53. {
  54. if(min_size < *size)
  55. return ptr;
  56. min_size= FFMAX(17*min_size/16 + 32, min_size);
  57. ptr= av_realloc(ptr, min_size);
  58. 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
  59. min_size= 0;
  60. *size= min_size;
  61. return ptr;
  62. }
  63. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  64. {
  65. void **p = ptr;
  66. if (min_size < *size)
  67. return 0;
  68. min_size= FFMAX(17*min_size/16 + 32, min_size);
  69. av_free(*p);
  70. *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  71. if (!*p) min_size = 0;
  72. *size= min_size;
  73. return 1;
  74. }
  75. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  76. {
  77. ff_fast_malloc(ptr, size, min_size, 0);
  78. }
  79. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  80. {
  81. uint8_t **p = ptr;
  82. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  83. av_freep(p);
  84. *size = 0;
  85. return;
  86. }
  87. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  88. memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  89. }
  90. /* encoder management */
  91. static AVCodec *first_avcodec = NULL;
  92. AVCodec *av_codec_next(AVCodec *c){
  93. if(c) return c->next;
  94. else return first_avcodec;
  95. }
  96. static void avcodec_init(void)
  97. {
  98. static int initialized = 0;
  99. if (initialized != 0)
  100. return;
  101. initialized = 1;
  102. dsputil_static_init();
  103. }
  104. static av_always_inline int codec_is_encoder(AVCodec *codec)
  105. {
  106. return codec && (codec->encode || codec->encode2);
  107. }
  108. static av_always_inline int codec_is_decoder(AVCodec *codec)
  109. {
  110. return codec && codec->decode;
  111. }
  112. void avcodec_register(AVCodec *codec)
  113. {
  114. AVCodec **p;
  115. avcodec_init();
  116. p = &first_avcodec;
  117. while (*p != NULL) p = &(*p)->next;
  118. *p = codec;
  119. codec->next = NULL;
  120. if (codec->init_static_data)
  121. codec->init_static_data(codec);
  122. }
  123. unsigned avcodec_get_edge_width(void)
  124. {
  125. return EDGE_WIDTH;
  126. }
  127. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  128. s->coded_width = width;
  129. s->coded_height= height;
  130. s->width = -((-width )>>s->lowres);
  131. s->height= -((-height)>>s->lowres);
  132. }
  133. #define INTERNAL_BUFFER_SIZE (32+1)
  134. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  135. int linesize_align[AV_NUM_DATA_POINTERS])
  136. {
  137. int i;
  138. int w_align= 1;
  139. int h_align= 1;
  140. switch(s->pix_fmt){
  141. case PIX_FMT_YUV420P:
  142. case PIX_FMT_YUYV422:
  143. case PIX_FMT_UYVY422:
  144. case PIX_FMT_YUV422P:
  145. case PIX_FMT_YUV440P:
  146. case PIX_FMT_YUV444P:
  147. case PIX_FMT_GBRP:
  148. case PIX_FMT_GRAY8:
  149. case PIX_FMT_GRAY16BE:
  150. case PIX_FMT_GRAY16LE:
  151. case PIX_FMT_YUVJ420P:
  152. case PIX_FMT_YUVJ422P:
  153. case PIX_FMT_YUVJ440P:
  154. case PIX_FMT_YUVJ444P:
  155. case PIX_FMT_YUVA420P:
  156. case PIX_FMT_YUV420P9LE:
  157. case PIX_FMT_YUV420P9BE:
  158. case PIX_FMT_YUV420P10LE:
  159. case PIX_FMT_YUV420P10BE:
  160. case PIX_FMT_YUV422P9LE:
  161. case PIX_FMT_YUV422P9BE:
  162. case PIX_FMT_YUV422P10LE:
  163. case PIX_FMT_YUV422P10BE:
  164. case PIX_FMT_YUV444P9LE:
  165. case PIX_FMT_YUV444P9BE:
  166. case PIX_FMT_YUV444P10LE:
  167. case PIX_FMT_YUV444P10BE:
  168. case PIX_FMT_GBRP9LE:
  169. case PIX_FMT_GBRP9BE:
  170. case PIX_FMT_GBRP10LE:
  171. case PIX_FMT_GBRP10BE:
  172. w_align = 16; //FIXME assume 16 pixel per macroblock
  173. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  174. break;
  175. case PIX_FMT_YUV411P:
  176. case PIX_FMT_UYYVYY411:
  177. w_align=32;
  178. h_align=8;
  179. break;
  180. case PIX_FMT_YUV410P:
  181. if(s->codec_id == CODEC_ID_SVQ1){
  182. w_align=64;
  183. h_align=64;
  184. }
  185. case PIX_FMT_RGB555:
  186. if(s->codec_id == CODEC_ID_RPZA){
  187. w_align=4;
  188. h_align=4;
  189. }
  190. case PIX_FMT_PAL8:
  191. case PIX_FMT_BGR8:
  192. case PIX_FMT_RGB8:
  193. if(s->codec_id == CODEC_ID_SMC){
  194. w_align=4;
  195. h_align=4;
  196. }
  197. break;
  198. case PIX_FMT_BGR24:
  199. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  200. w_align=4;
  201. h_align=4;
  202. }
  203. break;
  204. default:
  205. w_align= 1;
  206. h_align= 1;
  207. break;
  208. }
  209. if(s->codec_id == CODEC_ID_IFF_ILBM || s->codec_id == CODEC_ID_IFF_BYTERUN1){
  210. w_align= FFMAX(w_align, 8);
  211. }
  212. *width = FFALIGN(*width , w_align);
  213. *height= FFALIGN(*height, h_align);
  214. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  215. *height+=2; // some of the optimized chroma MC reads one line too much
  216. // which is also done in mpeg decoders with lowres > 0
  217. for (i = 0; i < 4; i++)
  218. linesize_align[i] = STRIDE_ALIGN;
  219. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  220. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  221. //picture size unneccessarily in some cases. The solution here is not
  222. //pretty and better ideas are welcome!
  223. #if HAVE_MMX
  224. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  225. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  226. s->codec_id == CODEC_ID_VP6A || s->codec_id == CODEC_ID_DIRAC) {
  227. for (i = 0; i < 4; i++)
  228. linesize_align[i] = 16;
  229. }
  230. #endif
  231. }
  232. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  233. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  234. int linesize_align[AV_NUM_DATA_POINTERS];
  235. int align;
  236. avcodec_align_dimensions2(s, width, height, linesize_align);
  237. align = FFMAX(linesize_align[0], linesize_align[3]);
  238. linesize_align[1] <<= chroma_shift;
  239. linesize_align[2] <<= chroma_shift;
  240. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  241. *width=FFALIGN(*width, align);
  242. }
  243. void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
  244. {
  245. if (s->pkt) {
  246. pic->pkt_pts = s->pkt->pts;
  247. pic->pkt_pos = s->pkt->pos;
  248. } else {
  249. pic->pkt_pts = AV_NOPTS_VALUE;
  250. pic->pkt_pos = -1;
  251. }
  252. pic->reordered_opaque= s->reordered_opaque;
  253. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  254. pic->width = s->width;
  255. pic->height = s->height;
  256. pic->format = s->pix_fmt;
  257. }
  258. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  259. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  260. int buf_size, int align)
  261. {
  262. int ch, planar, needed_size, ret = 0;
  263. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  264. frame->nb_samples, sample_fmt,
  265. align);
  266. if (buf_size < needed_size)
  267. return AVERROR(EINVAL);
  268. planar = av_sample_fmt_is_planar(sample_fmt);
  269. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  270. if (!(frame->extended_data = av_mallocz(nb_channels *
  271. sizeof(*frame->extended_data))))
  272. return AVERROR(ENOMEM);
  273. } else {
  274. frame->extended_data = frame->data;
  275. }
  276. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  277. buf, nb_channels, frame->nb_samples,
  278. sample_fmt, align)) < 0) {
  279. if (frame->extended_data != frame->data)
  280. av_freep(&frame->extended_data);
  281. return ret;
  282. }
  283. if (frame->extended_data != frame->data) {
  284. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  285. frame->data[ch] = frame->extended_data[ch];
  286. }
  287. return ret;
  288. }
  289. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  290. {
  291. AVCodecInternal *avci = avctx->internal;
  292. InternalBuffer *buf;
  293. int buf_size, ret;
  294. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  295. frame->nb_samples, avctx->sample_fmt,
  296. 32);
  297. if (buf_size < 0)
  298. return AVERROR(EINVAL);
  299. /* allocate InternalBuffer if needed */
  300. if (!avci->buffer) {
  301. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  302. if (!avci->buffer)
  303. return AVERROR(ENOMEM);
  304. }
  305. buf = avci->buffer;
  306. /* if there is a previously-used internal buffer, check its size and
  307. channel count to see if we can reuse it */
  308. if (buf->extended_data) {
  309. /* if current buffer is too small, free it */
  310. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  311. av_free(buf->extended_data[0]);
  312. if (buf->extended_data != buf->data)
  313. av_freep(&buf->extended_data);
  314. buf->extended_data = NULL;
  315. buf->data[0] = NULL;
  316. }
  317. /* if number of channels has changed, reset and/or free extended data
  318. pointers but leave data buffer in buf->data[0] for reuse */
  319. if (buf->nb_channels != avctx->channels) {
  320. if (buf->extended_data != buf->data)
  321. av_free(buf->extended_data);
  322. buf->extended_data = NULL;
  323. }
  324. }
  325. /* if there is no previous buffer or the previous buffer cannot be used
  326. as-is, allocate a new buffer and/or rearrange the channel pointers */
  327. if (!buf->extended_data) {
  328. if (!buf->data[0]) {
  329. if (!(buf->data[0] = av_mallocz(buf_size)))
  330. return AVERROR(ENOMEM);
  331. buf->audio_data_size = buf_size;
  332. }
  333. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  334. avctx->sample_fmt, buf->data[0],
  335. buf->audio_data_size, 32)))
  336. return ret;
  337. if (frame->extended_data == frame->data)
  338. buf->extended_data = buf->data;
  339. else
  340. buf->extended_data = frame->extended_data;
  341. memcpy(buf->data, frame->data, sizeof(frame->data));
  342. buf->linesize[0] = frame->linesize[0];
  343. buf->nb_channels = avctx->channels;
  344. } else {
  345. /* copy InternalBuffer info to the AVFrame */
  346. frame->extended_data = buf->extended_data;
  347. frame->linesize[0] = buf->linesize[0];
  348. memcpy(frame->data, buf->data, sizeof(frame->data));
  349. }
  350. frame->type = FF_BUFFER_TYPE_INTERNAL;
  351. if (avctx->pkt) {
  352. frame->pkt_pts = avctx->pkt->pts;
  353. frame->pkt_pos = avctx->pkt->pos;
  354. } else {
  355. frame->pkt_pts = AV_NOPTS_VALUE;
  356. frame->pkt_pos = -1;
  357. }
  358. frame->reordered_opaque = avctx->reordered_opaque;
  359. if (avctx->debug & FF_DEBUG_BUFFERS)
  360. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  361. "internal audio buffer used\n", frame);
  362. return 0;
  363. }
  364. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  365. {
  366. int i;
  367. int w= s->width;
  368. int h= s->height;
  369. InternalBuffer *buf;
  370. AVCodecInternal *avci = s->internal;
  371. if(pic->data[0]!=NULL) {
  372. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  373. return -1;
  374. }
  375. if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  376. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  377. return -1;
  378. }
  379. if(av_image_check_size(w, h, 0, s))
  380. return -1;
  381. if (!avci->buffer) {
  382. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
  383. sizeof(InternalBuffer));
  384. }
  385. buf = &avci->buffer[avci->buffer_count];
  386. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  387. if(s->active_thread_type&FF_THREAD_FRAME) {
  388. av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
  389. return -1;
  390. }
  391. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  392. av_freep(&buf->base[i]);
  393. buf->data[i]= NULL;
  394. }
  395. }
  396. if (!buf->base[0]) {
  397. int h_chroma_shift, v_chroma_shift;
  398. int size[4] = {0};
  399. int tmpsize;
  400. int unaligned;
  401. AVPicture picture;
  402. int stride_align[AV_NUM_DATA_POINTERS];
  403. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  404. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  405. avcodec_align_dimensions2(s, &w, &h, stride_align);
  406. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  407. w+= EDGE_WIDTH*2;
  408. h+= EDGE_WIDTH*2;
  409. }
  410. do {
  411. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  412. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  413. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  414. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  415. w += w & ~(w-1);
  416. unaligned = 0;
  417. for (i=0; i<4; i++){
  418. unaligned |= picture.linesize[i] % stride_align[i];
  419. }
  420. } while (unaligned);
  421. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  422. if (tmpsize < 0)
  423. return -1;
  424. for (i=0; i<3 && picture.data[i+1]; i++)
  425. size[i] = picture.data[i+1] - picture.data[i];
  426. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  427. memset(buf->base, 0, sizeof(buf->base));
  428. memset(buf->data, 0, sizeof(buf->data));
  429. for(i=0; i<4 && size[i]; i++){
  430. const int h_shift= i==0 ? 0 : h_chroma_shift;
  431. const int v_shift= i==0 ? 0 : v_chroma_shift;
  432. buf->linesize[i]= picture.linesize[i];
  433. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  434. if(buf->base[i]==NULL) return -1;
  435. memset(buf->base[i], 128, size[i]);
  436. // no edge if EDGE EMU or not planar YUV
  437. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  438. buf->data[i] = buf->base[i];
  439. else
  440. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
  441. }
  442. for (; i < AV_NUM_DATA_POINTERS; i++) {
  443. buf->base[i] = buf->data[i] = NULL;
  444. buf->linesize[i] = 0;
  445. }
  446. if(size[1] && !size[2])
  447. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  448. buf->width = s->width;
  449. buf->height = s->height;
  450. buf->pix_fmt= s->pix_fmt;
  451. }
  452. pic->type= FF_BUFFER_TYPE_INTERNAL;
  453. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  454. pic->base[i]= buf->base[i];
  455. pic->data[i]= buf->data[i];
  456. pic->linesize[i]= buf->linesize[i];
  457. }
  458. pic->extended_data = pic->data;
  459. avci->buffer_count++;
  460. if (s->pkt) {
  461. pic->pkt_pts = s->pkt->pts;
  462. pic->pkt_pos = s->pkt->pos;
  463. } else {
  464. pic->pkt_pts = AV_NOPTS_VALUE;
  465. pic->pkt_pos = -1;
  466. }
  467. pic->reordered_opaque= s->reordered_opaque;
  468. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  469. pic->width = s->width;
  470. pic->height = s->height;
  471. pic->format = s->pix_fmt;
  472. if(s->debug&FF_DEBUG_BUFFERS)
  473. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  474. "buffers used\n", pic, avci->buffer_count);
  475. return 0;
  476. }
  477. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  478. {
  479. switch (avctx->codec_type) {
  480. case AVMEDIA_TYPE_VIDEO:
  481. return video_get_buffer(avctx, frame);
  482. case AVMEDIA_TYPE_AUDIO:
  483. return audio_get_buffer(avctx, frame);
  484. default:
  485. return -1;
  486. }
  487. }
  488. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  489. int i;
  490. InternalBuffer *buf, *last;
  491. AVCodecInternal *avci = s->internal;
  492. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  493. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  494. assert(avci->buffer_count);
  495. if (avci->buffer) {
  496. buf = NULL; /* avoids warning */
  497. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  498. buf = &avci->buffer[i];
  499. if (buf->data[0] == pic->data[0])
  500. break;
  501. }
  502. assert(i < avci->buffer_count);
  503. avci->buffer_count--;
  504. last = &avci->buffer[avci->buffer_count];
  505. if (buf != last)
  506. FFSWAP(InternalBuffer, *buf, *last);
  507. }
  508. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  509. pic->data[i]=NULL;
  510. // pic->base[i]=NULL;
  511. }
  512. //printf("R%X\n", pic->opaque);
  513. if(s->debug&FF_DEBUG_BUFFERS)
  514. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  515. "buffers used\n", pic, avci->buffer_count);
  516. }
  517. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  518. AVFrame temp_pic;
  519. int i;
  520. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  521. if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
  522. av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  523. pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
  524. s->release_buffer(s, pic);
  525. }
  526. ff_init_buffer_info(s, pic);
  527. /* If no picture return a new buffer */
  528. if(pic->data[0] == NULL) {
  529. /* We will copy from buffer, so must be readable */
  530. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  531. return s->get_buffer(s, pic);
  532. }
  533. /* If internal buffer type return the same buffer */
  534. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  535. return 0;
  536. }
  537. /*
  538. * Not internal type and reget_buffer not overridden, emulate cr buffer
  539. */
  540. temp_pic = *pic;
  541. for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
  542. pic->data[i] = pic->base[i] = NULL;
  543. pic->opaque = NULL;
  544. /* Allocate new frame */
  545. if (s->get_buffer(s, pic))
  546. return -1;
  547. /* Copy image data from old buffer to new buffer */
  548. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  549. s->height);
  550. s->release_buffer(s, &temp_pic); // Release old frame
  551. return 0;
  552. }
  553. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  554. int i;
  555. for(i=0; i<count; i++){
  556. int r= func(c, (char*)arg + i*size);
  557. if(ret) ret[i]= r;
  558. }
  559. return 0;
  560. }
  561. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  562. int i;
  563. for(i=0; i<count; i++){
  564. int r= func(c, arg, i, 0);
  565. if(ret) ret[i]= r;
  566. }
  567. return 0;
  568. }
  569. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  570. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  571. ++fmt;
  572. return fmt[0];
  573. }
  574. void avcodec_get_frame_defaults(AVFrame *pic){
  575. memset(pic, 0, sizeof(AVFrame));
  576. pic->pts = pic->pkt_dts = pic->pkt_pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
  577. pic->pkt_pos = -1;
  578. pic->key_frame= 1;
  579. pic->sample_aspect_ratio = (AVRational){0, 1};
  580. pic->format = -1; /* unknown */
  581. }
  582. AVFrame *avcodec_alloc_frame(void){
  583. AVFrame *pic= av_malloc(sizeof(AVFrame));
  584. if(pic==NULL) return NULL;
  585. avcodec_get_frame_defaults(pic);
  586. return pic;
  587. }
  588. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  589. {
  590. memset(sub, 0, sizeof(*sub));
  591. sub->pts = AV_NOPTS_VALUE;
  592. }
  593. #if FF_API_AVCODEC_OPEN
  594. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  595. {
  596. return avcodec_open2(avctx, codec, NULL);
  597. }
  598. #endif
  599. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  600. {
  601. int ret = 0;
  602. AVDictionary *tmp = NULL;
  603. if (avcodec_is_open(avctx))
  604. return 0;
  605. if ((!codec && !avctx->codec)) {
  606. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  607. return AVERROR(EINVAL);
  608. }
  609. if ((codec && avctx->codec && codec != avctx->codec)) {
  610. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  611. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  612. return AVERROR(EINVAL);
  613. }
  614. if (!codec)
  615. codec = avctx->codec;
  616. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  617. return AVERROR(EINVAL);
  618. if (options)
  619. av_dict_copy(&tmp, *options, 0);
  620. /* If there is a user-supplied mutex locking routine, call it. */
  621. if (ff_lockmgr_cb) {
  622. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  623. return -1;
  624. }
  625. entangled_thread_counter++;
  626. if(entangled_thread_counter != 1){
  627. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  628. ret = -1;
  629. goto end;
  630. }
  631. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  632. if (!avctx->internal) {
  633. ret = AVERROR(ENOMEM);
  634. goto end;
  635. }
  636. if (codec->priv_data_size > 0) {
  637. if(!avctx->priv_data){
  638. avctx->priv_data = av_mallocz(codec->priv_data_size);
  639. if (!avctx->priv_data) {
  640. ret = AVERROR(ENOMEM);
  641. goto end;
  642. }
  643. if (codec->priv_class) {
  644. *(AVClass**)avctx->priv_data= codec->priv_class;
  645. av_opt_set_defaults(avctx->priv_data);
  646. }
  647. }
  648. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  649. goto free_and_end;
  650. } else {
  651. avctx->priv_data = NULL;
  652. }
  653. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  654. goto free_and_end;
  655. if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
  656. if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  657. av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, see -strict -2\n");
  658. ret = -1;
  659. goto free_and_end;
  660. }
  661. //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
  662. if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
  663. if(avctx->coded_width && avctx->coded_height)
  664. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  665. else if(avctx->width && avctx->height)
  666. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  667. }
  668. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  669. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  670. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  671. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  672. avcodec_set_dimensions(avctx, 0, 0);
  673. }
  674. /* if the decoder init function was already called previously,
  675. free the already allocated subtitle_header before overwriting it */
  676. if (codec_is_decoder(codec))
  677. av_freep(&avctx->subtitle_header);
  678. #define SANE_NB_CHANNELS 128U
  679. if (avctx->channels > SANE_NB_CHANNELS) {
  680. ret = AVERROR(EINVAL);
  681. goto free_and_end;
  682. }
  683. avctx->codec = codec;
  684. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  685. avctx->codec_id == CODEC_ID_NONE) {
  686. avctx->codec_type = codec->type;
  687. avctx->codec_id = codec->id;
  688. }
  689. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  690. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  691. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  692. ret = AVERROR(EINVAL);
  693. goto free_and_end;
  694. }
  695. avctx->frame_number = 0;
  696. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  697. (!avctx->time_base.num || !avctx->time_base.den)) {
  698. avctx->time_base.num = 1;
  699. avctx->time_base.den = avctx->sample_rate;
  700. }
  701. if (!HAVE_THREADS)
  702. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  703. if (HAVE_THREADS && !avctx->thread_opaque) {
  704. ret = ff_thread_init(avctx);
  705. if (ret < 0) {
  706. goto free_and_end;
  707. }
  708. }
  709. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  710. avctx->thread_count = 1;
  711. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  712. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  713. avctx->codec->max_lowres);
  714. ret = AVERROR(EINVAL);
  715. goto free_and_end;
  716. }
  717. if (codec_is_encoder(avctx->codec)) {
  718. int i;
  719. if (avctx->codec->sample_fmts) {
  720. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  721. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  722. break;
  723. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  724. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  725. ret = AVERROR(EINVAL);
  726. goto free_and_end;
  727. }
  728. }
  729. if (avctx->codec->supported_samplerates) {
  730. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  731. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  732. break;
  733. if (avctx->codec->supported_samplerates[i] == 0) {
  734. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  735. ret = AVERROR(EINVAL);
  736. goto free_and_end;
  737. }
  738. }
  739. if (avctx->codec->channel_layouts) {
  740. if (!avctx->channel_layout) {
  741. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  742. } else {
  743. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  744. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  745. break;
  746. if (avctx->codec->channel_layouts[i] == 0) {
  747. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  748. ret = AVERROR(EINVAL);
  749. goto free_and_end;
  750. }
  751. }
  752. }
  753. if (avctx->channel_layout && avctx->channels) {
  754. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  755. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  756. ret = AVERROR(EINVAL);
  757. goto free_and_end;
  758. }
  759. } else if (avctx->channel_layout) {
  760. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  761. }
  762. }
  763. avctx->pts_correction_num_faulty_pts =
  764. avctx->pts_correction_num_faulty_dts = 0;
  765. avctx->pts_correction_last_pts =
  766. avctx->pts_correction_last_dts = INT64_MIN;
  767. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  768. ret = avctx->codec->init(avctx);
  769. if (ret < 0) {
  770. goto free_and_end;
  771. }
  772. }
  773. ret=0;
  774. end:
  775. entangled_thread_counter--;
  776. /* Release any user-supplied mutex. */
  777. if (ff_lockmgr_cb) {
  778. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  779. }
  780. if (options) {
  781. av_dict_free(options);
  782. *options = tmp;
  783. }
  784. return ret;
  785. free_and_end:
  786. av_dict_free(&tmp);
  787. av_freep(&avctx->priv_data);
  788. av_freep(&avctx->internal);
  789. avctx->codec= NULL;
  790. goto end;
  791. }
  792. int ff_alloc_packet(AVPacket *avpkt, int size)
  793. {
  794. if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  795. return AVERROR(EINVAL);
  796. if (avpkt->data) {
  797. uint8_t *pkt_data;
  798. int pkt_size;
  799. if (avpkt->size < size)
  800. return AVERROR(EINVAL);
  801. pkt_data = avpkt->data;
  802. pkt_size = avpkt->size;
  803. av_init_packet(avpkt);
  804. avpkt->data = pkt_data;
  805. avpkt->size = pkt_size;
  806. return 0;
  807. } else {
  808. return av_new_packet(avpkt, size);
  809. }
  810. }
  811. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  812. AVPacket *avpkt,
  813. const AVFrame *frame,
  814. int *got_packet_ptr)
  815. {
  816. int ret;
  817. int user_packet = !!avpkt->data;
  818. int nb_samples;
  819. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  820. av_init_packet(avpkt);
  821. avpkt->size = 0;
  822. return 0;
  823. }
  824. /* check for valid frame size */
  825. if (frame) {
  826. nb_samples = frame->nb_samples;
  827. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  828. if (nb_samples > avctx->frame_size)
  829. return AVERROR(EINVAL);
  830. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  831. if (nb_samples != avctx->frame_size)
  832. return AVERROR(EINVAL);
  833. }
  834. } else {
  835. nb_samples = avctx->frame_size;
  836. }
  837. if (avctx->codec->encode2) {
  838. *got_packet_ptr = 0;
  839. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  840. if (!ret && *got_packet_ptr &&
  841. !(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  842. avpkt->pts = frame->pts;
  843. avpkt->duration = av_rescale_q(frame->nb_samples,
  844. (AVRational){ 1, avctx->sample_rate },
  845. avctx->time_base);
  846. }
  847. } else {
  848. /* for compatibility with encoders not supporting encode2(), we need to
  849. allocate a packet buffer if the user has not provided one or check
  850. the size otherwise */
  851. int fs_tmp = 0;
  852. int buf_size = avpkt->size;
  853. if (!user_packet) {
  854. if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
  855. av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
  856. if (!frame)
  857. return AVERROR(EINVAL);
  858. buf_size = nb_samples * avctx->channels *
  859. av_get_bits_per_sample(avctx->codec_id) / 8;
  860. } else {
  861. /* this is a guess as to the required size.
  862. if an encoder needs more than this, it should probably
  863. implement encode2() */
  864. buf_size = 2 * avctx->frame_size * avctx->channels *
  865. av_get_bytes_per_sample(avctx->sample_fmt);
  866. buf_size += FF_MIN_BUFFER_SIZE;
  867. }
  868. }
  869. if ((ret = ff_alloc_packet(avpkt, buf_size)))
  870. return ret;
  871. /* Encoders using AVCodec.encode() that support
  872. CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
  873. the smaller size when encoding the last frame.
  874. This code can be removed once all encoders supporting
  875. CODEC_CAP_SMALL_LAST_FRAME use encode2() */
  876. if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
  877. nb_samples < avctx->frame_size) {
  878. fs_tmp = avctx->frame_size;
  879. avctx->frame_size = nb_samples;
  880. }
  881. /* encode the frame */
  882. ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
  883. frame ? frame->data[0] : NULL);
  884. if (ret >= 0) {
  885. if (!ret) {
  886. /* no output. if the packet data was allocated by libavcodec,
  887. free it */
  888. if (!user_packet)
  889. av_freep(&avpkt->data);
  890. } else {
  891. if (avctx->coded_frame)
  892. avpkt->pts = avctx->coded_frame->pts;
  893. /* Set duration for final small packet. This can be removed
  894. once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
  895. encode2() */
  896. if (fs_tmp) {
  897. avpkt->duration = av_rescale_q(avctx->frame_size,
  898. (AVRational){ 1, avctx->sample_rate },
  899. avctx->time_base);
  900. }
  901. }
  902. avpkt->size = ret;
  903. *got_packet_ptr = (ret > 0);
  904. ret = 0;
  905. }
  906. if (fs_tmp)
  907. avctx->frame_size = fs_tmp;
  908. }
  909. if (!ret)
  910. avctx->frame_number++;
  911. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  912. this needs to be moved to the encoders, but for now we can do it
  913. here to simplify things */
  914. avpkt->flags |= AV_PKT_FLAG_KEY;
  915. return ret;
  916. }
  917. #if FF_API_OLD_DECODE_AUDIO
  918. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  919. uint8_t *buf, int buf_size,
  920. const short *samples)
  921. {
  922. AVPacket pkt;
  923. AVFrame frame0;
  924. AVFrame *frame;
  925. int ret, samples_size, got_packet;
  926. av_init_packet(&pkt);
  927. pkt.data = buf;
  928. pkt.size = buf_size;
  929. if (samples) {
  930. frame = &frame0;
  931. avcodec_get_frame_defaults(frame);
  932. if (avctx->frame_size) {
  933. frame->nb_samples = avctx->frame_size;
  934. } else {
  935. /* if frame_size is not set, the number of samples must be
  936. calculated from the buffer size */
  937. int64_t nb_samples;
  938. if (!av_get_bits_per_sample(avctx->codec_id)) {
  939. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  940. "support this codec\n");
  941. return AVERROR(EINVAL);
  942. }
  943. nb_samples = (int64_t)buf_size * 8 /
  944. (av_get_bits_per_sample(avctx->codec_id) *
  945. avctx->channels);
  946. if (nb_samples >= INT_MAX)
  947. return AVERROR(EINVAL);
  948. frame->nb_samples = nb_samples;
  949. }
  950. /* it is assumed that the samples buffer is large enough based on the
  951. relevant parameters */
  952. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  953. frame->nb_samples,
  954. avctx->sample_fmt, 1);
  955. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  956. avctx->sample_fmt,
  957. samples, samples_size, 1)))
  958. return ret;
  959. /* fabricate frame pts from sample count.
  960. this is needed because the avcodec_encode_audio() API does not have
  961. a way for the user to provide pts */
  962. if(avctx->sample_rate && avctx->time_base.num)
  963. frame->pts = av_rescale_q(avctx->internal->sample_count,
  964. (AVRational){ 1, avctx->sample_rate },
  965. avctx->time_base);
  966. else
  967. frame->pts = AV_NOPTS_VALUE;
  968. avctx->internal->sample_count += frame->nb_samples;
  969. } else {
  970. frame = NULL;
  971. }
  972. got_packet = 0;
  973. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  974. if (!ret && got_packet && avctx->coded_frame) {
  975. avctx->coded_frame->pts = pkt.pts;
  976. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  977. }
  978. /* free any side data since we cannot return it */
  979. ff_packet_free_side_data(&pkt);
  980. if (frame && frame->extended_data != frame->data)
  981. av_freep(&frame->extended_data);
  982. return ret ? ret : pkt.size;
  983. }
  984. #endif
  985. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  986. const AVFrame *pict)
  987. {
  988. if(buf_size < FF_MIN_BUFFER_SIZE){
  989. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  990. return -1;
  991. }
  992. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  993. return -1;
  994. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  995. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  996. avctx->frame_number++;
  997. emms_c(); //needed to avoid an emms_c() call before every return;
  998. return ret;
  999. }else
  1000. return 0;
  1001. }
  1002. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1003. const AVSubtitle *sub)
  1004. {
  1005. int ret;
  1006. if(sub->start_display_time) {
  1007. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1008. return -1;
  1009. }
  1010. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  1011. avctx->frame_number++;
  1012. return ret;
  1013. }
  1014. /**
  1015. * Attempt to guess proper monotonic timestamps for decoded video frames
  1016. * which might have incorrect times. Input timestamps may wrap around, in
  1017. * which case the output will as well.
  1018. *
  1019. * @param pts the pts field of the decoded AVPacket, as passed through
  1020. * AVFrame.pkt_pts
  1021. * @param dts the dts field of the decoded AVPacket
  1022. * @return one of the input values, may be AV_NOPTS_VALUE
  1023. */
  1024. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1025. int64_t reordered_pts, int64_t dts)
  1026. {
  1027. int64_t pts = AV_NOPTS_VALUE;
  1028. if (dts != AV_NOPTS_VALUE) {
  1029. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1030. ctx->pts_correction_last_dts = dts;
  1031. }
  1032. if (reordered_pts != AV_NOPTS_VALUE) {
  1033. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1034. ctx->pts_correction_last_pts = reordered_pts;
  1035. }
  1036. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1037. && reordered_pts != AV_NOPTS_VALUE)
  1038. pts = reordered_pts;
  1039. else
  1040. pts = dts;
  1041. return pts;
  1042. }
  1043. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1044. {
  1045. int size = 0;
  1046. const uint8_t *data;
  1047. uint32_t flags;
  1048. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1049. return;
  1050. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1051. if (!data || size < 4)
  1052. return;
  1053. flags = bytestream_get_le32(&data);
  1054. size -= 4;
  1055. if (size < 4) /* Required for any of the changes */
  1056. return;
  1057. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1058. avctx->channels = bytestream_get_le32(&data);
  1059. size -= 4;
  1060. }
  1061. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1062. if (size < 8)
  1063. return;
  1064. avctx->channel_layout = bytestream_get_le64(&data);
  1065. size -= 8;
  1066. }
  1067. if (size < 4)
  1068. return;
  1069. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1070. avctx->sample_rate = bytestream_get_le32(&data);
  1071. size -= 4;
  1072. }
  1073. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1074. if (size < 8)
  1075. return;
  1076. avctx->width = bytestream_get_le32(&data);
  1077. avctx->height = bytestream_get_le32(&data);
  1078. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1079. size -= 8;
  1080. }
  1081. }
  1082. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1083. int *got_picture_ptr,
  1084. const AVPacket *avpkt)
  1085. {
  1086. int ret;
  1087. // copy to ensure we do not change avpkt
  1088. AVPacket tmp = *avpkt;
  1089. *got_picture_ptr= 0;
  1090. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1091. return -1;
  1092. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  1093. int did_split = av_packet_split_side_data(&tmp);
  1094. apply_param_change(avctx, &tmp);
  1095. avctx->pkt = &tmp;
  1096. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1097. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1098. &tmp);
  1099. else {
  1100. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1101. &tmp);
  1102. picture->pkt_dts= avpkt->dts;
  1103. if(!avctx->has_b_frames){
  1104. picture->pkt_pos= avpkt->pos;
  1105. }
  1106. //FIXME these should be under if(!avctx->has_b_frames)
  1107. if (!picture->sample_aspect_ratio.num)
  1108. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1109. if (!picture->width)
  1110. picture->width = avctx->width;
  1111. if (!picture->height)
  1112. picture->height = avctx->height;
  1113. if (picture->format == PIX_FMT_NONE)
  1114. picture->format = avctx->pix_fmt;
  1115. }
  1116. emms_c(); //needed to avoid an emms_c() call before every return;
  1117. avctx->pkt = NULL;
  1118. if (did_split)
  1119. ff_packet_free_side_data(&tmp);
  1120. if (*got_picture_ptr){
  1121. avctx->frame_number++;
  1122. picture->best_effort_timestamp = guess_correct_pts(avctx,
  1123. picture->pkt_pts,
  1124. picture->pkt_dts);
  1125. }
  1126. }else
  1127. ret= 0;
  1128. return ret;
  1129. }
  1130. #if FF_API_OLD_DECODE_AUDIO
  1131. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1132. int *frame_size_ptr,
  1133. AVPacket *avpkt)
  1134. {
  1135. AVFrame frame;
  1136. int ret, got_frame = 0;
  1137. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1138. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1139. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1140. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1141. "avcodec_decode_audio4()\n");
  1142. avctx->get_buffer = avcodec_default_get_buffer;
  1143. avctx->release_buffer = avcodec_default_release_buffer;
  1144. }
  1145. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1146. if (ret >= 0 && got_frame) {
  1147. int ch, plane_size;
  1148. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1149. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1150. frame.nb_samples,
  1151. avctx->sample_fmt, 1);
  1152. if (*frame_size_ptr < data_size) {
  1153. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1154. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1155. return AVERROR(EINVAL);
  1156. }
  1157. memcpy(samples, frame.extended_data[0], plane_size);
  1158. if (planar && avctx->channels > 1) {
  1159. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1160. for (ch = 1; ch < avctx->channels; ch++) {
  1161. memcpy(out, frame.extended_data[ch], plane_size);
  1162. out += plane_size;
  1163. }
  1164. }
  1165. *frame_size_ptr = data_size;
  1166. } else {
  1167. *frame_size_ptr = 0;
  1168. }
  1169. return ret;
  1170. }
  1171. #endif
  1172. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1173. AVFrame *frame,
  1174. int *got_frame_ptr,
  1175. AVPacket *avpkt)
  1176. {
  1177. int ret = 0;
  1178. *got_frame_ptr = 0;
  1179. if (!avpkt->data && avpkt->size) {
  1180. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1181. return AVERROR(EINVAL);
  1182. }
  1183. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1184. av_packet_split_side_data(avpkt);
  1185. apply_param_change(avctx, avpkt);
  1186. avctx->pkt = avpkt;
  1187. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1188. if (ret >= 0 && *got_frame_ptr) {
  1189. avctx->frame_number++;
  1190. frame->pkt_dts = avpkt->dts;
  1191. if (frame->format == AV_SAMPLE_FMT_NONE)
  1192. frame->format = avctx->sample_fmt;
  1193. }
  1194. }
  1195. return ret;
  1196. }
  1197. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1198. int *got_sub_ptr,
  1199. AVPacket *avpkt)
  1200. {
  1201. int ret;
  1202. avctx->pkt = avpkt;
  1203. *got_sub_ptr = 0;
  1204. avcodec_get_subtitle_defaults(sub);
  1205. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1206. if (*got_sub_ptr)
  1207. avctx->frame_number++;
  1208. return ret;
  1209. }
  1210. void avsubtitle_free(AVSubtitle *sub)
  1211. {
  1212. int i;
  1213. for (i = 0; i < sub->num_rects; i++)
  1214. {
  1215. av_freep(&sub->rects[i]->pict.data[0]);
  1216. av_freep(&sub->rects[i]->pict.data[1]);
  1217. av_freep(&sub->rects[i]->pict.data[2]);
  1218. av_freep(&sub->rects[i]->pict.data[3]);
  1219. av_freep(&sub->rects[i]->text);
  1220. av_freep(&sub->rects[i]->ass);
  1221. av_freep(&sub->rects[i]);
  1222. }
  1223. av_freep(&sub->rects);
  1224. memset(sub, 0, sizeof(AVSubtitle));
  1225. }
  1226. av_cold int avcodec_close(AVCodecContext *avctx)
  1227. {
  1228. /* If there is a user-supplied mutex locking routine, call it. */
  1229. if (ff_lockmgr_cb) {
  1230. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1231. return -1;
  1232. }
  1233. entangled_thread_counter++;
  1234. if(entangled_thread_counter != 1){
  1235. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1236. entangled_thread_counter--;
  1237. return -1;
  1238. }
  1239. if (avcodec_is_open(avctx)) {
  1240. if (HAVE_THREADS && avctx->thread_opaque)
  1241. ff_thread_free(avctx);
  1242. if (avctx->codec && avctx->codec->close)
  1243. avctx->codec->close(avctx);
  1244. avcodec_default_free_buffers(avctx);
  1245. avctx->coded_frame = NULL;
  1246. av_freep(&avctx->internal);
  1247. }
  1248. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1249. av_opt_free(avctx->priv_data);
  1250. av_opt_free(avctx);
  1251. av_freep(&avctx->priv_data);
  1252. if (codec_is_encoder(avctx->codec))
  1253. av_freep(&avctx->extradata);
  1254. avctx->codec = NULL;
  1255. avctx->active_thread_type = 0;
  1256. entangled_thread_counter--;
  1257. /* Release any user-supplied mutex. */
  1258. if (ff_lockmgr_cb) {
  1259. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1260. }
  1261. return 0;
  1262. }
  1263. static enum CodecID remap_deprecated_codec_id(enum CodecID id)
  1264. {
  1265. switch(id){
  1266. //This is for future deprecatec codec ids, its empty since
  1267. //last major bump but will fill up again over time, please dont remove it
  1268. // case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
  1269. default : return id;
  1270. }
  1271. }
  1272. AVCodec *avcodec_find_encoder(enum CodecID id)
  1273. {
  1274. AVCodec *p, *experimental=NULL;
  1275. p = first_avcodec;
  1276. id= remap_deprecated_codec_id(id);
  1277. while (p) {
  1278. if (codec_is_encoder(p) && p->id == id) {
  1279. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1280. experimental = p;
  1281. } else
  1282. return p;
  1283. }
  1284. p = p->next;
  1285. }
  1286. return experimental;
  1287. }
  1288. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1289. {
  1290. AVCodec *p;
  1291. if (!name)
  1292. return NULL;
  1293. p = first_avcodec;
  1294. while (p) {
  1295. if (codec_is_encoder(p) && strcmp(name,p->name) == 0)
  1296. return p;
  1297. p = p->next;
  1298. }
  1299. return NULL;
  1300. }
  1301. AVCodec *avcodec_find_decoder(enum CodecID id)
  1302. {
  1303. AVCodec *p, *experimental=NULL;
  1304. p = first_avcodec;
  1305. id= remap_deprecated_codec_id(id);
  1306. while (p) {
  1307. if (codec_is_decoder(p) && p->id == id) {
  1308. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1309. experimental = p;
  1310. } else
  1311. return p;
  1312. }
  1313. p = p->next;
  1314. }
  1315. return experimental;
  1316. }
  1317. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1318. {
  1319. AVCodec *p;
  1320. if (!name)
  1321. return NULL;
  1322. p = first_avcodec;
  1323. while (p) {
  1324. if (codec_is_decoder(p) && strcmp(name,p->name) == 0)
  1325. return p;
  1326. p = p->next;
  1327. }
  1328. return NULL;
  1329. }
  1330. static int get_bit_rate(AVCodecContext *ctx)
  1331. {
  1332. int bit_rate;
  1333. int bits_per_sample;
  1334. switch(ctx->codec_type) {
  1335. case AVMEDIA_TYPE_VIDEO:
  1336. case AVMEDIA_TYPE_DATA:
  1337. case AVMEDIA_TYPE_SUBTITLE:
  1338. case AVMEDIA_TYPE_ATTACHMENT:
  1339. bit_rate = ctx->bit_rate;
  1340. break;
  1341. case AVMEDIA_TYPE_AUDIO:
  1342. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1343. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1344. break;
  1345. default:
  1346. bit_rate = 0;
  1347. break;
  1348. }
  1349. return bit_rate;
  1350. }
  1351. const char *avcodec_get_name(enum CodecID id)
  1352. {
  1353. AVCodec *codec;
  1354. #if !CONFIG_SMALL
  1355. switch (id) {
  1356. #include "libavcodec/codec_names.h"
  1357. }
  1358. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1359. #endif
  1360. codec = avcodec_find_decoder(id);
  1361. if (codec)
  1362. return codec->name;
  1363. codec = avcodec_find_encoder(id);
  1364. if (codec)
  1365. return codec->name;
  1366. return "unknown_codec";
  1367. }
  1368. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1369. {
  1370. int i, len, ret = 0;
  1371. for (i = 0; i < 4; i++) {
  1372. len = snprintf(buf, buf_size,
  1373. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1374. buf += len;
  1375. buf_size = buf_size > len ? buf_size - len : 0;
  1376. ret += len;
  1377. codec_tag>>=8;
  1378. }
  1379. return ret;
  1380. }
  1381. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1382. {
  1383. const char *codec_type;
  1384. const char *codec_name;
  1385. const char *profile = NULL;
  1386. AVCodec *p;
  1387. int bitrate;
  1388. AVRational display_aspect_ratio;
  1389. if (!buf || buf_size <= 0)
  1390. return;
  1391. codec_type = av_get_media_type_string(enc->codec_type);
  1392. codec_name = avcodec_get_name(enc->codec_id);
  1393. if (enc->profile != FF_PROFILE_UNKNOWN) {
  1394. p = encode ? avcodec_find_encoder(enc->codec_id) :
  1395. avcodec_find_decoder(enc->codec_id);
  1396. if (p)
  1397. profile = av_get_profile_name(p, enc->profile);
  1398. }
  1399. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  1400. codec_name, enc->mb_decision ? " (hq)" : "");
  1401. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1402. if (profile)
  1403. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1404. if (enc->codec_tag) {
  1405. char tag_buf[32];
  1406. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1407. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1408. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  1409. }
  1410. switch(enc->codec_type) {
  1411. case AVMEDIA_TYPE_VIDEO:
  1412. if (enc->pix_fmt != PIX_FMT_NONE) {
  1413. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1414. ", %s",
  1415. av_get_pix_fmt_name(enc->pix_fmt));
  1416. }
  1417. if (enc->width) {
  1418. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1419. ", %dx%d",
  1420. enc->width, enc->height);
  1421. if (enc->sample_aspect_ratio.num) {
  1422. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1423. enc->width*enc->sample_aspect_ratio.num,
  1424. enc->height*enc->sample_aspect_ratio.den,
  1425. 1024*1024);
  1426. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1427. " [SAR %d:%d DAR %d:%d]",
  1428. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1429. display_aspect_ratio.num, display_aspect_ratio.den);
  1430. }
  1431. if(av_log_get_level() >= AV_LOG_DEBUG){
  1432. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1433. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1434. ", %d/%d",
  1435. enc->time_base.num/g, enc->time_base.den/g);
  1436. }
  1437. }
  1438. if (encode) {
  1439. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1440. ", q=%d-%d", enc->qmin, enc->qmax);
  1441. }
  1442. break;
  1443. case AVMEDIA_TYPE_AUDIO:
  1444. if (enc->sample_rate) {
  1445. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1446. ", %d Hz", enc->sample_rate);
  1447. }
  1448. av_strlcat(buf, ", ", buf_size);
  1449. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1450. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1451. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1452. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1453. }
  1454. break;
  1455. default:
  1456. return;
  1457. }
  1458. if (encode) {
  1459. if (enc->flags & CODEC_FLAG_PASS1)
  1460. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1461. ", pass 1");
  1462. if (enc->flags & CODEC_FLAG_PASS2)
  1463. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1464. ", pass 2");
  1465. }
  1466. bitrate = get_bit_rate(enc);
  1467. if (bitrate != 0) {
  1468. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1469. ", %d kb/s", bitrate / 1000);
  1470. }
  1471. }
  1472. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1473. {
  1474. const AVProfile *p;
  1475. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1476. return NULL;
  1477. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1478. if (p->profile == profile)
  1479. return p->name;
  1480. return NULL;
  1481. }
  1482. unsigned avcodec_version( void )
  1483. {
  1484. // av_assert0(CODEC_ID_V410==164);
  1485. av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
  1486. av_assert0(CODEC_ID_ADPCM_G722==69660);
  1487. // av_assert0(CODEC_ID_BMV_AUDIO==86071);
  1488. av_assert0(CODEC_ID_SRT==94216);
  1489. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1490. return LIBAVCODEC_VERSION_INT;
  1491. }
  1492. const char *avcodec_configuration(void)
  1493. {
  1494. return FFMPEG_CONFIGURATION;
  1495. }
  1496. const char *avcodec_license(void)
  1497. {
  1498. #define LICENSE_PREFIX "libavcodec license: "
  1499. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1500. }
  1501. void avcodec_flush_buffers(AVCodecContext *avctx)
  1502. {
  1503. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1504. ff_thread_flush(avctx);
  1505. else if(avctx->codec->flush)
  1506. avctx->codec->flush(avctx);
  1507. }
  1508. static void video_free_buffers(AVCodecContext *s)
  1509. {
  1510. AVCodecInternal *avci = s->internal;
  1511. int i, j;
  1512. if (!avci->buffer)
  1513. return;
  1514. if (avci->buffer_count)
  1515. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1516. avci->buffer_count);
  1517. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1518. InternalBuffer *buf = &avci->buffer[i];
  1519. for(j=0; j<4; j++){
  1520. av_freep(&buf->base[j]);
  1521. buf->data[j]= NULL;
  1522. }
  1523. }
  1524. av_freep(&avci->buffer);
  1525. avci->buffer_count=0;
  1526. }
  1527. static void audio_free_buffers(AVCodecContext *avctx)
  1528. {
  1529. AVCodecInternal *avci = avctx->internal;
  1530. InternalBuffer *buf;
  1531. if (!avci->buffer)
  1532. return;
  1533. buf = avci->buffer;
  1534. if (buf->extended_data) {
  1535. av_free(buf->extended_data[0]);
  1536. if (buf->extended_data != buf->data)
  1537. av_freep(&buf->extended_data);
  1538. }
  1539. av_freep(&avci->buffer);
  1540. }
  1541. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1542. {
  1543. switch (avctx->codec_type) {
  1544. case AVMEDIA_TYPE_VIDEO:
  1545. video_free_buffers(avctx);
  1546. break;
  1547. case AVMEDIA_TYPE_AUDIO:
  1548. audio_free_buffers(avctx);
  1549. break;
  1550. default:
  1551. break;
  1552. }
  1553. }
  1554. int av_get_bits_per_sample(enum CodecID codec_id){
  1555. switch(codec_id){
  1556. case CODEC_ID_ADPCM_SBPRO_2:
  1557. return 2;
  1558. case CODEC_ID_ADPCM_SBPRO_3:
  1559. return 3;
  1560. case CODEC_ID_ADPCM_SBPRO_4:
  1561. case CODEC_ID_ADPCM_CT:
  1562. case CODEC_ID_ADPCM_IMA_APC:
  1563. case CODEC_ID_ADPCM_IMA_WAV:
  1564. case CODEC_ID_ADPCM_IMA_QT:
  1565. case CODEC_ID_ADPCM_SWF:
  1566. case CODEC_ID_ADPCM_MS:
  1567. case CODEC_ID_ADPCM_YAMAHA:
  1568. case CODEC_ID_ADPCM_G722:
  1569. return 4;
  1570. case CODEC_ID_PCM_ALAW:
  1571. case CODEC_ID_PCM_MULAW:
  1572. case CODEC_ID_PCM_S8:
  1573. case CODEC_ID_PCM_U8:
  1574. case CODEC_ID_PCM_ZORK:
  1575. return 8;
  1576. case CODEC_ID_PCM_S16BE:
  1577. case CODEC_ID_PCM_S16LE:
  1578. case CODEC_ID_PCM_S16LE_PLANAR:
  1579. case CODEC_ID_PCM_U16BE:
  1580. case CODEC_ID_PCM_U16LE:
  1581. return 16;
  1582. case CODEC_ID_PCM_S24DAUD:
  1583. case CODEC_ID_PCM_S24BE:
  1584. case CODEC_ID_PCM_S24LE:
  1585. case CODEC_ID_PCM_U24BE:
  1586. case CODEC_ID_PCM_U24LE:
  1587. return 24;
  1588. case CODEC_ID_PCM_S32BE:
  1589. case CODEC_ID_PCM_S32LE:
  1590. case CODEC_ID_PCM_U32BE:
  1591. case CODEC_ID_PCM_U32LE:
  1592. case CODEC_ID_PCM_F32BE:
  1593. case CODEC_ID_PCM_F32LE:
  1594. return 32;
  1595. case CODEC_ID_PCM_F64BE:
  1596. case CODEC_ID_PCM_F64LE:
  1597. return 64;
  1598. default:
  1599. return 0;
  1600. }
  1601. }
  1602. #if !HAVE_THREADS
  1603. int ff_thread_init(AVCodecContext *s){
  1604. return -1;
  1605. }
  1606. #endif
  1607. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1608. {
  1609. unsigned int n = 0;
  1610. while(v >= 0xff) {
  1611. *s++ = 0xff;
  1612. v -= 0xff;
  1613. n++;
  1614. }
  1615. *s = v;
  1616. n++;
  1617. return n;
  1618. }
  1619. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1620. int i;
  1621. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1622. return i;
  1623. }
  1624. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1625. {
  1626. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1627. "version to the newest one from Git. If the problem still "
  1628. "occurs, it means that your file has a feature which has not "
  1629. "been implemented.\n", feature);
  1630. if(want_sample)
  1631. av_log_ask_for_sample(avc, NULL);
  1632. }
  1633. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1634. {
  1635. va_list argument_list;
  1636. va_start(argument_list, msg);
  1637. if (msg)
  1638. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1639. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1640. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1641. "and contact the ffmpeg-devel mailing list.\n");
  1642. va_end(argument_list);
  1643. }
  1644. static AVHWAccel *first_hwaccel = NULL;
  1645. void av_register_hwaccel(AVHWAccel *hwaccel)
  1646. {
  1647. AVHWAccel **p = &first_hwaccel;
  1648. while (*p)
  1649. p = &(*p)->next;
  1650. *p = hwaccel;
  1651. hwaccel->next = NULL;
  1652. }
  1653. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1654. {
  1655. return hwaccel ? hwaccel->next : first_hwaccel;
  1656. }
  1657. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1658. {
  1659. AVHWAccel *hwaccel=NULL;
  1660. while((hwaccel= av_hwaccel_next(hwaccel))){
  1661. if ( hwaccel->id == codec_id
  1662. && hwaccel->pix_fmt == pix_fmt)
  1663. return hwaccel;
  1664. }
  1665. return NULL;
  1666. }
  1667. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1668. {
  1669. if (ff_lockmgr_cb) {
  1670. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1671. return -1;
  1672. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1673. return -1;
  1674. }
  1675. ff_lockmgr_cb = cb;
  1676. if (ff_lockmgr_cb) {
  1677. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1678. return -1;
  1679. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1680. return -1;
  1681. }
  1682. return 0;
  1683. }
  1684. int avpriv_lock_avformat(void)
  1685. {
  1686. if (ff_lockmgr_cb) {
  1687. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1688. return -1;
  1689. }
  1690. return 0;
  1691. }
  1692. int avpriv_unlock_avformat(void)
  1693. {
  1694. if (ff_lockmgr_cb) {
  1695. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1696. return -1;
  1697. }
  1698. return 0;
  1699. }
  1700. unsigned int avpriv_toupper4(unsigned int x)
  1701. {
  1702. return toupper( x &0xFF)
  1703. + (toupper((x>>8 )&0xFF)<<8 )
  1704. + (toupper((x>>16)&0xFF)<<16)
  1705. + (toupper((x>>24)&0xFF)<<24);
  1706. }
  1707. #if !HAVE_THREADS
  1708. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1709. {
  1710. f->owner = avctx;
  1711. ff_init_buffer_info(avctx, f);
  1712. return avctx->get_buffer(avctx, f);
  1713. }
  1714. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1715. {
  1716. f->owner->release_buffer(f->owner, f);
  1717. }
  1718. void ff_thread_finish_setup(AVCodecContext *avctx)
  1719. {
  1720. }
  1721. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1722. {
  1723. }
  1724. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1725. {
  1726. }
  1727. #endif
  1728. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  1729. {
  1730. AVCodec *c= avcodec_find_decoder(codec_id);
  1731. if(!c)
  1732. c= avcodec_find_encoder(codec_id);
  1733. if(c)
  1734. return c->type;
  1735. if (codec_id <= CODEC_ID_NONE)
  1736. return AVMEDIA_TYPE_UNKNOWN;
  1737. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  1738. return AVMEDIA_TYPE_VIDEO;
  1739. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  1740. return AVMEDIA_TYPE_AUDIO;
  1741. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  1742. return AVMEDIA_TYPE_SUBTITLE;
  1743. return AVMEDIA_TYPE_UNKNOWN;
  1744. }
  1745. int avcodec_is_open(AVCodecContext *s)
  1746. {
  1747. return !!s->internal;
  1748. }