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.

589 lines
15KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. #include "dsputil.h"
  21. #include "mpegvideo.h"
  22. void *av_mallocz(unsigned int size)
  23. {
  24. void *ptr;
  25. if(size == 0) fprintf(stderr, "Warning, allocating 0 bytes\n");
  26. ptr = av_malloc(size);
  27. if (!ptr)
  28. return NULL;
  29. memset(ptr, 0, size);
  30. return ptr;
  31. }
  32. /* allocation of static arrays - do not use for normal allocation */
  33. static unsigned int last_static = 0;
  34. static char*** array_static = NULL;
  35. static const unsigned int grow_static = 64; // ^2
  36. void *__av_mallocz_static(void** location, unsigned int size)
  37. {
  38. int l = (last_static + grow_static) & ~(grow_static - 1);
  39. void *ptr = av_mallocz(size);
  40. if (!ptr)
  41. return NULL;
  42. if (location)
  43. {
  44. if (l > last_static)
  45. array_static = realloc(array_static, l);
  46. array_static[last_static++] = (char**) location;
  47. *location = ptr;
  48. }
  49. return ptr;
  50. }
  51. /* free all static arrays and reset pointers to 0 */
  52. void av_free_static()
  53. {
  54. if (array_static)
  55. {
  56. unsigned i;
  57. for (i = 0; i < last_static; i++)
  58. {
  59. free(*array_static[i]);
  60. *array_static[i] = NULL;
  61. }
  62. free(array_static);
  63. array_static = 0;
  64. }
  65. last_static = 0;
  66. }
  67. /* cannot call it directly because of 'void **' casting is not automatic */
  68. void __av_freep(void **ptr)
  69. {
  70. av_free(*ptr);
  71. *ptr = NULL;
  72. }
  73. /* encoder management */
  74. AVCodec *first_avcodec;
  75. void register_avcodec(AVCodec *format)
  76. {
  77. AVCodec **p;
  78. p = &first_avcodec;
  79. while (*p != NULL) p = &(*p)->next;
  80. *p = format;
  81. format->next = NULL;
  82. }
  83. typedef struct DefaultPicOpaque{
  84. int last_pic_num;
  85. uint8_t *data[4];
  86. }DefaultPicOpaque;
  87. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  88. int i;
  89. const int width = s->width;
  90. const int height= s->height;
  91. DefaultPicOpaque *opaque;
  92. assert(pic->data[0]==NULL);
  93. assert(pic->type==0 || pic->type==FF_TYPE_INTERNAL);
  94. if(pic->opaque){
  95. opaque= (DefaultPicOpaque *)pic->opaque;
  96. for(i=0; i<3; i++)
  97. pic->data[i]= opaque->data[i];
  98. // printf("get_buffer %X coded_pic_num:%d last:%d\n", pic->opaque, pic->coded_picture_number, opaque->last_pic_num);
  99. pic->age= pic->coded_picture_number - opaque->last_pic_num;
  100. opaque->last_pic_num= pic->coded_picture_number;
  101. //printf("age: %d %d %d\n", pic->age, c->picture_number, pic->coded_picture_number);
  102. }else{
  103. int align, h_chroma_shift, v_chroma_shift;
  104. int w, h, pixel_size;
  105. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  106. switch(s->pix_fmt){
  107. case PIX_FMT_YUV422:
  108. pixel_size=2;
  109. break;
  110. case PIX_FMT_RGB24:
  111. case PIX_FMT_BGR24:
  112. pixel_size=3;
  113. break;
  114. case PIX_FMT_RGBA32:
  115. pixel_size=4;
  116. break;
  117. default:
  118. pixel_size=1;
  119. }
  120. if(s->codec_id==CODEC_ID_SVQ1) align=63;
  121. else align=15;
  122. w= (width +align)&~align;
  123. h= (height+align)&~align;
  124. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  125. w+= EDGE_WIDTH*2;
  126. h+= EDGE_WIDTH*2;
  127. }
  128. opaque= av_mallocz(sizeof(DefaultPicOpaque));
  129. if(opaque==NULL) return -1;
  130. pic->opaque= opaque;
  131. opaque->last_pic_num= -256*256*256*64;
  132. for(i=0; i<3; i++){
  133. int h_shift= i==0 ? 0 : h_chroma_shift;
  134. int v_shift= i==0 ? 0 : v_chroma_shift;
  135. pic->linesize[i]= pixel_size*w>>h_shift;
  136. pic->base[i]= av_mallocz((pic->linesize[i]*h>>v_shift)+16); //FIXME 16
  137. if(pic->base[i]==NULL) return -1;
  138. memset(pic->base[i], 128, pic->linesize[i]*h>>v_shift);
  139. if(s->flags&CODEC_FLAG_EMU_EDGE)
  140. pic->data[i] = pic->base[i] + 16; //FIXME 16
  141. else
  142. pic->data[i] = pic->base[i] + (pic->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift) + 16; //FIXME 16
  143. opaque->data[i]= pic->data[i];
  144. }
  145. pic->age= 256*256*256*64;
  146. pic->type= FF_BUFFER_TYPE_INTERNAL;
  147. }
  148. return 0;
  149. }
  150. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  151. int i;
  152. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  153. for(i=0; i<3; i++)
  154. pic->data[i]=NULL;
  155. //printf("R%X\n", pic->opaque);
  156. }
  157. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, enum PixelFormat * fmt){
  158. return fmt[0];
  159. }
  160. void avcodec_get_context_defaults(AVCodecContext *s){
  161. s->bit_rate= 800*1000;
  162. s->bit_rate_tolerance= s->bit_rate*10;
  163. s->qmin= 2;
  164. s->qmax= 31;
  165. s->mb_qmin= 2;
  166. s->mb_qmax= 31;
  167. s->rc_eq= "tex^qComp";
  168. s->qcompress= 0.5;
  169. s->max_qdiff= 3;
  170. s->b_quant_factor=1.25;
  171. s->b_quant_offset=1.25;
  172. s->i_quant_factor=-0.8;
  173. s->i_quant_offset=0.0;
  174. s->error_concealment= 3;
  175. s->error_resilience= 1;
  176. s->workaround_bugs= FF_BUG_AUTODETECT;
  177. s->frame_rate = 25 * FRAME_RATE_BASE;
  178. s->gop_size= 50;
  179. s->me_method= ME_EPZS;
  180. s->get_buffer= avcodec_default_get_buffer;
  181. s->release_buffer= avcodec_default_release_buffer;
  182. s->get_format= avcodec_default_get_format;
  183. s->me_subpel_quality=8;
  184. }
  185. /**
  186. * allocates a AVCodecContext and set it to defaults.
  187. * this can be deallocated by simply calling free()
  188. */
  189. AVCodecContext *avcodec_alloc_context(void){
  190. AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
  191. if(avctx==NULL) return NULL;
  192. avcodec_get_context_defaults(avctx);
  193. return avctx;
  194. }
  195. /**
  196. * allocates a AVPFrame and set it to defaults.
  197. * this can be deallocated by simply calling free()
  198. */
  199. AVFrame *avcodec_alloc_frame(void){
  200. AVFrame *pic= av_mallocz(sizeof(AVFrame));
  201. return pic;
  202. }
  203. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  204. {
  205. int ret;
  206. avctx->codec = codec;
  207. avctx->codec_id = codec->id;
  208. avctx->frame_number = 0;
  209. if (codec->priv_data_size > 0) {
  210. avctx->priv_data = av_mallocz(codec->priv_data_size);
  211. if (!avctx->priv_data)
  212. return -ENOMEM;
  213. } else {
  214. avctx->priv_data = NULL;
  215. }
  216. ret = avctx->codec->init(avctx);
  217. if (ret < 0) {
  218. av_freep(&avctx->priv_data);
  219. return ret;
  220. }
  221. return 0;
  222. }
  223. int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  224. const short *samples)
  225. {
  226. int ret;
  227. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  228. avctx->frame_number++;
  229. return ret;
  230. }
  231. int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  232. const AVFrame *pict)
  233. {
  234. int ret;
  235. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  236. emms_c(); //needed to avoid a emms_c() call before every return;
  237. avctx->frame_number++;
  238. return ret;
  239. }
  240. /* decode a frame. return -1 if error, otherwise return the number of
  241. bytes used. If no frame could be decompressed, *got_picture_ptr is
  242. zero. Otherwise, it is non zero */
  243. int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  244. int *got_picture_ptr,
  245. UINT8 *buf, int buf_size)
  246. {
  247. int ret;
  248. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  249. buf, buf_size);
  250. emms_c(); //needed to avoid a emms_c() call before every return;
  251. if (*got_picture_ptr)
  252. avctx->frame_number++;
  253. return ret;
  254. }
  255. /* decode an audio frame. return -1 if error, otherwise return the
  256. *number of bytes used. If no frame could be decompressed,
  257. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  258. *size in BYTES. */
  259. int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
  260. int *frame_size_ptr,
  261. UINT8 *buf, int buf_size)
  262. {
  263. int ret;
  264. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  265. buf, buf_size);
  266. avctx->frame_number++;
  267. return ret;
  268. }
  269. int avcodec_close(AVCodecContext *avctx)
  270. {
  271. if (avctx->codec->close)
  272. avctx->codec->close(avctx);
  273. av_freep(&avctx->priv_data);
  274. avctx->codec = NULL;
  275. return 0;
  276. }
  277. AVCodec *avcodec_find_encoder(enum CodecID id)
  278. {
  279. AVCodec *p;
  280. p = first_avcodec;
  281. while (p) {
  282. if (p->encode != NULL && p->id == id)
  283. return p;
  284. p = p->next;
  285. }
  286. return NULL;
  287. }
  288. AVCodec *avcodec_find_encoder_by_name(const char *name)
  289. {
  290. AVCodec *p;
  291. p = first_avcodec;
  292. while (p) {
  293. if (p->encode != NULL && strcmp(name,p->name) == 0)
  294. return p;
  295. p = p->next;
  296. }
  297. return NULL;
  298. }
  299. AVCodec *avcodec_find_decoder(enum CodecID id)
  300. {
  301. AVCodec *p;
  302. p = first_avcodec;
  303. while (p) {
  304. if (p->decode != NULL && p->id == id)
  305. return p;
  306. p = p->next;
  307. }
  308. return NULL;
  309. }
  310. AVCodec *avcodec_find_decoder_by_name(const char *name)
  311. {
  312. AVCodec *p;
  313. p = first_avcodec;
  314. while (p) {
  315. if (p->decode != NULL && strcmp(name,p->name) == 0)
  316. return p;
  317. p = p->next;
  318. }
  319. return NULL;
  320. }
  321. AVCodec *avcodec_find(enum CodecID id)
  322. {
  323. AVCodec *p;
  324. p = first_avcodec;
  325. while (p) {
  326. if (p->id == id)
  327. return p;
  328. p = p->next;
  329. }
  330. return NULL;
  331. }
  332. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  333. {
  334. const char *codec_name;
  335. AVCodec *p;
  336. char buf1[32];
  337. char channels_str[100];
  338. int bitrate;
  339. if (encode)
  340. p = avcodec_find_encoder(enc->codec_id);
  341. else
  342. p = avcodec_find_decoder(enc->codec_id);
  343. if (p) {
  344. codec_name = p->name;
  345. } else if (enc->codec_name[0] != '\0') {
  346. codec_name = enc->codec_name;
  347. } else {
  348. /* output avi tags */
  349. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  350. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  351. enc->codec_tag & 0xff,
  352. (enc->codec_tag >> 8) & 0xff,
  353. (enc->codec_tag >> 16) & 0xff,
  354. (enc->codec_tag >> 24) & 0xff);
  355. } else {
  356. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  357. }
  358. codec_name = buf1;
  359. }
  360. switch(enc->codec_type) {
  361. case CODEC_TYPE_VIDEO:
  362. snprintf(buf, buf_size,
  363. "Video: %s%s",
  364. codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
  365. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  366. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  367. ", %s",
  368. avcodec_get_pix_fmt_name(enc->pix_fmt));
  369. }
  370. if (enc->width) {
  371. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  372. ", %dx%d, %0.2f fps",
  373. enc->width, enc->height,
  374. (float)enc->frame_rate / FRAME_RATE_BASE);
  375. }
  376. if (encode) {
  377. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  378. ", q=%d-%d", enc->qmin, enc->qmax);
  379. }
  380. bitrate = enc->bit_rate;
  381. break;
  382. case CODEC_TYPE_AUDIO:
  383. snprintf(buf, buf_size,
  384. "Audio: %s",
  385. codec_name);
  386. switch (enc->channels) {
  387. case 1:
  388. strcpy(channels_str, "mono");
  389. break;
  390. case 2:
  391. strcpy(channels_str, "stereo");
  392. break;
  393. case 6:
  394. strcpy(channels_str, "5:1");
  395. break;
  396. default:
  397. sprintf(channels_str, "%d channels", enc->channels);
  398. break;
  399. }
  400. if (enc->sample_rate) {
  401. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  402. ", %d Hz, %s",
  403. enc->sample_rate,
  404. channels_str);
  405. }
  406. /* for PCM codecs, compute bitrate directly */
  407. switch(enc->codec_id) {
  408. case CODEC_ID_PCM_S16LE:
  409. case CODEC_ID_PCM_S16BE:
  410. case CODEC_ID_PCM_U16LE:
  411. case CODEC_ID_PCM_U16BE:
  412. bitrate = enc->sample_rate * enc->channels * 16;
  413. break;
  414. case CODEC_ID_PCM_S8:
  415. case CODEC_ID_PCM_U8:
  416. case CODEC_ID_PCM_ALAW:
  417. case CODEC_ID_PCM_MULAW:
  418. bitrate = enc->sample_rate * enc->channels * 8;
  419. break;
  420. default:
  421. bitrate = enc->bit_rate;
  422. break;
  423. }
  424. break;
  425. default:
  426. av_abort();
  427. }
  428. if (encode) {
  429. if (enc->flags & CODEC_FLAG_PASS1)
  430. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  431. ", pass 1");
  432. if (enc->flags & CODEC_FLAG_PASS2)
  433. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  434. ", pass 2");
  435. }
  436. if (bitrate != 0) {
  437. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  438. ", %d kb/s", bitrate / 1000);
  439. }
  440. }
  441. unsigned avcodec_version( void )
  442. {
  443. return LIBAVCODEC_VERSION_INT;
  444. }
  445. unsigned avcodec_build( void )
  446. {
  447. return LIBAVCODEC_BUILD;
  448. }
  449. /* must be called before any other functions */
  450. void avcodec_init(void)
  451. {
  452. static int inited = 0;
  453. if (inited != 0)
  454. return;
  455. inited = 1;
  456. //dsputil_init();
  457. }
  458. /* this can be called after seeking and before trying to decode the next keyframe */
  459. void avcodec_flush_buffers(AVCodecContext *avctx)
  460. {
  461. int i;
  462. MpegEncContext *s = avctx->priv_data;
  463. switch(avctx->codec_id){
  464. case CODEC_ID_MPEG1VIDEO:
  465. case CODEC_ID_H263:
  466. case CODEC_ID_RV10:
  467. case CODEC_ID_MJPEG:
  468. case CODEC_ID_MJPEGB:
  469. case CODEC_ID_MPEG4:
  470. case CODEC_ID_MSMPEG4V1:
  471. case CODEC_ID_MSMPEG4V2:
  472. case CODEC_ID_MSMPEG4V3:
  473. case CODEC_ID_WMV1:
  474. case CODEC_ID_WMV2:
  475. case CODEC_ID_H263P:
  476. case CODEC_ID_H263I:
  477. case CODEC_ID_SVQ1:
  478. for(i=0; i<MAX_PICTURE_COUNT; i++){
  479. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  480. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  481. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  482. }
  483. s->last_picture.data[0] = s->next_picture.data[0] = NULL;
  484. break;
  485. default:
  486. //FIXME
  487. break;
  488. }
  489. }
  490. static int raw_encode_init(AVCodecContext *s)
  491. {
  492. return 0;
  493. }
  494. static int raw_decode_frame(AVCodecContext *avctx,
  495. void *data, int *data_size,
  496. UINT8 *buf, int buf_size)
  497. {
  498. return -1;
  499. }
  500. static int raw_encode_frame(AVCodecContext *avctx,
  501. unsigned char *frame, int buf_size, void *data)
  502. {
  503. return -1;
  504. }
  505. AVCodec rawvideo_codec = {
  506. "rawvideo",
  507. CODEC_TYPE_VIDEO,
  508. CODEC_ID_RAWVIDEO,
  509. 0,
  510. raw_encode_init,
  511. raw_encode_frame,
  512. NULL,
  513. raw_decode_frame,
  514. };