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