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.

1443 lines
42KB

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