VS1063a MP3 Encode Not Working. Decoding just fine

Writing software for systems that use VLSI Solution's devices as slave codecs to a host microcontroller.
vtech
User
Posts: 13
Joined: Thu 2020-01-30 22:06

VS1063a MP3 Encode Not Working. Decoding just fine

Post by vtech » Thu 2021-02-11 6:21

All,

I have been searching for a reason why my VS1063a isn't gracefully cancelling my Encoding. The decoding piece is working just fine. My difficulty is asserting SM_CANCEL and having the encoding end gracefully. I followed the example code, and I can't seem to see where i'm going sideways. I am attaching the Record Logic. I confirm that the SM_CANCEL is being set, and I can confirm it then gets cleared, but the encoding sometimes fails to stop.

I am assuming that the number of RECWORDS never stops, but I don't know why that is. Is there a setup that i'm missing ?

Code: Select all

void VS1063RecordFile(FILE *writeFp) {
	
	static u_int8 recBuf[REC_BUFFER_SIZE];
	u_int32 nextReportPos=0;      // File pointer where to next collect/report
	u_int32 fileSize = 0;

	int volLevel = ReadSci(SCI_VOL) & 0xFF;

	playerState = psPlayback;


	/* Initialize recording */

	/* This clock is high enough for both Ogg and MP3. */
	WriteSci(SCI_CLOCKF, HZ_TO_SC_FREQ(12288000) | SC_MULT_53_50X | SC_ADD_53_00X);

	// MP3 Recording setup
	WriteSci(SCI_RECRATE, 48000);
	WriteSci(SCI_RECGAIN, 0);

	WriteSci(SCI_RECMODE, RM_63_FORMAT_MP3 | RM_63_ADC_MODE_LEFT);
	WriteSci(SCI_RECQUALITY, RQ_MODE_VBR | RQ_MULT_1000 | 48);
  
	audioFormat = afMp3;

	// These 2 Lines will start the recording
	WriteSci(SCI_MODE, ReadSci(SCI_MODE) | SM_LINE1 | SM_ENCODE);
  	WriteSci(SCI_AIADDR, 0x0050); /* Activate recording! */

	while (playerState != psStopped) {
		int n;

		/* See if there is some data available */
		if ((n = ReadSci(SCI_RECWORDS)) > 0) {
		
			int i;
			u_int8 *rbp = recBuf;

			n = min(n, REC_BUFFER_SIZE/2);
			for (i=0; i<n; i++) {
				u_int16 w = ReadSci(SCI_RECDATA);
				*rbp++ = (u_int8)(w >> 8);
				*rbp++ = (u_int8)(w & 0xFF);
			}
			fwrite(recBuf, 1, 2*n, writeFp);
			fileSize += 2*n;
		} else {
			/* The following read from SCI_RECWORDS may appear redundant.
			But it's not: SCI_RECWORDS needs to be rechecked AFTER we
			have seen that SM_CANCEL have cleared. */
			if (playerState != psPlayback && !(ReadSci(SCI_MODE) & SM_CANCEL) && !ReadSci(SCI_RECWORDS)) {
			        playerState = psStopped;
			}
		}

		if (fileSize - nextReportPos >= REPORT_INTERVAL) {
			u_int16 sampleRate = ReadSci(SCI_AUDATA);
			nextReportPos += REPORT_INTERVAL;
	  
			printf("\r%ldKiB %lds %uHz %s %s",
				fileSize/1024,
				ReadVS10xxMem32Counter(PAR_SAMPLE_COUNTER) /
				(sampleRate & 0xFFFE),
				sampleRate & 0xFFFE,
				(sampleRate & 1) ? "stereo" : "mono",
				afName[audioFormat]);
			fflush(stdout);
		}
	} /* while (playerState != psStopped) */


	/* We need to check whether the file had an odd length.
	That information is available in the MSB of PAR_END_FILL_BYTE.
	In that case, the 8 LSB's are the missing byte, so we'll add
	it to the output file. */
	{
		u_int16 lastByte;
		lastByte = ReadVS10xxMem(PAR_END_FILL_BYTE);
		if (lastByte & 0x8000U) {
			printf("\nOdd length recording\n");
		} else {
			printf("\nEven length recording\n");
		} 
	}

	/* Finally, reset the VS10xx software, including realoading the
	patches package, to make sure everything is set up properly. */
	VSTestInitSoftware(0);

	printf("************* EXITING RECORDING ***************\n");
}

Attachments
RecFile.c
(3.88 KiB) Downloaded 6 times

User avatar
pasi
VLSI Staff
Posts: 1767
Joined: Thu 2010-07-15 16:04

