comparison mpeg.c @ 543:acf8a88674ff libavformat

cleanup
author michael
date Sun, 03 Oct 2004 02:57:42 +0000
parents be81a0f1974d
children 1244786bfb26
comparison
equal deleted inserted replaced
542:be81a0f1974d 543:acf8a88674ff
41 int buffer_index; 41 int buffer_index;
42 PacketDesc *predecode_packet; 42 PacketDesc *predecode_packet;
43 PacketDesc *premux_packet; 43 PacketDesc *premux_packet;
44 PacketDesc **next_packet; 44 PacketDesc **next_packet;
45 int packet_number; 45 int packet_number;
46 int64_t start_pts;
47 int64_t start_dts;
48 uint8_t lpcm_header[3]; 46 uint8_t lpcm_header[3];
49 int lpcm_align; 47 int lpcm_align;
50 } StreamInfo; 48 } StreamInfo;
51 49
52 typedef struct { 50 typedef struct {
406 s->system_header_freq = s->pack_header_freq * 5; 404 s->system_header_freq = s->pack_header_freq * 5;
407 405
408 for(i=0;i<ctx->nb_streams;i++) { 406 for(i=0;i<ctx->nb_streams;i++) {
409 stream = ctx->streams[i]->priv_data; 407 stream = ctx->streams[i]->priv_data;
410 stream->packet_number = 0; 408 stream->packet_number = 0;
411 stream->start_pts = AV_NOPTS_VALUE;
412 stream->start_dts = AV_NOPTS_VALUE;
413 } 409 }
414 s->system_header_size = get_system_header_size(ctx); 410 s->system_header_size = get_system_header_size(ctx);
415 s->last_scr = 0; 411 s->last_scr = 0;
416 return 0; 412 return 0;
417 fail: 413 fail:
838 sector (it represents the sector index, not the MPEG pack index) 834 sector (it represents the sector index, not the MPEG pack index)
839 (see VCD standard p. IV-6)*/ 835 (see VCD standard p. IV-6)*/
840 s->packet_number++; 836 s->packet_number++;
841 } 837 }
842 838
843 static int64_t update_scr(AVFormatContext *ctx,int stream_index,int64_t pts) 839 static int64_t get_vcd_scr(AVFormatContext *ctx,int stream_index,int64_t pts)
844 { 840 {
845 MpegMuxContext *s = ctx->priv_data; 841 MpegMuxContext *s = ctx->priv_data;
846 int64_t scr; 842 int64_t scr;
847 StreamInfo *stream; 843
848 int i;
849
850 if (s->is_vcd) {
851 /* Since the data delivery rate is constant, SCR is computed 844 /* Since the data delivery rate is constant, SCR is computed
852 using the formula C + i * 1200 where C is the start constant 845 using the formula C + i * 1200 where C is the start constant
853 and i is the pack index. 846 and i is the pack index.
854 It is recommended that SCR 0 is at the beginning of the VCD front 847 It is recommended that SCR 0 is at the beginning of the VCD front
855 margin (a sequence of empty Form 2 sectors on the CD). 848 margin (a sequence of empty Form 2 sectors on the CD).
857 we use C = 30*1200 = 36000 850 we use C = 30*1200 = 36000
858 (Note that even if the front margin is not 30 sectors the file 851 (Note that even if the front margin is not 30 sectors the file
859 will still be correct according to the standard. It just won't have 852 will still be correct according to the standard. It just won't have
860 the "recommended" value).*/ 853 the "recommended" value).*/
861 scr = 36000 + s->packet_number * 1200; 854 scr = 36000 + s->packet_number * 1200;
862
863
864 #if 0
865 for(i=0;i<ctx->nb_streams;i++) {
866 stream = ctx->streams[i]->priv_data;
867
868 if(scr > stream->start_pts && stream->start_pts!=AV_NOPTS_VALUE) {
869 av_log(ctx, AV_LOG_DEBUG, "mpeg vcd: SCR above PTS (scr=%0.3f, stream index=%d, stream_pts=%0.3f).\n", scr/90000.0, i, stream->start_pts / 90000.0);
870 }
871 }
872 #endif
873 }
874 else {
875
876
877 /* XXX I believe this calculation of SCR is wrong. SCR
878 specifies at which time the data should enter the decoder.
879 Two packs cannot enter the decoder at the same time. */
880
881 /* XXX: system clock should be computed precisely, especially for
882 CBR case. The current mode gives at least something coherent */
883 if (stream_index == 0
884 && pts != AV_NOPTS_VALUE)
885 scr = pts;
886 else
887 scr = s->last_scr;
888 }
889
890 s->last_scr=scr;
891 855
892 return scr; 856 return scr;
893 } 857 }
894 858
895 static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){ 859 static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){
998 if (s->is_vcd) { 962 if (s->is_vcd) {
999 /* Write one or more padding sectors, if necessary, to reach 963 /* Write one or more padding sectors, if necessary, to reach
1000 the constant overall bitrate.*/ 964 the constant overall bitrate.*/
1001 int vcd_pad_bytes; 965 int vcd_pad_bytes;
1002 966
1003 while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->start_pts) ) >= s->packet_size){ //FIXME pts cannot be correct here 967 while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->premux_packet->pts) ) >= s->packet_size){ //FIXME pts cannot be correct here
1004 put_vcd_padding_sector(ctx); 968 put_vcd_padding_sector(ctx);
1005 s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet 969 s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
1006 } 970 }
1007 } 971 }
1008 972
1028 int stream_index= pkt->stream_index; 992 int stream_index= pkt->stream_index;
1029 int size= pkt->size; 993 int size= pkt->size;
1030 uint8_t *buf= pkt->data; 994 uint8_t *buf= pkt->data;
1031 AVStream *st = ctx->streams[stream_index]; 995 AVStream *st = ctx->streams[stream_index];
1032 StreamInfo *stream = st->priv_data; 996 StreamInfo *stream = st->priv_data;
1033 int64_t pts, dts, new_start_pts, new_start_dts; 997 int64_t pts, dts;
1034 int len, avail_size;
1035 PacketDesc *pkt_desc; 998 PacketDesc *pkt_desc;
1036 999
1037 pts= pkt->pts; 1000 pts= pkt->pts;
1038 dts= pkt->dts; 1001 dts= pkt->dts;
1039 1002
1079 } 1042 }
1080 fifo_write(&stream->fifo, buf, size, &stream->fifo.wptr); 1043 fifo_write(&stream->fifo, buf, size, &stream->fifo.wptr);
1081 1044
1082 for(;;){ 1045 for(;;){
1083 int ret= output_packet(ctx, 0); 1046 int ret= output_packet(ctx, 0);
1084 if(ret<0) 1047 if(ret<=0)
1085 return ret; 1048 return ret;
1086 else if(ret==0) 1049 }
1087 break;
1088 }
1089
1090 #if 0
1091
1092 /* we assume here that pts != AV_NOPTS_VALUE */
1093 new_start_pts = stream->start_pts;
1094 new_start_dts = stream->start_dts;
1095
1096 if (stream->start_pts == AV_NOPTS_VALUE) {
1097 new_start_pts = pts;
1098 new_start_dts = dts;
1099 }
1100 avail_size = get_packet_payload_size(ctx, stream_index,
1101 new_start_pts,
1102 new_start_dts);
1103 if (stream->buffer_ptr >= avail_size) {
1104
1105 update_scr(ctx,stream_index,stream->start_pts);
1106
1107 /* unlikely case: outputing the pts or dts increase the packet
1108 size so that we cannot write the start of the next
1109 packet. In this case, we must flush the current packet with
1110 padding.
1111 Note: this always happens for the first audio and video packet
1112 in a VCD file, since they do not carry any data.*/
1113 flush_packet(ctx, stream_index,
1114 stream->start_pts, stream->start_dts, s->last_scr);
1115 stream->buffer_ptr = 0;
1116 }
1117 stream->start_pts = new_start_pts;
1118 stream->start_dts = new_start_dts;
1119 stream->nb_frames++;
1120 while (size > 0) {
1121 avail_size = get_packet_payload_size(ctx, stream_index,
1122 stream->start_pts,
1123 stream->start_dts);
1124 len = avail_size - stream->buffer_ptr;
1125 if (len > size)
1126 len = size;
1127 memcpy(stream->buffer + stream->buffer_ptr, buf, len);
1128 stream->buffer_ptr += len;
1129 buf += len;
1130 size -= len;
1131 if (stream->buffer_ptr >= avail_size) {
1132
1133 update_scr(ctx,stream_index,stream->start_pts);
1134
1135 /* if packet full, we send it now */
1136 flush_packet(ctx, stream_index,
1137 stream->start_pts, stream->start_dts, s->last_scr);
1138 stream->buffer_ptr = 0;
1139
1140 if (s->is_vcd) {
1141 /* Write one or more padding sectors, if necessary, to reach
1142 the constant overall bitrate.*/
1143 int vcd_pad_bytes;
1144
1145 while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->start_pts) ) >= s->packet_size)
1146 put_vcd_padding_sector(ctx);
1147 }
1148
1149 /* Make sure only the FIRST pes packet for this frame has
1150 a timestamp */
1151 stream->start_pts = AV_NOPTS_VALUE;
1152 stream->start_dts = AV_NOPTS_VALUE;
1153 }
1154 }
1155 #endif
1156 return 0;
1157 } 1050 }
1158 1051
1159 static int mpeg_mux_end(AVFormatContext *ctx) 1052 static int mpeg_mux_end(AVFormatContext *ctx)
1160 { 1053 {
1161 // MpegMuxContext *s = ctx->priv_data; 1054 // MpegMuxContext *s = ctx->priv_data;