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.

1497 lines
44KB

  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/avstring.h"
  27. #include "libavutil/crc.h"
  28. #include "libavutil/mathematics.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/audioconvert.h"
  31. #include "libavutil/imgutils.h"
  32. #include "libavutil/samplefmt.h"
  33. #include "libavutil/dict.h"
  34. #include "avcodec.h"
  35. #include "dsputil.h"
  36. #include "libavutil/opt.h"
  37. #include "imgconvert.h"
  38. #include "thread.h"
  39. #include "audioconvert.h"
  40. #include "internal.h"
  41. #include <stdlib.h>
  42. #include <stdarg.h>
  43. #include <limits.h>
  44. #include <float.h>
  45. static int volatile entangled_thread_counter=0;
  46. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  47. static void *codec_mutex;
  48. static void *avformat_mutex;
  49. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  50. {
  51. if(min_size < *size)
  52. return ptr;
  53. min_size= FFMAX(17*min_size/16 + 32, min_size);
  54. ptr= av_realloc(ptr, min_size);
  55. 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
  56. min_size= 0;
  57. *size= min_size;
  58. return ptr;
  59. }
  60. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  61. {
  62. void **p = ptr;
  63. if (min_size < *size)
  64. return;
  65. min_size= FFMAX(17*min_size/16 + 32, min_size);
  66. av_free(*p);
  67. *p = av_malloc(min_size);
  68. if (!*p) min_size = 0;
  69. *size= min_size;
  70. }
  71. /* encoder management */
  72. static AVCodec *first_avcodec = NULL;
  73. AVCodec *av_codec_next(AVCodec *c){
  74. if(c) return c->next;
  75. else return first_avcodec;
  76. }
  77. #if !FF_API_AVCODEC_INIT
  78. static
  79. #endif
  80. void avcodec_init(void)
  81. {
  82. static int initialized = 0;
  83. if (initialized != 0)
  84. return;
  85. initialized = 1;
  86. dsputil_static_init();
  87. }
  88. void avcodec_register(AVCodec *codec)
  89. {
  90. AVCodec **p;
  91. avcodec_init();
  92. p = &first_avcodec;
  93. while (*p != NULL) p = &(*p)->next;
  94. *p = codec;
  95. codec->next = NULL;
  96. if (codec->init_static_data)
  97. codec->init_static_data(codec);
  98. }
  99. unsigned avcodec_get_edge_width(void)
  100. {
  101. return EDGE_WIDTH;
  102. }
  103. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  104. s->coded_width = width;
  105. s->coded_height= height;
  106. s->width = -((-width )>>s->lowres);
  107. s->height= -((-height)>>s->lowres);
  108. }
  109. #define INTERNAL_BUFFER_SIZE (32+1)
  110. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
  111. int w_align= 1;
  112. int h_align= 1;
  113. switch(s->pix_fmt){
  114. case PIX_FMT_YUV420P:
  115. case PIX_FMT_YUYV422:
  116. case PIX_FMT_UYVY422:
  117. case PIX_FMT_YUV422P:
  118. case PIX_FMT_YUV440P:
  119. case PIX_FMT_YUV444P:
  120. case PIX_FMT_GBRP:
  121. case PIX_FMT_GRAY8:
  122. case PIX_FMT_GRAY16BE:
  123. case PIX_FMT_GRAY16LE:
  124. case PIX_FMT_YUVJ420P:
  125. case PIX_FMT_YUVJ422P:
  126. case PIX_FMT_YUVJ440P:
  127. case PIX_FMT_YUVJ444P:
  128. case PIX_FMT_YUVA420P:
  129. case PIX_FMT_YUV420P9LE:
  130. case PIX_FMT_YUV420P9BE:
  131. case PIX_FMT_YUV420P10LE:
  132. case PIX_FMT_YUV420P10BE:
  133. case PIX_FMT_YUV422P9LE:
  134. case PIX_FMT_YUV422P9BE:
  135. case PIX_FMT_YUV422P10LE:
  136. case PIX_FMT_YUV422P10BE:
  137. case PIX_FMT_YUV444P9LE:
  138. case PIX_FMT_YUV444P9BE:
  139. case PIX_FMT_YUV444P10LE:
  140. case PIX_FMT_YUV444P10BE:
  141. case PIX_FMT_GBRP9LE:
  142. case PIX_FMT_GBRP9BE:
  143. case PIX_FMT_GBRP10LE:
  144. case PIX_FMT_GBRP10BE:
  145. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  146. h_align= 16;
  147. if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP || s->codec_id == CODEC_ID_H264 || s->codec_id == CODEC_ID_PRORES)
  148. h_align= 32; // interlaced is rounded up to 2 MBs
  149. break;
  150. case PIX_FMT_YUV411P:
  151. case PIX_FMT_UYYVYY411:
  152. w_align=32;
  153. h_align=8;
  154. break;
  155. case PIX_FMT_YUV410P:
  156. if(s->codec_id == CODEC_ID_SVQ1){
  157. w_align=64;
  158. h_align=64;
  159. }
  160. case PIX_FMT_RGB555:
  161. if(s->codec_id == CODEC_ID_RPZA){
  162. w_align=4;
  163. h_align=4;
  164. }
  165. case PIX_FMT_PAL8:
  166. case PIX_FMT_BGR8:
  167. case PIX_FMT_RGB8:
  168. if(s->codec_id == CODEC_ID_SMC){
  169. w_align=4;
  170. h_align=4;
  171. }
  172. break;
  173. case PIX_FMT_BGR24:
  174. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  175. w_align=4;
  176. h_align=4;
  177. }
  178. break;
  179. default:
  180. w_align= 1;
  181. h_align= 1;
  182. break;
  183. }
  184. if(s->codec_id == CODEC_ID_IFF_ILBM || s->codec_id == CODEC_ID_IFF_BYTERUN1){
  185. w_align= FFMAX(w_align, 8);
  186. }
  187. *width = FFALIGN(*width , w_align);
  188. *height= FFALIGN(*height, h_align);
  189. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  190. *height+=2; // some of the optimized chroma MC reads one line too much
  191. // which is also done in mpeg decoders with lowres > 0
  192. linesize_align[0] =
  193. linesize_align[1] =
  194. linesize_align[2] =
  195. linesize_align[3] = STRIDE_ALIGN;
  196. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  197. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  198. //picture size unneccessarily in some cases. The solution here is not
  199. //pretty and better ideas are welcome!
  200. #if HAVE_MMX
  201. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  202. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  203. s->codec_id == CODEC_ID_VP6A || s->codec_id == CODEC_ID_DIRAC) {
  204. linesize_align[0] =
  205. linesize_align[1] =
  206. linesize_align[2] = 16;
  207. }
  208. #endif
  209. }
  210. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  211. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  212. int linesize_align[4];
  213. int align;
  214. avcodec_align_dimensions2(s, width, height, linesize_align);
  215. align = FFMAX(linesize_align[0], linesize_align[3]);
  216. linesize_align[1] <<= chroma_shift;
  217. linesize_align[2] <<= chroma_shift;
  218. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  219. *width=FFALIGN(*width, align);
  220. }
  221. void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
  222. {
  223. if (s->pkt) {
  224. pic->pkt_pts = s->pkt->pts;
  225. pic->pkt_pos = s->pkt->pos;
  226. } else {
  227. pic->pkt_pts = AV_NOPTS_VALUE;
  228. pic->pkt_pos = -1;
  229. }
  230. pic->reordered_opaque= s->reordered_opaque;
  231. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  232. pic->width = s->width;
  233. pic->height = s->height;
  234. pic->format = s->pix_fmt;
  235. }
  236. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  237. int i;
  238. int w= s->width;
  239. int h= s->height;
  240. InternalBuffer *buf;
  241. int *picture_number;
  242. AVCodecInternal *avci = s->internal;
  243. if(pic->data[0]!=NULL) {
  244. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  245. return -1;
  246. }
  247. if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  248. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  249. return -1;
  250. }
  251. if(av_image_check_size(w, h, 0, s))
  252. return -1;
  253. if (!avci->buffer) {
  254. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
  255. sizeof(InternalBuffer));
  256. }
  257. buf = &avci->buffer[avci->buffer_count];
  258. picture_number = &(avci->buffer[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  259. (*picture_number)++;
  260. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  261. if(s->active_thread_type&FF_THREAD_FRAME) {
  262. av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
  263. return -1;
  264. }
  265. for(i=0; i<4; i++){
  266. av_freep(&buf->base[i]);
  267. buf->data[i]= NULL;
  268. }
  269. }
  270. if(buf->base[0]){
  271. pic->age= *picture_number - buf->last_pic_num;
  272. buf->last_pic_num= *picture_number;
  273. }else{
  274. int h_chroma_shift, v_chroma_shift;
  275. int size[4] = {0};
  276. int tmpsize;
  277. int unaligned;
  278. AVPicture picture;
  279. int stride_align[4];
  280. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  281. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  282. avcodec_align_dimensions2(s, &w, &h, stride_align);
  283. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  284. w+= EDGE_WIDTH*2;
  285. h+= EDGE_WIDTH*2;
  286. }
  287. do {
  288. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  289. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  290. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  291. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  292. w += w & ~(w-1);
  293. unaligned = 0;
  294. for (i=0; i<4; i++){
  295. unaligned |= picture.linesize[i] % stride_align[i];
  296. }
  297. } while (unaligned);
  298. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  299. if (tmpsize < 0)
  300. return -1;
  301. for (i=0; i<3 && picture.data[i+1]; i++)
  302. size[i] = picture.data[i+1] - picture.data[i];
  303. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  304. buf->last_pic_num= -256*256*256*64;
  305. memset(buf->base, 0, sizeof(buf->base));
  306. memset(buf->data, 0, sizeof(buf->data));
  307. for(i=0; i<4 && size[i]; i++){
  308. const int h_shift= i==0 ? 0 : h_chroma_shift;
  309. const int v_shift= i==0 ? 0 : v_chroma_shift;
  310. buf->linesize[i]= picture.linesize[i];
  311. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  312. if(buf->base[i]==NULL) return -1;
  313. memset(buf->base[i], 128, size[i]);
  314. // no edge if EDGE EMU or not planar YUV
  315. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  316. buf->data[i] = buf->base[i];
  317. else
  318. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
  319. }
  320. if(size[1] && !size[2])
  321. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  322. buf->width = s->width;
  323. buf->height = s->height;
  324. buf->pix_fmt= s->pix_fmt;
  325. pic->age= 256*256*256*64;
  326. }
  327. pic->type= FF_BUFFER_TYPE_INTERNAL;
  328. for(i=0; i<4; i++){
  329. pic->base[i]= buf->base[i];
  330. pic->data[i]= buf->data[i];
  331. pic->linesize[i]= buf->linesize[i];
  332. }
  333. avci->buffer_count++;
  334. if (s->pkt) {
  335. pic->pkt_pts = s->pkt->pts;
  336. pic->pkt_pos = s->pkt->pos;
  337. } else {
  338. pic->pkt_pts = AV_NOPTS_VALUE;
  339. pic->pkt_pos = -1;
  340. }
  341. pic->reordered_opaque= s->reordered_opaque;
  342. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  343. pic->width = s->width;
  344. pic->height = s->height;
  345. pic->format = s->pix_fmt;
  346. if(s->debug&FF_DEBUG_BUFFERS)
  347. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  348. "buffers used\n", pic, avci->buffer_count);
  349. return 0;
  350. }
  351. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  352. int i;
  353. InternalBuffer *buf, *last;
  354. AVCodecInternal *avci = s->internal;
  355. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  356. assert(avci->buffer_count);
  357. if (avci->buffer) {
  358. buf = NULL; /* avoids warning */
  359. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  360. buf = &avci->buffer[i];
  361. if (buf->data[0] == pic->data[0])
  362. break;
  363. }
  364. assert(i < avci->buffer_count);
  365. avci->buffer_count--;
  366. last = &avci->buffer[avci->buffer_count];
  367. FFSWAP(InternalBuffer, *buf, *last);
  368. }
  369. for(i=0; i<4; i++){
  370. pic->data[i]=NULL;
  371. // pic->base[i]=NULL;
  372. }
  373. //printf("R%X\n", pic->opaque);
  374. if(s->debug&FF_DEBUG_BUFFERS)
  375. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  376. "buffers used\n", pic, avci->buffer_count);
  377. }
  378. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  379. AVFrame temp_pic;
  380. int i;
  381. /* If no picture return a new buffer */
  382. if(pic->data[0] == NULL) {
  383. /* We will copy from buffer, so must be readable */
  384. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  385. return s->get_buffer(s, pic);
  386. }
  387. /* If internal buffer type return the same buffer */
  388. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  389. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  390. else pic->pkt_pts= AV_NOPTS_VALUE;
  391. pic->reordered_opaque= s->reordered_opaque;
  392. return 0;
  393. }
  394. /*
  395. * Not internal type and reget_buffer not overridden, emulate cr buffer
  396. */
  397. temp_pic = *pic;
  398. for(i = 0; i < 4; i++)
  399. pic->data[i] = pic->base[i] = NULL;
  400. pic->opaque = NULL;
  401. /* Allocate new frame */
  402. if (s->get_buffer(s, pic))
  403. return -1;
  404. /* Copy image data from old buffer to new buffer */
  405. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  406. s->height);
  407. s->release_buffer(s, &temp_pic); // Release old frame
  408. return 0;
  409. }
  410. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  411. int i;
  412. for(i=0; i<count; i++){
  413. int r= func(c, (char*)arg + i*size);
  414. if(ret) ret[i]= r;
  415. }
  416. return 0;
  417. }
  418. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  419. int i;
  420. for(i=0; i<count; i++){
  421. int r= func(c, arg, i, 0);
  422. if(ret) ret[i]= r;
  423. }
  424. return 0;
  425. }
  426. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  427. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  428. ++fmt;
  429. return fmt[0];
  430. }
  431. void avcodec_get_frame_defaults(AVFrame *pic){
  432. memset(pic, 0, sizeof(AVFrame));
  433. pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
  434. pic->pkt_pos = -1;
  435. pic->key_frame= 1;
  436. pic->sample_aspect_ratio = (AVRational){0, 1};
  437. pic->format = -1; /* unknown */
  438. }
  439. AVFrame *avcodec_alloc_frame(void){
  440. AVFrame *pic= av_malloc(sizeof(AVFrame));
  441. if(pic==NULL) return NULL;
  442. avcodec_get_frame_defaults(pic);
  443. return pic;
  444. }
  445. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  446. {
  447. memset(sub, 0, sizeof(*sub));
  448. sub->pts = AV_NOPTS_VALUE;
  449. }
  450. #if FF_API_AVCODEC_OPEN
  451. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  452. {
  453. return avcodec_open2(avctx, codec, NULL);
  454. }
  455. #endif
  456. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  457. {
  458. int ret = 0;
  459. AVDictionary *tmp = NULL;
  460. if (options)
  461. av_dict_copy(&tmp, *options, 0);
  462. /* If there is a user-supplied mutex locking routine, call it. */
  463. if (ff_lockmgr_cb) {
  464. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  465. return -1;
  466. }
  467. entangled_thread_counter++;
  468. if(entangled_thread_counter != 1){
  469. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  470. ret = -1;
  471. goto end;
  472. }
  473. if(avctx->codec || !codec) {
  474. ret = AVERROR(EINVAL);
  475. goto end;
  476. }
  477. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  478. if (!avctx->internal) {
  479. ret = AVERROR(ENOMEM);
  480. goto end;
  481. }
  482. if (codec->priv_data_size > 0) {
  483. if(!avctx->priv_data){
  484. avctx->priv_data = av_mallocz(codec->priv_data_size);
  485. if (!avctx->priv_data) {
  486. ret = AVERROR(ENOMEM);
  487. goto end;
  488. }
  489. if (codec->priv_class) {
  490. *(AVClass**)avctx->priv_data= codec->priv_class;
  491. av_opt_set_defaults(avctx->priv_data);
  492. }
  493. }
  494. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  495. goto free_and_end;
  496. } else {
  497. avctx->priv_data = NULL;
  498. }
  499. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  500. goto free_and_end;
  501. //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
  502. if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
  503. if(avctx->coded_width && avctx->coded_height)
  504. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  505. else if(avctx->width && avctx->height)
  506. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  507. }
  508. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  509. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  510. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  511. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  512. avcodec_set_dimensions(avctx, 0, 0);
  513. }
  514. /* if the decoder init function was already called previously,
  515. free the already allocated subtitle_header before overwriting it */
  516. if (codec->decode)
  517. av_freep(&avctx->subtitle_header);
  518. #define SANE_NB_CHANNELS 128U
  519. if (avctx->channels > SANE_NB_CHANNELS) {
  520. ret = AVERROR(EINVAL);
  521. goto free_and_end;
  522. }
  523. avctx->codec = codec;
  524. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  525. avctx->codec_id == CODEC_ID_NONE) {
  526. avctx->codec_type = codec->type;
  527. avctx->codec_id = codec->id;
  528. }
  529. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  530. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  531. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  532. ret = AVERROR(EINVAL);
  533. goto free_and_end;
  534. }
  535. avctx->frame_number = 0;
  536. #if FF_API_ER
  537. av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
  538. avctx->error_recognition, avctx->err_recognition);
  539. switch(avctx->error_recognition){
  540. case FF_ER_EXPLODE : avctx->err_recognition |= AV_EF_EXPLODE | AV_EF_COMPLIANT | AV_EF_CAREFUL;
  541. break;
  542. case FF_ER_VERY_AGGRESSIVE:
  543. case FF_ER_AGGRESSIVE : avctx->err_recognition |= AV_EF_AGGRESSIVE;
  544. case FF_ER_COMPLIANT : avctx->err_recognition |= AV_EF_COMPLIANT;
  545. case FF_ER_CAREFUL : avctx->err_recognition |= AV_EF_CAREFUL;
  546. }
  547. av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
  548. avctx->error_recognition, avctx->err_recognition);
  549. #endif
  550. if (!HAVE_THREADS)
  551. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  552. if (HAVE_THREADS && !avctx->thread_opaque) {
  553. ret = ff_thread_init(avctx);
  554. if (ret < 0) {
  555. goto free_and_end;
  556. }
  557. }
  558. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  559. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  560. avctx->codec->max_lowres);
  561. ret = AVERROR(EINVAL);
  562. goto free_and_end;
  563. }
  564. if (avctx->codec->encode) {
  565. int i;
  566. if (avctx->codec->sample_fmts) {
  567. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  568. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  569. break;
  570. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  571. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  572. ret = AVERROR(EINVAL);
  573. goto free_and_end;
  574. }
  575. }
  576. if (avctx->codec->supported_samplerates) {
  577. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  578. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  579. break;
  580. if (avctx->codec->supported_samplerates[i] == 0) {
  581. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  582. ret = AVERROR(EINVAL);
  583. goto free_and_end;
  584. }
  585. }
  586. if (avctx->codec->channel_layouts) {
  587. if (!avctx->channel_layout) {
  588. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  589. } else {
  590. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  591. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  592. break;
  593. if (avctx->codec->channel_layouts[i] == 0) {
  594. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  595. ret = AVERROR(EINVAL);
  596. goto free_and_end;
  597. }
  598. }
  599. }
  600. if (avctx->channel_layout && avctx->channels) {
  601. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  602. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  603. ret = AVERROR(EINVAL);
  604. goto free_and_end;
  605. }
  606. } else if (avctx->channel_layout) {
  607. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  608. }
  609. }
  610. avctx->pts_correction_num_faulty_pts =
  611. avctx->pts_correction_num_faulty_dts = 0;
  612. avctx->pts_correction_last_pts =
  613. avctx->pts_correction_last_dts = INT64_MIN;
  614. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  615. ret = avctx->codec->init(avctx);
  616. if (ret < 0) {
  617. goto free_and_end;
  618. }
  619. }
  620. ret=0;
  621. end:
  622. entangled_thread_counter--;
  623. /* Release any user-supplied mutex. */
  624. if (ff_lockmgr_cb) {
  625. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  626. }
  627. if (options) {
  628. av_dict_free(options);
  629. *options = tmp;
  630. }
  631. return ret;
  632. free_and_end:
  633. av_dict_free(&tmp);
  634. av_freep(&avctx->priv_data);
  635. av_freep(&avctx->internal);
  636. avctx->codec= NULL;
  637. goto end;
  638. }
  639. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  640. const short *samples)
  641. {
  642. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  643. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  644. return -1;
  645. }
  646. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  647. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  648. avctx->frame_number++;
  649. return ret;
  650. }else
  651. return 0;
  652. }
  653. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  654. const AVFrame *pict)
  655. {
  656. if(buf_size < FF_MIN_BUFFER_SIZE){
  657. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  658. return -1;
  659. }
  660. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  661. return -1;
  662. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  663. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  664. avctx->frame_number++;
  665. emms_c(); //needed to avoid an emms_c() call before every return;
  666. return ret;
  667. }else
  668. return 0;
  669. }
  670. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  671. const AVSubtitle *sub)
  672. {
  673. int ret;
  674. if(sub->start_display_time) {
  675. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  676. return -1;
  677. }
  678. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  679. avctx->frame_number++;
  680. return ret;
  681. }
  682. /**
  683. * Attempt to guess proper monotonic timestamps for decoded video frames
  684. * which might have incorrect times. Input timestamps may wrap around, in
  685. * which case the output will as well.
  686. *
  687. * @param pts the pts field of the decoded AVPacket, as passed through
  688. * AVFrame.pkt_pts
  689. * @param dts the dts field of the decoded AVPacket
  690. * @return one of the input values, may be AV_NOPTS_VALUE
  691. */
  692. static int64_t guess_correct_pts(AVCodecContext *ctx,
  693. int64_t reordered_pts, int64_t dts)
  694. {
  695. int64_t pts = AV_NOPTS_VALUE;
  696. if (dts != AV_NOPTS_VALUE) {
  697. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  698. ctx->pts_correction_last_dts = dts;
  699. }
  700. if (reordered_pts != AV_NOPTS_VALUE) {
  701. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  702. ctx->pts_correction_last_pts = reordered_pts;
  703. }
  704. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  705. && reordered_pts != AV_NOPTS_VALUE)
  706. pts = reordered_pts;
  707. else
  708. pts = dts;
  709. return pts;
  710. }
  711. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  712. int *got_picture_ptr,
  713. AVPacket *avpkt)
  714. {
  715. int ret;
  716. *got_picture_ptr= 0;
  717. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  718. return -1;
  719. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  720. av_packet_split_side_data(avpkt);
  721. avctx->pkt = avpkt;
  722. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  723. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  724. avpkt);
  725. else {
  726. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  727. avpkt);
  728. picture->pkt_dts= avpkt->dts;
  729. if(!avctx->has_b_frames){
  730. picture->pkt_pos= avpkt->pos;
  731. }
  732. //FIXME these should be under if(!avctx->has_b_frames)
  733. if (!picture->sample_aspect_ratio.num)
  734. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  735. if (!picture->width)
  736. picture->width = avctx->width;
  737. if (!picture->height)
  738. picture->height = avctx->height;
  739. if (picture->format == PIX_FMT_NONE)
  740. picture->format = avctx->pix_fmt;
  741. }
  742. emms_c(); //needed to avoid an emms_c() call before every return;
  743. if (*got_picture_ptr){
  744. avctx->frame_number++;
  745. picture->best_effort_timestamp = guess_correct_pts(avctx,
  746. picture->pkt_pts,
  747. picture->pkt_dts);
  748. }
  749. }else
  750. ret= 0;
  751. return ret;
  752. }
  753. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  754. int *frame_size_ptr,
  755. AVPacket *avpkt)
  756. {
  757. int ret;
  758. avctx->pkt = avpkt;
  759. if (!avpkt->data && avpkt->size) {
  760. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  761. return AVERROR(EINVAL);
  762. }
  763. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  764. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  765. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  766. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  767. return -1;
  768. }
  769. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  770. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  771. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  772. return -1;
  773. }
  774. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  775. avctx->frame_number++;
  776. }else{
  777. ret= 0;
  778. *frame_size_ptr=0;
  779. }
  780. return ret;
  781. }
  782. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  783. int *got_sub_ptr,
  784. AVPacket *avpkt)
  785. {
  786. int ret;
  787. avctx->pkt = avpkt;
  788. *got_sub_ptr = 0;
  789. avcodec_get_subtitle_defaults(sub);
  790. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  791. if (*got_sub_ptr)
  792. avctx->frame_number++;
  793. return ret;
  794. }
  795. void avsubtitle_free(AVSubtitle *sub)
  796. {
  797. int i;
  798. for (i = 0; i < sub->num_rects; i++)
  799. {
  800. av_freep(&sub->rects[i]->pict.data[0]);
  801. av_freep(&sub->rects[i]->pict.data[1]);
  802. av_freep(&sub->rects[i]->pict.data[2]);
  803. av_freep(&sub->rects[i]->pict.data[3]);
  804. av_freep(&sub->rects[i]->text);
  805. av_freep(&sub->rects[i]->ass);
  806. av_freep(&sub->rects[i]);
  807. }
  808. av_freep(&sub->rects);
  809. memset(sub, 0, sizeof(AVSubtitle));
  810. }
  811. av_cold int avcodec_close(AVCodecContext *avctx)
  812. {
  813. /* If there is a user-supplied mutex locking routine, call it. */
  814. if (ff_lockmgr_cb) {
  815. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  816. return -1;
  817. }
  818. entangled_thread_counter++;
  819. if(entangled_thread_counter != 1){
  820. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  821. entangled_thread_counter--;
  822. return -1;
  823. }
  824. if (HAVE_THREADS && avctx->thread_opaque)
  825. ff_thread_free(avctx);
  826. if (avctx->codec && avctx->codec->close)
  827. avctx->codec->close(avctx);
  828. avcodec_default_free_buffers(avctx);
  829. avctx->coded_frame = NULL;
  830. av_freep(&avctx->internal);
  831. if (avctx->codec && avctx->codec->priv_class)
  832. av_opt_free(avctx->priv_data);
  833. av_opt_free(avctx);
  834. av_freep(&avctx->priv_data);
  835. if(avctx->codec && avctx->codec->encode)
  836. av_freep(&avctx->extradata);
  837. avctx->codec = NULL;
  838. avctx->active_thread_type = 0;
  839. entangled_thread_counter--;
  840. /* Release any user-supplied mutex. */
  841. if (ff_lockmgr_cb) {
  842. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  843. }
  844. return 0;
  845. }
  846. static enum CodecID remap_deprecated_codec_id(enum CodecID id)
  847. {
  848. switch(id){
  849. case CODEC_ID_G723_1_DEPRECATED : return CODEC_ID_G723_1;
  850. case CODEC_ID_G729_DEPRECATED : return CODEC_ID_G729;
  851. case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
  852. default : return id;
  853. }
  854. }
  855. AVCodec *avcodec_find_encoder(enum CodecID id)
  856. {
  857. AVCodec *p, *experimental=NULL;
  858. p = first_avcodec;
  859. id= remap_deprecated_codec_id(id);
  860. while (p) {
  861. if (p->encode != NULL && p->id == id) {
  862. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  863. experimental = p;
  864. } else
  865. return p;
  866. }
  867. p = p->next;
  868. }
  869. return experimental;
  870. }
  871. AVCodec *avcodec_find_encoder_by_name(const char *name)
  872. {
  873. AVCodec *p;
  874. if (!name)
  875. return NULL;
  876. p = first_avcodec;
  877. while (p) {
  878. if (p->encode != NULL && strcmp(name,p->name) == 0)
  879. return p;
  880. p = p->next;
  881. }
  882. return NULL;
  883. }
  884. AVCodec *avcodec_find_decoder(enum CodecID id)
  885. {
  886. AVCodec *p, *experimental=NULL;
  887. p = first_avcodec;
  888. id= remap_deprecated_codec_id(id);
  889. while (p) {
  890. if (p->decode != NULL && p->id == id) {
  891. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  892. experimental = p;
  893. } else
  894. return p;
  895. }
  896. p = p->next;
  897. }
  898. return experimental;
  899. }
  900. AVCodec *avcodec_find_decoder_by_name(const char *name)
  901. {
  902. AVCodec *p;
  903. if (!name)
  904. return NULL;
  905. p = first_avcodec;
  906. while (p) {
  907. if (p->decode != NULL && strcmp(name,p->name) == 0)
  908. return p;
  909. p = p->next;
  910. }
  911. return NULL;
  912. }
  913. static int get_bit_rate(AVCodecContext *ctx)
  914. {
  915. int bit_rate;
  916. int bits_per_sample;
  917. switch(ctx->codec_type) {
  918. case AVMEDIA_TYPE_VIDEO:
  919. case AVMEDIA_TYPE_DATA:
  920. case AVMEDIA_TYPE_SUBTITLE:
  921. case AVMEDIA_TYPE_ATTACHMENT:
  922. bit_rate = ctx->bit_rate;
  923. break;
  924. case AVMEDIA_TYPE_AUDIO:
  925. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  926. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  927. break;
  928. default:
  929. bit_rate = 0;
  930. break;
  931. }
  932. return bit_rate;
  933. }
  934. const char *avcodec_get_name(enum CodecID id)
  935. {
  936. AVCodec *codec;
  937. #if !CONFIG_SMALL
  938. switch (id) {
  939. #include "libavcodec/codec_names.h"
  940. }
  941. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  942. #endif
  943. codec = avcodec_find_decoder(id);
  944. if (codec)
  945. return codec->name;
  946. codec = avcodec_find_encoder(id);
  947. if (codec)
  948. return codec->name;
  949. return "unknown_codec";
  950. }
  951. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  952. {
  953. int i, len, ret = 0;
  954. for (i = 0; i < 4; i++) {
  955. len = snprintf(buf, buf_size,
  956. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  957. buf += len;
  958. buf_size = buf_size > len ? buf_size - len : 0;
  959. ret += len;
  960. codec_tag>>=8;
  961. }
  962. return ret;
  963. }
  964. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  965. {
  966. const char *codec_type;
  967. const char *codec_name;
  968. const char *profile = NULL;
  969. AVCodec *p;
  970. int bitrate;
  971. AVRational display_aspect_ratio;
  972. if (!buf || buf_size <= 0)
  973. return;
  974. codec_type = av_get_media_type_string(enc->codec_type);
  975. codec_name = avcodec_get_name(enc->codec_id);
  976. if (enc->profile != FF_PROFILE_UNKNOWN) {
  977. p = encode ? avcodec_find_encoder(enc->codec_id) :
  978. avcodec_find_decoder(enc->codec_id);
  979. if (p)
  980. profile = av_get_profile_name(p, enc->profile);
  981. }
  982. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  983. codec_name, enc->mb_decision ? " (hq)" : "");
  984. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  985. if (profile)
  986. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  987. if (enc->codec_tag) {
  988. char tag_buf[32];
  989. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  990. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  991. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  992. }
  993. switch(enc->codec_type) {
  994. case AVMEDIA_TYPE_VIDEO:
  995. if (enc->pix_fmt != PIX_FMT_NONE) {
  996. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  997. ", %s",
  998. av_get_pix_fmt_name(enc->pix_fmt));
  999. }
  1000. if (enc->width) {
  1001. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1002. ", %dx%d",
  1003. enc->width, enc->height);
  1004. if (enc->sample_aspect_ratio.num) {
  1005. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1006. enc->width*enc->sample_aspect_ratio.num,
  1007. enc->height*enc->sample_aspect_ratio.den,
  1008. 1024*1024);
  1009. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1010. " [SAR %d:%d DAR %d:%d]",
  1011. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1012. display_aspect_ratio.num, display_aspect_ratio.den);
  1013. }
  1014. if(av_log_get_level() >= AV_LOG_DEBUG){
  1015. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1016. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1017. ", %d/%d",
  1018. enc->time_base.num/g, enc->time_base.den/g);
  1019. }
  1020. }
  1021. if (encode) {
  1022. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1023. ", q=%d-%d", enc->qmin, enc->qmax);
  1024. }
  1025. break;
  1026. case AVMEDIA_TYPE_AUDIO:
  1027. if (enc->sample_rate) {
  1028. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1029. ", %d Hz", enc->sample_rate);
  1030. }
  1031. av_strlcat(buf, ", ", buf_size);
  1032. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1033. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1034. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1035. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1036. }
  1037. break;
  1038. default:
  1039. return;
  1040. }
  1041. if (encode) {
  1042. if (enc->flags & CODEC_FLAG_PASS1)
  1043. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1044. ", pass 1");
  1045. if (enc->flags & CODEC_FLAG_PASS2)
  1046. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1047. ", pass 2");
  1048. }
  1049. bitrate = get_bit_rate(enc);
  1050. if (bitrate != 0) {
  1051. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1052. ", %d kb/s", bitrate / 1000);
  1053. }
  1054. }
  1055. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1056. {
  1057. const AVProfile *p;
  1058. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1059. return NULL;
  1060. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1061. if (p->profile == profile)
  1062. return p->name;
  1063. return NULL;
  1064. }
  1065. unsigned avcodec_version( void )
  1066. {
  1067. return LIBAVCODEC_VERSION_INT;
  1068. }
  1069. const char *avcodec_configuration(void)
  1070. {
  1071. return FFMPEG_CONFIGURATION;
  1072. }
  1073. const char *avcodec_license(void)
  1074. {
  1075. #define LICENSE_PREFIX "libavcodec license: "
  1076. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1077. }
  1078. void avcodec_flush_buffers(AVCodecContext *avctx)
  1079. {
  1080. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1081. ff_thread_flush(avctx);
  1082. else if(avctx->codec->flush)
  1083. avctx->codec->flush(avctx);
  1084. }
  1085. void avcodec_default_free_buffers(AVCodecContext *s){
  1086. AVCodecInternal *avci = s->internal;
  1087. int i, j;
  1088. if (!avci->buffer)
  1089. return;
  1090. if (avci->buffer_count)
  1091. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1092. avci->buffer_count);
  1093. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1094. InternalBuffer *buf = &avci->buffer[i];
  1095. for(j=0; j<4; j++){
  1096. av_freep(&buf->base[j]);
  1097. buf->data[j]= NULL;
  1098. }
  1099. }
  1100. av_freep(&avci->buffer);
  1101. avci->buffer_count=0;
  1102. }
  1103. #if FF_API_OLD_FF_PICT_TYPES
  1104. char av_get_pict_type_char(int pict_type){
  1105. return av_get_picture_type_char(pict_type);
  1106. }
  1107. #endif
  1108. int av_get_bits_per_sample(enum CodecID codec_id){
  1109. switch(codec_id){
  1110. case CODEC_ID_ADPCM_SBPRO_2:
  1111. return 2;
  1112. case CODEC_ID_ADPCM_SBPRO_3:
  1113. return 3;
  1114. case CODEC_ID_ADPCM_SBPRO_4:
  1115. case CODEC_ID_ADPCM_CT:
  1116. case CODEC_ID_ADPCM_IMA_WAV:
  1117. case CODEC_ID_ADPCM_IMA_QT:
  1118. case CODEC_ID_ADPCM_SWF:
  1119. case CODEC_ID_ADPCM_MS:
  1120. case CODEC_ID_ADPCM_YAMAHA:
  1121. return 4;
  1122. case CODEC_ID_ADPCM_G722:
  1123. case CODEC_ID_PCM_ALAW:
  1124. case CODEC_ID_PCM_MULAW:
  1125. case CODEC_ID_PCM_S8:
  1126. case CODEC_ID_PCM_U8:
  1127. case CODEC_ID_PCM_ZORK:
  1128. return 8;
  1129. case CODEC_ID_PCM_S16BE:
  1130. case CODEC_ID_PCM_S16LE:
  1131. case CODEC_ID_PCM_S16LE_PLANAR:
  1132. case CODEC_ID_PCM_U16BE:
  1133. case CODEC_ID_PCM_U16LE:
  1134. return 16;
  1135. case CODEC_ID_PCM_S24DAUD:
  1136. case CODEC_ID_PCM_S24BE:
  1137. case CODEC_ID_PCM_S24LE:
  1138. case CODEC_ID_PCM_U24BE:
  1139. case CODEC_ID_PCM_U24LE:
  1140. return 24;
  1141. case CODEC_ID_PCM_S32BE:
  1142. case CODEC_ID_PCM_S32LE:
  1143. case CODEC_ID_PCM_U32BE:
  1144. case CODEC_ID_PCM_U32LE:
  1145. case CODEC_ID_PCM_F32BE:
  1146. case CODEC_ID_PCM_F32LE:
  1147. return 32;
  1148. case CODEC_ID_PCM_F64BE:
  1149. case CODEC_ID_PCM_F64LE:
  1150. return 64;
  1151. default:
  1152. return 0;
  1153. }
  1154. }
  1155. #if FF_API_OLD_SAMPLE_FMT
  1156. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  1157. return av_get_bytes_per_sample(sample_fmt) << 3;
  1158. }
  1159. #endif
  1160. #if !HAVE_THREADS
  1161. int ff_thread_init(AVCodecContext *s){
  1162. return -1;
  1163. }
  1164. #endif
  1165. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1166. {
  1167. unsigned int n = 0;
  1168. while(v >= 0xff) {
  1169. *s++ = 0xff;
  1170. v -= 0xff;
  1171. n++;
  1172. }
  1173. *s = v;
  1174. n++;
  1175. return n;
  1176. }
  1177. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1178. int i;
  1179. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1180. return i;
  1181. }
  1182. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1183. {
  1184. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1185. "version to the newest one from Git. If the problem still "
  1186. "occurs, it means that your file has a feature which has not "
  1187. "been implemented.\n", feature);
  1188. if(want_sample)
  1189. av_log_ask_for_sample(avc, NULL);
  1190. }
  1191. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1192. {
  1193. va_list argument_list;
  1194. va_start(argument_list, msg);
  1195. if (msg)
  1196. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1197. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1198. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1199. "and contact the ffmpeg-devel mailing list.\n");
  1200. va_end(argument_list);
  1201. }
  1202. static AVHWAccel *first_hwaccel = NULL;
  1203. void av_register_hwaccel(AVHWAccel *hwaccel)
  1204. {
  1205. AVHWAccel **p = &first_hwaccel;
  1206. while (*p)
  1207. p = &(*p)->next;
  1208. *p = hwaccel;
  1209. hwaccel->next = NULL;
  1210. }
  1211. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1212. {
  1213. return hwaccel ? hwaccel->next : first_hwaccel;
  1214. }
  1215. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1216. {
  1217. AVHWAccel *hwaccel=NULL;
  1218. while((hwaccel= av_hwaccel_next(hwaccel))){
  1219. if ( hwaccel->id == codec_id
  1220. && hwaccel->pix_fmt == pix_fmt)
  1221. return hwaccel;
  1222. }
  1223. return NULL;
  1224. }
  1225. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1226. {
  1227. if (ff_lockmgr_cb) {
  1228. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1229. return -1;
  1230. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1231. return -1;
  1232. }
  1233. ff_lockmgr_cb = cb;
  1234. if (ff_lockmgr_cb) {
  1235. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1236. return -1;
  1237. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1238. return -1;
  1239. }
  1240. return 0;
  1241. }
  1242. int avpriv_lock_avformat(void)
  1243. {
  1244. if (ff_lockmgr_cb) {
  1245. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1246. return -1;
  1247. }
  1248. return 0;
  1249. }
  1250. int avpriv_unlock_avformat(void)
  1251. {
  1252. if (ff_lockmgr_cb) {
  1253. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1254. return -1;
  1255. }
  1256. return 0;
  1257. }
  1258. unsigned int avpriv_toupper4(unsigned int x)
  1259. {
  1260. return toupper( x &0xFF)
  1261. + (toupper((x>>8 )&0xFF)<<8 )
  1262. + (toupper((x>>16)&0xFF)<<16)
  1263. + (toupper((x>>24)&0xFF)<<24);
  1264. }
  1265. #if !HAVE_THREADS
  1266. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1267. {
  1268. f->owner = avctx;
  1269. ff_init_buffer_info(avctx, f);
  1270. return avctx->get_buffer(avctx, f);
  1271. }
  1272. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1273. {
  1274. f->owner->release_buffer(f->owner, f);
  1275. }
  1276. void ff_thread_finish_setup(AVCodecContext *avctx)
  1277. {
  1278. }
  1279. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1280. {
  1281. }
  1282. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1283. {
  1284. }
  1285. #endif
  1286. #if FF_API_THREAD_INIT
  1287. int avcodec_thread_init(AVCodecContext *s, int thread_count)
  1288. {
  1289. s->thread_count = thread_count;
  1290. return ff_thread_init(s);
  1291. }
  1292. #endif
  1293. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  1294. {
  1295. AVCodec *c= avcodec_find_decoder(codec_id);
  1296. if(!c)
  1297. c= avcodec_find_encoder(codec_id);
  1298. if(c)
  1299. return c->type;
  1300. if (codec_id <= CODEC_ID_NONE)
  1301. return AVMEDIA_TYPE_UNKNOWN;
  1302. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  1303. return AVMEDIA_TYPE_VIDEO;
  1304. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  1305. return AVMEDIA_TYPE_AUDIO;
  1306. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  1307. return AVMEDIA_TYPE_SUBTITLE;
  1308. return AVMEDIA_TYPE_UNKNOWN;
  1309. }