1 /*
2  *			GPAC - Multimedia Framework C SDK
3  *
4  *			Authors: Arash Shafiei
5  *			Copyright (c) Telecom ParisTech 2000-2013
6  *					All rights reserved
7  *
8  *  This file is part of GPAC / dashcast
9  *
10  *  GPAC is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU Lesser General Public License as published by
12  *  the Free Software Foundation; either version 2, or (at your option)
13  *  any later version.
14  *
15  *  GPAC is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU Lesser General Public License for more details.
19  *
20  *  You should have received a copy of the GNU Lesser General Public
21  *  License along with this library; see the file COPYING.  If not, write to
22  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  */
25 
26 #ifndef VIDEO_MUXER_H_
27 #define VIDEO_MUXER_H_
28 
29 #include "../../modules/ffmpeg_in/ffmpeg_in.h"
30 #include "libavformat/avformat.h"
31 #include "libavdevice/avdevice.h"
32 #include "libswscale/swscale.h"
33 #include "libavutil/mathematics.h"
34 #include <gpac/isomedia.h>
35 #include <gpac/internal/media_dev.h>
36 
37 #include "video_scaler.h"
38 
39 typedef enum {
40 	FFMPEG_VIDEO_MUXER,
41 	RAW_VIDEO_H264,
42 	GPAC_VIDEO_MUXER,
43 	GPAC_INIT_VIDEO_MUXER_AVC1,
44 	GPAC_INIT_VIDEO_MUXER_AVC3
45 } VideoMuxerType;
46 
47 /*
48  * VideoOutputFile structure has the data needed to encode video frames and write them on the file.
49  * It reads the data from a circular buffer so it needs to keep the index to that circular buffer. This index is
50  * available in Consumer data structure.
51  */
52 typedef struct {
53 	VideoDataConf *video_data_conf;
54 	VideoMuxerType muxer_type;
55 
56 	/* file format context structure */
57 	AVFormatContext *av_fmt_ctx;
58 	AVCodecContext *codec_ctx;
59 	AVCodec *codec;
60 
61 	FILE *file;
62 
63 #ifndef GPAC_DISABLE_ISOM
64 	GF_ISOFile *isof;
65 	GF_ISOSample *sample;
66 #endif
67 
68 	u32 trackID;
69 	/* Index of the video stream in the file */
70 	int vstream_idx;
71 	/* keeps the index with which encoder access to the circular buffer (as a consumer) */
72 	Consumer consumer;
73 
74 	/* Variables that encoder needs to encode data */
75 	uint8_t *vbuf;
76 	int vbuf_size;
77 	int encoded_frame_size;
78 
79 	int frame_per_fragment;
80 	int frame_per_segment;
81 
82 	int seg_dur;
83 	int frag_dur;
84 
85 	u64 first_dts_in_fragment;
86 	u64 ntp_at_first_dts;
87 
88 	u32 seg_marker;
89 
90 	int gop_size;
91 	int gdr;
92 
93 	Bool use_source_timing;
94 
95 	u64 pts_at_segment_start, pts_at_first_segment;
96 	u64 last_pts, last_dts;
97 	u64 frame_dur;
98 	u32 timescale;
99 	u32 nb_segments;
100 	Bool fragment_started, segment_started;
101 	const char *rep_id;
102 
103 	u64 frame_ntp, frame_utc;
104 } VideoOutputFile;
105 
106 int dc_video_muxer_init(VideoOutputFile *video_output_file, VideoDataConf *video_data_conf, VideoMuxerType muxer_type, int frame_per_segment, int frame_per_fragment, u32 seg_marker, int gdr, int seg_dur, int frag_dur, int frame_dur, int gop_size, int video_cb_size);
107 
108 int dc_video_muxer_free(VideoOutputFile *video_output_file);
109 
110 int dc_video_muxer_open(VideoOutputFile *video_output_file, char *directory, char *id_name, int seg);
111 
112 int dc_video_muxer_write(VideoOutputFile *video_output_file, int frame_nb, Bool insert_ntp_timestamp);
113 
114 int dc_video_muxer_close(VideoOutputFile *video_output_file);
115 
116 #endif /* VIDEO_MUXER_H_ */
117