comparison mencoder.c @ 37101:ab6b3fd992dc

Split "len" variable into an additional bytes_to_mux. This should make the code less confusion and less risky to change.
author reimar
date Sat, 10 May 2014 20:23:20 +0000
parents 3466f03a5256
children 0bbd04a5f920
comparison
equal deleted inserted replaced
37100:3466f03a5256 37101:ab6b3fd992dc
1308 1308
1309 if(sh_audio){ 1309 if(sh_audio){
1310 // get audio: 1310 // get audio:
1311 while(force_audio || a_muxer_time-audio_preload<v_muxer_time){ 1311 while(force_audio || a_muxer_time-audio_preload<v_muxer_time){
1312 float tottime; 1312 float tottime;
1313 int len=0; 1313 int bytes_to_mux=0;
1314 force_audio = 0; 1314 force_audio = 0;
1315 1315
1316 ptimer_start = GetTimerMS(); 1316 ptimer_start = GetTimerMS();
1317 // CBR - copy 0.5 sec of audio 1317 // CBR - copy 0.5 sec of audio
1318 // or until the end of video: 1318 // or until the end of video:
1328 1328
1329 if(aencoder) 1329 if(aencoder)
1330 { 1330 {
1331 if(mux_a->h.dwSampleSize) /* CBR */ 1331 if(mux_a->h.dwSampleSize) /* CBR */
1332 { 1332 {
1333 int len;
1333 if(aencoder->set_decoded_len) 1334 if(aencoder->set_decoded_len)
1334 { 1335 {
1335 len = mux_a->h.dwSampleSize*(int)(mux_a->h.dwRate*tottime); 1336 len = mux_a->h.dwSampleSize*(int)(mux_a->h.dwRate*tottime);
1336 aencoder->set_decoded_len(aencoder, len); 1337 aencoder->set_decoded_len(aencoder, len);
1337 } 1338 }
1339 len = aencoder->decode_buffer_size; 1340 len = aencoder->decode_buffer_size;
1340 1341
1341 len = dec_audio(sh_audio, aencoder->decode_buffer, len); 1342 len = dec_audio(sh_audio, aencoder->decode_buffer, len);
1342 mux_a->buffer_len += aencoder->encode(aencoder, mux_a->buffer + mux_a->buffer_len, 1343 mux_a->buffer_len += aencoder->encode(aencoder, mux_a->buffer + mux_a->buffer_len,
1343 len <= 0 && sh_audio->ds->eof ? NULL : aencoder->decode_buffer, len, mux_a->buffer_size-mux_a->buffer_len); 1344 len <= 0 && sh_audio->ds->eof ? NULL : aencoder->decode_buffer, len, mux_a->buffer_size-mux_a->buffer_len);
1344 if(mux_a->buffer_len < mux_a->wf->nBlockAlign) 1345 if(mux_a->buffer_len >= mux_a->wf->nBlockAlign)
1345 len = 0; 1346 bytes_to_mux = mux_a->wf->nBlockAlign*(mux_a->buffer_len/mux_a->wf->nBlockAlign);
1346 else
1347 len = mux_a->wf->nBlockAlign*(mux_a->buffer_len/mux_a->wf->nBlockAlign);
1348 } 1347 }
1349 else /* VBR */ 1348 else /* VBR */
1350 { 1349 {
1351 int sz = 0; 1350 int sz = 0;
1352 while(1) 1351 while(1)
1353 { 1352 {
1354 len = 0; 1353 int len;
1355 if(! sz) 1354 if(! sz)
1356 sz = aencoder->get_frame_size(aencoder); 1355 sz = aencoder->get_frame_size(aencoder);
1357 if(sz > 0 && mux_a->buffer_len >= sz) 1356 if(sz > 0 && mux_a->buffer_len >= sz)
1358 { 1357 {
1359 len = sz; 1358 bytes_to_mux = sz;
1360 break; 1359 break;
1361 } 1360 }
1362 len = dec_audio(sh_audio,aencoder->decode_buffer, aencoder->decode_buffer_size); 1361 len = dec_audio(sh_audio,aencoder->decode_buffer, aencoder->decode_buffer_size);
1363 if(len <= 0) 1362 if(len <= 0)
1364 { 1363 {
1374 } 1373 }
1375 if (v_muxer_time == 0) mux_a->h.dwInitialFrames++; 1374 if (v_muxer_time == 0) mux_a->h.dwInitialFrames++;
1376 } 1375 }
1377 else { 1376 else {
1378 if(mux_a->h.dwSampleSize){ 1377 if(mux_a->h.dwSampleSize){
1378 int len;
1379 switch(mux_a->codec){ 1379 switch(mux_a->codec){
1380 case ACODEC_COPY: // copy 1380 case ACODEC_COPY: // copy
1381 len=mux_a->wf->nAvgBytesPerSec*tottime; 1381 len=mux_a->wf->nAvgBytesPerSec*tottime;
1382 len/=mux_a->h.dwSampleSize;if(len<1) len=1; 1382 len/=mux_a->h.dwSampleSize;if(len<1) len=1;
1383 len*=mux_a->h.dwSampleSize; 1383 len*=mux_a->h.dwSampleSize;
1384 len=demux_read_data(sh_audio->ds,mux_a->buffer,len); 1384 bytes_to_mux=demux_read_data(sh_audio->ds,mux_a->buffer,len);
1385 break; 1385 break;
1386 } 1386 }
1387 } else { 1387 } else {
1388 // VBR - encode/copy an audio frame 1388 // VBR - encode/copy an audio frame
1389 switch(mux_a->codec){ 1389 switch(mux_a->codec){
1390 case ACODEC_COPY: // copy 1390 case ACODEC_COPY: // copy
1391 len=ds_get_packet(sh_audio->ds,(unsigned char**) &mux_a->buffer); 1391 bytes_to_mux=ds_get_packet(sh_audio->ds,(unsigned char**) &mux_a->buffer);
1392 break; 1392 break;
1393 } 1393 }
1394 } 1394 }
1395 } 1395 }
1396 if(len<=0) { 1396 if(bytes_to_mux<=0) {
1397 // EOF? 1397 // EOF?
1398 if (!sh_audio->a_out_buffer_len && sh_audio->ds->eof) { 1398 if (!sh_audio->a_out_buffer_len && sh_audio->ds->eof) {
1399 if (mux_a->buffer_len) 1399 if (mux_a->buffer_len)
1400 mp_msg(MSGT_MENCODER, MSGL_WARN, "Audio data left in buffer at end of file. Probably bug in audio encoder, please report."); 1400 mp_msg(MSGT_MENCODER, MSGL_WARN, "Audio data left in buffer at end of file. Probably bug in audio encoder, please report.");
1401 at_eof |= 2; 1401 at_eof |= 2;
1402 } 1402 }
1403 break; 1403 break;
1404 } 1404 }
1405 muxer_write_chunk(mux_a,len,AVIIF_KEYFRAME, MP_NOPTS_VALUE, MP_NOPTS_VALUE); 1405 muxer_write_chunk(mux_a,bytes_to_mux,AVIIF_KEYFRAME, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1406 a_muxer_time = adjusted_muxer_time(mux_a); // update after muxing 1406 a_muxer_time = adjusted_muxer_time(mux_a); // update after muxing
1407 if(!mux_a->h.dwSampleSize && a_muxer_time>0) 1407 if(!mux_a->h.dwSampleSize && a_muxer_time>0)
1408 mux_a->wf->nAvgBytesPerSec=0.5f+(double)mux_a->size/a_muxer_time; // avg bps (VBR) 1408 mux_a->wf->nAvgBytesPerSec=0.5f+(double)mux_a->size/a_muxer_time; // avg bps (VBR)
1409 if(mux_a->buffer_len>=len){ 1409 if(mux_a->buffer_len>=bytes_to_mux){
1410 mux_a->buffer_len-=len; 1410 mux_a->buffer_len-=bytes_to_mux;
1411 memmove(mux_a->buffer,mux_a->buffer+len,mux_a->buffer_len); 1411 memmove(mux_a->buffer,mux_a->buffer+bytes_to_mux,mux_a->buffer_len);
1412 } 1412 }
1413 1413
1414 1414
1415 audiosamples++; 1415 audiosamples++;
1416 audiorate+= (GetTimerMS() - ptimer_start); 1416 audiorate+= (GetTimerMS() - ptimer_start);