Re: VS1063a MP3 Encode Not Working. Decoding just fine

Post by pasi » Thu 2021-02-11 14:05

Does that mean that SCI_RECWORDS never goes to 0 after setting SM_CANCEL?

The data FIFO could wrap if you read too many words, but I don't see any problem with your code. (Could happen e.g. reading RECWORDS immediately after reading RECDATA.)
Visit https://www.facebook.com/VLSISolution VLSI Solution on Facebook

vtech
User
Posts: 13
Joined: Thu 2020-01-30 22:06

Re: VS1063a MP3 Encode Not Working. Decoding just fine

Post by vtech » Fri 2021-02-12 17:20

Yes, that is correct. I am showing that RECWORDS is non-zero, but the same non-zero after each pass. For instance, it was reading 209 words over and over again. While sometimes, its 1201 words over and over again. I am confirming that the CANCEL is acknowledged, but the RECWORDS never go to zero, hence why I never drop out of the loop.

User avatar
pasi
VLSI Staff
Posts: 1767
Joined: Thu 2010-07-15 16:04

Re: VS1063a MP3 Encode Not Working. Decoding just fine

Post by pasi » Mon 2021-02-15 12:27

Which routine is run when you want to end encoding?

When you cancel the encoding, make sure you are not clearing the SM_ENCODE bit in SCI_MODE.

(Also, after finalizing, give a software (or hardware) reset before loading the patches package.)
Visit https://www.facebook.com/VLSISolution VLSI Solution on Facebook

vtech
User
Posts: 13
Joined: Thu 2020-01-30 22:06

Re: VS1063a MP3 Encode Not Working. Decoding just fine

Post by vtech » Tue 2021-02-23 15:25

It fails to end. Since the RECWORDS count is greater than zero, it just perpetually adds "garbage" to the end of the file and never seems to end. I never clear the SM_ENCODE flag. I simply set the SM_CANCEL and then wait, but the count doesn't go to zero. I have since just resorted to breaking out after a few seconds as the work-around and there doesn't seem to be a problem for now. I do run a software reset before loading the patches package. That is part of my finalization routine.

User avatar
pasi
VLSI Staff
Posts: 1767
Joined: Thu 2010-07-15 16:04

Re: VS1063a MP3 Encode Not Working. Decoding just fine

Post by pasi » Tue 2021-02-23 16:31

Can you post the code which sets SM_CANCEL? I think the problem must be there.
Visit https://www.facebook.com/VLSISolution VLSI Solution on Facebook

vtech
User
Posts: 13
Joined: Thu 2020-01-30 22:06

Re: VS1063a MP3 Encode Not Working. Decoding just fine

Post by vtech » Tue 2021-02-23 20:02

I have the cancel happen inside a handler that is triggered when I receive a signal SIGUSR1

Code: Select all

void sig_record_handler(int signum) 
{
	if (signum == SIGUSR1)
	{
		printf("Asserting the Cancel Now\n");
		WriteSci(SCI_MODE, ReadSci(SCI_MODE) | SM_CANCEL);
	}
}

Hannu
Senior User
Posts: 146
Joined: Mon 2016-05-30 11:54

Re: VS1063a MP3 Encode Not Working. Decoding just fine

Post by Hannu » Wed 2021-02-24 9:48

The signal could be the problem. I'm not a specialist with vs1063 (Pasi is) or UNIX signals as I like to select my battles which I can win. And writing really portable signal handler is hard.

The execution environment of signal context differs normal execution environment of the code. Also as signals are asynchronous, what makes sure that you don't have already SCI write going on?

What I would do is to have some global variable something like volatile int pleaseStop = 0; and from signal handler I would set it. Then in read loop I would check that variable and send the cancel and read the rest data out.

User avatar
pasi
VLSI Staff
Posts: 1767
Joined: Thu 2010-07-15 16:04

Re: VS1063a MP3 Encode Not Working. Decoding just fine

Post by pasi » Wed 2021-02-24 15:36

Your WriteSci() in the signal handler can occur at any time, even during an ongoing SCI operation.

See Hannu's suggestion above about setting a variable in the signal handler instead (and declare it volatile to be certain), and only performing SCI and SDI operations in the main loop.
Visit https://www.facebook.com/VLSISolution VLSI Solution on Facebook

vtech
User
Posts: 13
Joined: Thu 2020-01-30 22:06

Re: VS1063a MP3 Encode Not Working. Decoding just fine

Post by vtech » Thu 2021-02-25 2:42

Ahhhhhhh. Thanks guys. That makes sense. I'll give that a shot.

Post Reply